Generate ffmpeg command for two-pass encoding

This commit is contained in:
pta 2023-08-16 11:19:39 -04:00
parent 4261c11cb9
commit 5658058cb3
1 changed files with 22 additions and 4 deletions

View File

@ -124,9 +124,23 @@ local function isss(x)
end
end
local function cliplength(start, finish)
return finish - start
end
local targetsize = 32000 -- 4ch's 4M filesize limit in kilobits
local function copyff(x)
local a = mp.get_property("ab-loop-a")
local b = mp.get_property("ab-loop-b")
-- functional style code but mpv's lua version doesn't support anonymous functions
-- local a = (function(x) if type(x) == "number" then return x else return 0 end)(mp.get_property("ab-loop-a"))
-- local b = (function(x) if type(x) == "number" then return x else return mp.get_property("duration") end)(mp.get_property("ab-loop-b"))
local a = mp.get_property_native("ab-loop-a")
local a = (type(a) == "number") and a or 0
local b = mp.get_property_native("ab-loop-b")
local b = (type(b) == "number") and b or mp.get_property_native("duration")
local duration = cliplength(a, b)
local targetbitrate = targetsize / duration -- kilobits/s
local targetbitratestring = " -b:v " .. targetbitrate .. "k "
local vft = mp.get_property_native("vf")
local crop_was_set = (#vft > 0)
-- added following crop condition check because when manually cropping, placing the
@ -156,13 +170,17 @@ local function copyff(x)
local vf = not x and crop_was_set and " -vf " .. c or ""
local fc = x and " -f lavfi -i color=white:s=1698x102 -filter_complex overlay=33:961," .. c or ""
-- constrained quality https://trac.ffmpeg.org/wiki/Encode/VP9#constrainedq
local cq = " -crf 32 -b:v 2200k "
-- I usually run mpv from Dired in emacs, using the "openwith" package, which calls mpv
-- with the full path to the video, so the copied ffmpeg command can find the file from
-- any working-directory. Running mpv from the shell sets the mpv's "path" property to
-- whatever path you used on the command line.
local f = mp.get_property("path")
local cmd = "ffmpeg " .. " -i " .. "'file:" .. f .. "'" .. vf .. fc .. cq .. isss(a) .. isto(b) .. " /tmp/a.webm"
-- local cmd = "ffmpeg " .. " -i " .. "'file:" .. f .. "'" .. vf .. fc .. cq .. isss(a) .. isto(b) .. " /tmp/a.webm"
local cmd = "ffmpeg -i 'file:" .. f .. "'" ..
vf .. fc .. " -c:v libvpx-vp9" .. targetbitratestring .. "-pass 1" .. isss(a) .. isto(b) ..
" -an -f null /dev/null && ffmpeg -hide_banner -y -i 'file:" .. f .. "'" ..
vf .. fc .. " -c:v libvpx-vp9" .. targetbitratestring .. "-pass 2" .. isss(a) .. isto(b) ..
" -c:a libopus /tmp/a.webm; rm -f ffmpeg2pass-0.log"
if set_clipboard(cmd) then
mp.osd_message(string.format("Copied to Clipboard: %s", cmd))
else