Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- README:
- -- original version by Zehkul https://gist.github.com/Zehkul/25ea7ae77b30af959be0
- -- needs: libnotify, imagemagick (and ytdl if you want to encode streams)
- -- press any of alt + w, a, g, x, 5 or c to set the start and end frames
- -- press alt + q to stop and lose the current input
- local mputils = require 'mp.utils'
- local msg = require "mp.msg"
- -- Options
- webm_bitrate = 3*1024*8
- webm_bitsize = "K"
- x26_quality = "18"
- x26_attachments = "" -- "-map 0" to inlcude attachments
- gif_scale = "320"
- gif_fps = "24"
- -- sets start and end frame of the portion to encode and calls the respective gui
- function set_timepos(call)
- if timepos1 then
- timepos2 = mp.get_property("time-pos")
- if tonumber(timepos1) > tonumber(timepos2) then
- length = timepos1-timepos2
- start = timepos2
- elseif tonumber(timepos2) > tonumber(timepos1) then
- length = timepos2-timepos1
- start = timepos1
- else
- mp.osd_message("Both frames are the same, ignoring the second one", 3)
- timepos2 = nil
- return
- end
- timepos1 = nil
- wrap(call)
- else
- timepos1 = mp.get_property("time-pos")
- mp.osd_message("Start frame set")
- end
- end
- function wrap(f)
- generate_filenames(e)
- local full_command = f() .. ') && notify-send "Encode done!"'
- mp.osd_message("Encode started")
- os.execute("(" .. full_command .. ") &")
- end
- -- checks and generates input and output filenames
- function generate_filenames(ext)
- local filename_ext = mp.get_property_osd("filename")
- filename_ext = string.gsub(filename_ext, "'", "'\\''")
- title = string.gsub(filename_ext, "%....$","")
- local dir, fil = mputils.split_path(mp.get_property("path", ""))
- file_in = mp.get_property("stream-open-filename")
- file_in = string.gsub(file_in, "'", "'\\''")
- out = os.date("-%d%m%y-%H:%M:%S.")
- --check for ytdl-stream
- if string.sub(file_in,1,4) == "http" then
- local filename = mp.get_property("media-title")
- filename = string.gsub(filename, "/", "|")
- file_out = os.getenv("HOME") .. filename .. out .. ext
- file_out = string.gsub(file_out, "'", "'\\''")
- else
- file_out = dir .. title .. out .. ext
- end
- end
- function encode_webm ()
- bitrate = math.floor(webm_bitrate/length*0.975)
- return '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -c:v libvpx -c:a libvorbis -sn -t ' .. length .. ' -b:v ' .. bitrate .. webm_bitsize .. ' -vf scale=-1:-1 -metadata title="' .. title .. '" -quality good -cpu-used 0 -pass 1 ' .. audio .. ' -f webm /dev/null -y'
- ..
- ' && ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -c:v libvpx -c:a libvorbis -sn -t ' .. length .. ' -b:v ' .. bitrate .. webm_bitsize .. ' -vf scale=-1:-1 -metadata title="' .. title .. '" -quality good -cpu-used 0 -pass 2 ' .. audio .. ' -f webm \'' .. file_out .. '\' -y && rm ffmpeg2pass*.log'
- end
- function encode_gif ()
- return '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -vf scale=' .. gif_scale .. ':-1 -r ' .. gif_fps .. ' -f image2pipe -vcodec ppm - | convert -delay 1x' .. gif_fps .. ' -loop 0 - gif:- | convert -layers Optimize - \'' .. file_out .. '\''
- end
- function encode_x264 ()
- return '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -c:v libx264 -vf scale=-1:-1 -preset slower -qp ' .. x26_quality .. ' -c:a copy -c:s copy ' .. x26_attachments .. ' \'' .. file_out .. '\' -y'
- end
- function encode_x265 ()
- return '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -c:v libx265 -vf scale=-1:-1 -preset slower -x265-params qp=' .. x26_quality .. ',D=10 -strict experimental -c:a copy -c:s copy ' .. x26_attachments .. ' \'' .. file_out .. '\' -y'
- end
- function encode_copy ()
- return '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -c:v copy -c:a copy -c:s copy \'' .. file_out .. '\' -y'
- end
- mp.add_key_binding("alt+w", "cs_webm-na",
- function() audio = "-an" e="webm" set_timepos(encode_webm) end)
- mp.add_key_binding("alt+a", "cs_webm-a" ,
- function() audio = "" e="webm" set_timepos(encode_webm) end)
- mp.add_key_binding("alt+g", "cs_gif" ,
- function() e="gif" set_timepos(encode_gif) end)
- mp.add_key_binding("alt+x", "cs_x264" ,
- function() e="mkv" set_timepos(encode_x264) end)
- mp.add_key_binding("alt+5", "cs_x265" ,
- function() e="mkv" set_timepos(encode_x265) end)
- mp.add_key_binding("alt+c", "cs_copy" ,
- function() e="mkv" set_timepos(encode_copy) end)
- mp.add_key_binding("alt+q", "cs_skip" ,
- function() set_timepos(function() mp.osd_message("skipping") end) end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement