summaryrefslogtreecommitdiff
path: root/lib/booru/image_processing/webm_processor.rb
diff options
context:
space:
mode:
authorAppleDash <[email protected]>2020-08-07 19:27:33 -0400
committerAppleDash <[email protected]>2020-08-07 19:27:33 -0400
commit07c9e6fcf2ae7b0e22c2333ebb3ffe6a5e3a1d3c (patch)
treebec48d0f2665921e62d5fb17deacc439305ac1b6 /lib/booru/image_processing/webm_processor.rb
parent32a3f9346b8b39ab5b506019629fb416c86f2228 (diff)
Rewrite image processing engine pretty majorly
Diffstat (limited to 'lib/booru/image_processing/webm_processor.rb')
-rw-r--r--lib/booru/image_processing/webm_processor.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/booru/image_processing/webm_processor.rb b/lib/booru/image_processing/webm_processor.rb
new file mode 100644
index 0000000..8a022cc
--- /dev/null
+++ b/lib/booru/image_processing/webm_processor.rb
@@ -0,0 +1,51 @@
+require 'shellwords'
+require 'svg_scrubber'
+
+module Booru
+ module ImageProcessing
+ class WebMProcessor < Processor
+ include ShellHelper
+
+ def initialize(original_path, directory)
+ super(original_path, directory)
+ @rasterized = File.join(@directory, 'still.png')
+ end
+
+ def pre_process
+ median_time = `ffprobe -i #{@original_path.shellescape} -show_entries format=duration -v quiet -of csv="p=0"`.to_f / 2
+ `ffmpeg -loglevel quiet -n -i #{@original_path.shellescape} -ss #{median_time} -frames:v 1 #{@rasterized.shellescape}`
+ end
+
+ def is_video?
+ true
+ end
+
+ def generate_version(dimensions, destination_path)
+ width, height = dimensions
+ width &= 4_294_967_294
+ height &= 4_294_967_294
+
+ # generate webm thumb
+ `ffmpeg -loglevel 0 -threads 0 -y -i #{@original_path.shellescape} -c:v libvpx -b:v 1M -crf 10 -speed 16 -vf "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease" #{destination_path.shellescape}`
+ # generate mp4 thumb
+ `ffmpeg -loglevel 0 -threads 0 -y -i #{@original_path.shellescape} -vf "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease, scale=trunc(iw/2)*2:trunc(ih/2)*2" -preset veryfast #{destination_path.gsub(/webm\z/, 'mp4').shellescape}`
+ end
+
+ def generate_full_version(destination_path)
+ mp4_path = File.join(File.dirname(destination_path), 'full.mp4')
+ FileUtils.ln_sf(@original_path, destination_path)
+
+ # generate full.mp4 from full.webm
+ `ffmpeg -loglevel 0 -threads 0 -y -i #{@original_path} -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -preset veryfast #{mp4_path}`
+ end
+
+ def additional_process
+ # generate gifs from webms; no idea why this is special-cased.
+ duration = `ffprobe -loglevel quiet -of 'compact=nokey=1:print_section=0' -show_format_entry duration #{@original_path.shellescape}`.chomp.to_f / 10
+ [[250, :thumb], [150, :thumb_small], [50, :thumb_tiny]].each do |w, n|
+ `ffmpeg -loglevel 0 -i #{@original_path.shellescape} -vf "fps=1/#{duration},scale=w=#{w}:h=#{w}:force_original_aspect_ratio=decrease" -vframes 10 -qscale:v 2 -f image2pipe -vcodec ppm - | convert -delay 50 -loop 0 - gif:- | gifsicle -O2 > #{@directory}/#{n}.gif`
+ end
+ end
+ end
+ end
+end