--@action MP4 copies of marked videos --@version 1.0.0 --@author TheFile.Ninja --@id mp4-copies --@description Makes an MP4 copy (H.264 and AAC) beside each marked video with ffmpeg, in the background, one after the other, with a progress bar. Needs ffmpeg. --@tags video, terminal, background --@requires terminal -- ffmpeg is not part of TheFile.Ninja: winget install Gyan.FFmpeg, then -- start the app again so that it finds it. The originals are never touched; -- a copy that is there already is skipped. Cancel in the strip under the -- panes stops the one being made (its unfinished file is left) and the rest. local ARGS = { "-c:v", "libx264", "-crf", "23", "-preset", "medium", "-c:a", "aac", "-b:a", "160k", "-movflags", "+faststart" } if not shell.which("ffmpeg") then ui.message("ffmpeg is not on the PATH.\n\nwinget install Gyan.FFmpeg, then start TheFile.Ninja again.", "MP4 copies", "error") return end local videos = {} for _, f in ipairs(pane.active():effective_selection()) do if f.is_file then videos[#videos + 1] = f end end if #videos == 0 then ui.message("Mark the videos first, or put the cursor on one.", "MP4 copies") return end local function seconds(h, m, s) return tonumber(h) * 3600 + tonumber(m) * 60 + tonumber(s) end local prog = ui.progress("MP4 copies") local done, failed = 0, 0 local index, job = 0, nil local function next_video() index = index + 1 local f = videos[index] if not f or prog:is_cancelled() then prog:finish(failed == 0 and (done .. " made") or (done .. " made, " .. failed .. " failed")) return end local name = path.stem(f.name) .. (f.extension:lower() == "mp4" and " (copy).mp4" or ".mp4") local out = path.join(path.parent(f.full_path), name) if fs.exists(out) then ui.log_warning("Already there: " .. out) return next_video() end prog:update(math.floor((index - 1) * 100 / #videos), f.name) local argv = { "ffmpeg", "-hide_banner", "-nostdin", "-n", "-progress", "pipe:1", "-i", f.full_path } for _, a in ipairs(ARGS) do argv[#argv + 1] = a end argv[#argv + 1] = out local duration job = terminal.run{ argv = argv, cwd = path.parent(f.full_path), title = "ffmpeg " .. f.name, cols = 300, timeout_ms = 3600000, on_line = function(line) if prog:is_cancelled() then job:cancel() return end local h, m, s = line:match("Duration: (%d+):(%d+):([%d%.]+)") if h then duration = seconds(h, m, s) end -- -progress writes how far it is, in microseconds, a line at a time. local us = tonumber(line:match("^out_time_[mu]s=(%d+)")) if us and duration and duration > 0 then local part = math.min(1, us / 1e6 / duration) prog:update(math.floor((index - 1 + part) * 100 / #videos), f.name) end end, on_done = function(r) if r.ok then done = done + 1 elseif r.cancelled then ui.log_warning("Stopped: " .. out .. " is unfinished") else failed = failed + 1 ui.log_error(f.name .. ": ffmpeg ended with " .. tostring(r.exit_code) .. "\n" .. table.concat(r.lines, "\n", math.max(1, #r.lines - 5))) end next_video() end, } end next_video()