Advertisement
Guest User

convert

a guest
Jul 26th, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. -- README:
  2. -- original version by Zehkul https://gist.github.com/Zehkul/25ea7ae77b30af959be0
  3. -- needs: libnotify, imagemagick (and ytdl if you want to encode streams)
  4. -- press any of alt + w, a, g, x, 5 or c to set the start and end frames
  5. -- press alt + q to stop and lose the current input
  6.  
  7. local mputils = require 'mp.utils'
  8. local msg = require "mp.msg"
  9.  
  10. -- Options
  11. webm_bitrate = 3*1024*8
  12. webm_bitsize = "K"
  13.  
  14. x26_quality = "18"
  15. x26_attachments = "" -- "-map 0" to inlcude attachments
  16.  
  17. gif_scale = "320"
  18. gif_fps = "24"
  19.  
  20. -- sets start and end frame of the portion to encode and calls the respective gui
  21. function set_timepos(call)
  22. if timepos1 then
  23. timepos2 = mp.get_property("time-pos")
  24.  
  25. if tonumber(timepos1) > tonumber(timepos2) then
  26. length = timepos1-timepos2
  27. start = timepos2
  28. elseif tonumber(timepos2) > tonumber(timepos1) then
  29. length = timepos2-timepos1
  30. start = timepos1
  31. else
  32. mp.osd_message("Both frames are the same, ignoring the second one", 3)
  33. timepos2 = nil
  34. return
  35. end
  36. timepos1 = nil
  37. wrap(call)
  38. else
  39. timepos1 = mp.get_property("time-pos")
  40. mp.osd_message("Start frame set")
  41. end
  42. end
  43.  
  44. function wrap(f)
  45. generate_filenames(e)
  46.  
  47. local full_command = f() .. ') && notify-send "Encode done!"'
  48.  
  49. mp.osd_message("Encode started")
  50. os.execute("(" .. full_command .. ") &")
  51. end
  52.  
  53. -- checks and generates input and output filenames
  54. function generate_filenames(ext)
  55. local filename_ext = mp.get_property_osd("filename")
  56. filename_ext = string.gsub(filename_ext, "'", "'\\''")
  57. title = string.gsub(filename_ext, "%....$","")
  58.  
  59. local dir, fil = mputils.split_path(mp.get_property("path", ""))
  60.  
  61. file_in = mp.get_property("stream-open-filename")
  62. file_in = string.gsub(file_in, "'", "'\\''")
  63.  
  64. out = os.date("-%d%m%y-%H:%M:%S.")
  65.  
  66. --check for ytdl-stream
  67. if string.sub(file_in,1,4) == "http" then
  68. local filename = mp.get_property("media-title")
  69. filename = string.gsub(filename, "/", "|")
  70.  
  71. file_out = os.getenv("HOME") .. filename .. out .. ext
  72. file_out = string.gsub(file_out, "'", "'\\''")
  73. else
  74. file_out = dir .. title .. out .. ext
  75. end
  76. end
  77.  
  78. function encode_webm ()
  79. bitrate = math.floor(webm_bitrate/length*0.975)
  80.  
  81. 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'
  82. ..
  83. ' && 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'
  84. end
  85.  
  86. function encode_gif ()
  87. 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 .. '\''
  88. end
  89.  
  90. function encode_x264 ()
  91. 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'
  92. end
  93.  
  94. function encode_x265 ()
  95. 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'
  96. end
  97.  
  98. function encode_copy ()
  99. return '(ffmpeg -ss ' .. start .. ' -i \'' .. file_in .. '\' -t ' .. length .. ' -c:v copy -c:a copy -c:s copy \'' .. file_out .. '\' -y'
  100. end
  101.  
  102. mp.add_key_binding("alt+w", "cs_webm-na",
  103. function() audio = "-an" e="webm" set_timepos(encode_webm) end)
  104. mp.add_key_binding("alt+a", "cs_webm-a" ,
  105. function() audio = "" e="webm" set_timepos(encode_webm) end)
  106. mp.add_key_binding("alt+g", "cs_gif" ,
  107. function() e="gif" set_timepos(encode_gif) end)
  108. mp.add_key_binding("alt+x", "cs_x264" ,
  109. function() e="mkv" set_timepos(encode_x264) end)
  110. mp.add_key_binding("alt+5", "cs_x265" ,
  111. function() e="mkv" set_timepos(encode_x265) end)
  112. mp.add_key_binding("alt+c", "cs_copy" ,
  113. function() e="mkv" set_timepos(encode_copy) end)
  114. mp.add_key_binding("alt+q", "cs_skip" ,
  115. function() set_timepos(function() mp.osd_message("skipping") end) end)
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement