From 7031fd13d768e451e002377ecc72625d0b0b9d97 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 28 Feb 2022 21:47:46 -0600 Subject: [PATCH] ugoiras: encode .webm samples using VP9 instead of VP8. Switch the codec for .webm samples from VP8 to VP9. All modern browsers support VP9 (Safari was the last to add support in ~2020), so it should be safe to provide only VP9 .webms without a fallback. VP9 lets us use two-pass encoding, which should offer better compression. Fixes ugoira samples still having poor quality even after 4c652cf3e. 4c652cf3e tried to remove the max bitrate limit by setting `-b:v 0`, but this only worked in FFmpeg 4.2. In production Danbooru uses FFmpeg 4.4, and apparently in 4.4 `-b:v 0` means "use the default max bitrate of 256kb/s" instead of "no bitrate limit". https://trac.ffmpeg.org/wiki/Encode/VP9 https://developers.google.com/media/vp9/bitrate-modes https://developers.google.com/media/vp9/settings/vod http://wiki.webmproject.org/ffmpeg/vp9-encoding-guide https://www.reddit.com/r/AV1/comments/k7colv/encoder_tuning_part_1_tuning_libvpxvp9_be_more/ --- app/logical/media_file/ugoira.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/logical/media_file/ugoira.rb b/app/logical/media_file/ugoira.rb index 88978de82..56eb9d2ae 100644 --- a/app/logical/media_file/ugoira.rb +++ b/app/logical/media_file/ugoira.rb @@ -85,7 +85,10 @@ class MediaFile::Ugoira < MediaFile end ext = zipfile.first.name.match(/\.(\w{,4})$/)[1] - ffmpeg_out, status = Open3.capture2e("ffmpeg -i #{tmpdir}/images/%06d.#{ext} -codec:v libvpx -crf 12 -b:v 0 -an #{tmpdir}/tmp.webm") + ffmpeg_out, status = Open3.capture2e("ffmpeg -i #{tmpdir}/images/%06d.#{ext} -codec:v libvpx-vp9 -crf 12 -b:v 0 -an -threads 8 -tile-columns 2 -tile-rows 1 -row-mt 1 -pass 1 -f null /dev/null") + raise Error, "ffmpeg failed: #{ffmpeg_out}" unless status.success? + + ffmpeg_out, status = Open3.capture2e("ffmpeg -i #{tmpdir}/images/%06d.#{ext} -codec:v libvpx-vp9 -crf 12 -b:v 0 -an -threads 8 -tile-columns 2 -tile-rows 1 -row-mt 1 -pass 2 #{tmpdir}/tmp.webm") raise Error, "ffmpeg failed: #{ffmpeg_out}" unless status.success? mkvmerge_out, status = Open3.capture2e("mkvmerge -o #{output_file.path} --webm --timecodes 0:#{tmpdir}/timecodes.tc #{tmpdir}/tmp.webm")