videoclip/videoclip.lua

545 lines
16 KiB
Lua
Raw Normal View History

2021-04-14 15:30:40 +00:00
local mp = require('mp')
2020-08-21 04:19:06 +00:00
local mpopt = require('mp.options')
2020-10-02 08:27:40 +00:00
local utils = require('mp.utils')
2020-08-21 04:19:06 +00:00
-- Options can be changed here or in a separate config file.
-- Config path: ~/.config/mpv/script-opts/videoclip.conf
local config = {
-- absolute paths
2020-09-19 23:47:10 +00:00
-- relative paths (e.g. ~ for home dir) do NOT work.
2020-10-02 07:32:25 +00:00
video_folder_path = string.format('%s/Videos/', os.getenv("HOME") or os.getenv('USERPROFILE')),
audio_folder_path = string.format('%s/Music/', os.getenv("HOME") or os.getenv('USERPROFILE')),
2020-08-21 04:19:06 +00:00
-- The range of the CRF scale is 051, where 0 is lossless,
-- 23 is the default, and 51 is worst quality possible.
2020-09-20 00:43:41 +00:00
-- Insane values like 9999 still work but produce the worst quality.
2020-08-21 04:19:06 +00:00
video_quality = 23,
-- Use the slowest preset that you have patience for.
-- https://trac.ffmpeg.org/wiki/Encode/H.264
preset = 'faster',
2020-10-23 15:34:24 +00:00
video_format = 'mp4', -- mp4, vp9, vp8
2020-10-23 15:34:45 +00:00
video_bitrate = '1M',
2020-08-21 04:19:06 +00:00
video_width = -2,
video_height = 480,
2020-10-23 15:34:24 +00:00
audio_bitrate = '32k',
mute_audio = false,
2020-10-23 15:34:24 +00:00
font_size = 24,
2021-04-14 20:08:12 +00:00
embed_subs = true,
2020-08-21 04:19:06 +00:00
}
mpopt.read_options(config, 'videoclip')
2020-10-23 13:34:23 +00:00
local main_menu
2020-10-23 13:33:10 +00:00
local pref_menu
local encoder
local OSD
local Timings
2020-08-21 04:19:06 +00:00
2020-09-24 18:16:14 +00:00
local allowed_presets = {
ultrafast = true,
superfast = true,
veryfast = true,
faster = true,
fast = true,
medium = true,
slow = true,
slower = true,
veryslow = true,
}
2020-08-21 04:19:06 +00:00
------------------------------------------------------------
2020-10-23 15:34:24 +00:00
-- Utility functions
2020-08-21 04:19:06 +00:00
local function remove_extension(filename)
2020-09-19 23:47:10 +00:00
return filename:gsub('%.%w+$', '')
2020-08-21 04:19:06 +00:00
end
local function remove_text_in_brackets(str)
2020-09-19 23:47:10 +00:00
return str:gsub('%b[]', '')
2020-08-21 04:19:06 +00:00
end
local function remove_special_characters(str)
2020-09-19 23:47:10 +00:00
return str:gsub('[%c%p%s]', '')
2020-08-21 04:19:06 +00:00
end
local function human_readable_time(seconds)
if type(seconds) ~= 'number' or seconds < 0 then
2020-08-21 04:19:06 +00:00
return 'empty'
end
local parts = {}
2020-08-21 04:19:06 +00:00
2020-09-19 23:47:10 +00:00
parts.h = math.floor(seconds / 3600)
parts.m = math.floor(seconds / 60) % 60
parts.s = math.floor(seconds % 60)
parts.ms = math.floor((seconds * 1000) % 1000)
local ret = string.format("%02dm%02ds%03dms", parts.m, parts.s, parts.ms)
if parts.h > 0 then
ret = string.format('%dh%s', parts.h, ret)
2020-08-21 04:19:06 +00:00
end
return ret
2020-08-21 04:19:06 +00:00
end
local function construct_filename()
2020-08-21 04:19:06 +00:00
local filename = mp.get_property("filename") -- filename without path
filename = remove_extension(filename)
filename = remove_text_in_brackets(filename)
filename = remove_special_characters(filename)
filename = string.format(
2020-09-13 20:02:45 +00:00
'%s_(%s-%s)',
filename,
2020-10-23 13:34:23 +00:00
human_readable_time(main_menu.timings['start']),
human_readable_time(main_menu.timings['end'])
2020-08-21 04:19:06 +00:00
)
return filename
end
local function subprocess(args)
return mp.command_native {
2020-08-27 03:29:23 +00:00
name = "subprocess",
playback_only = false,
capture_stdout = true,
capture_stderr = true,
2020-08-27 03:29:23 +00:00
args = args
}
2020-08-21 04:19:06 +00:00
end
local function force_resolution(width, height, clip_fn, ...)
local cached_prefs = {
video_width = config.video_width,
video_height = config.video_height,
}
config.video_width = width
config.video_height = height
clip_fn(...)
config.video_width = cached_prefs.video_width
config.video_height = cached_prefs.video_height
end
local function set_video_settings()
if config.video_format == 'mp4' then
config.video_codec = 'libx264'
config.video_extension = '.mp4'
2020-10-23 15:34:24 +00:00
elseif config.video_format == 'vp9' then
config.video_codec = 'libvpx-vp9'
config.video_extension = '.webm'
2020-10-23 15:34:24 +00:00
else
config.video_codec = 'libvpx'
config.video_extension = '.webm'
end
end
2020-10-24 09:02:32 +00:00
local function validate_config()
if not config.audio_bitrate:match('^%d+[kK]$') then
config.audio_bitrate = (tonumber(config.audio_bitrate) or 32) .. 'k'
end
if not config.video_bitrate:match('^%d+[kKmM]$') then
config.video_bitrate = '1M'
end
if not allowed_presets[config.preset] then
config.preset = 'faster'
end
set_video_settings()
end
------------------------------------------------------------
2020-10-23 15:34:24 +00:00
-- Provides interface for creating audio/video clips
encoder = {}
2021-04-14 20:08:12 +00:00
function encoder.get_active_track(track_type)
local track_list = mp.get_property_native('track-list')
for _, track in pairs(track_list) do
if track.type == track_type and track.selected == true then
return track
end
end
return nil
end
function encoder.get_ext_subs_fp()
local track = encoder.get_active_track('sub')
if track and track.external == true then
return track['external-filename']
end
end
function encoder.append_embed_subs_args(args)
local ext_subs_fp = encoder.get_ext_subs_fp()
if ext_subs_fp then
table.insert(args, #args, table.concat { '--sub-files-append=', ext_subs_fp })
end
table.insert(args, #args, table.concat { '--sid=', mp.get_property("sid") })
table.insert(args, #args, table.concat { '--sub-delay=', mp.get_property("sub-delay") })
return args
end
2020-10-23 13:26:07 +00:00
encoder.create_videoclip = function(clip_filename)
2020-10-23 13:22:43 +00:00
local clip_path = utils.join_path(config.video_folder_path, clip_filename .. config.video_extension)
2021-04-14 20:08:12 +00:00
local args = {
'mpv',
mp.get_property('path'),
2020-09-26 22:35:36 +00:00
'--loop-file=no',
'--no-ocopy-metadata',
'--no-sub',
'--audio-channels=2',
'--oac=libopus',
'--oacopts-add=vbr=on',
'--oacopts-add=application=voip',
'--oacopts-add=compression_level=10',
2020-10-23 13:22:43 +00:00
table.concat { '--ovc=', config.video_codec },
2020-10-23 13:34:23 +00:00
table.concat { '--start=', main_menu.timings['start'] },
table.concat { '--end=', main_menu.timings['end'] },
2020-10-23 13:26:07 +00:00
table.concat { '--aid=', config.mute_audio and 'no' or mp.get_property("aid") }, -- track number
2020-10-16 01:42:55 +00:00
table.concat { '--volume=', mp.get_property('volume') },
2020-10-23 15:34:45 +00:00
table.concat { '--ovcopts-add=b=', config.video_bitrate },
table.concat { '--oacopts-add=b=', config.audio_bitrate },
table.concat { '--ovcopts-add=crf=', config.video_quality },
table.concat { '--ovcopts-add=preset=', config.preset },
table.concat { '--vf-add=scale=', config.video_width, ':', config.video_height },
2021-04-14 16:11:15 +00:00
table.concat { '--ytdl-format=', mp.get_property("ytdl-format") },
table.concat { '-o=', clip_path }
2020-08-21 04:19:06 +00:00
}
2021-04-14 20:08:12 +00:00
if config.embed_subs == true then
args = encoder.append_embed_subs_args(args)
end
return subprocess(args)
2020-08-21 04:19:06 +00:00
end
encoder.create_audioclip = function(clip_filename)
2020-10-02 08:27:40 +00:00
local clip_path = utils.join_path(config.audio_folder_path, clip_filename .. '.ogg')
return subprocess {
'mpv',
mp.get_property('path'),
'--loop-file=no',
'--no-ocopy-metadata',
'--no-sub',
'--audio-channels=2',
2020-09-26 22:35:36 +00:00
'--video=no',
'--oac=libopus',
'--oacopts-add=vbr=on',
'--oacopts-add=application=voip',
'--oacopts-add=compression_level=10',
2020-10-23 13:34:23 +00:00
table.concat { '--start=', main_menu.timings['start'] },
table.concat { '--end=', main_menu.timings['end'] },
2020-10-16 01:42:55 +00:00
table.concat { '--volume=', mp.get_property('volume') },
table.concat { '--aid=', mp.get_property("aid") }, -- track number
table.concat { '--oacopts-add=b=', config.audio_bitrate },
2021-04-14 16:11:15 +00:00
table.concat { '--ytdl-format=', mp.get_property("ytdl-format") },
table.concat { '-o=', clip_path }
2020-08-22 00:44:37 +00:00
}
end
encoder.create_clip = function(clip_type)
2020-10-23 13:34:23 +00:00
main_menu:close();
if clip_type == nil then
return
end
2020-08-22 00:44:37 +00:00
2020-10-23 13:34:23 +00:00
if not main_menu.timings:validate() then
2020-08-22 00:44:37 +00:00
mp.osd_message("Wrong timings. Aborting.", 2)
return
end
local clip_filename = construct_filename()
2020-08-27 03:29:23 +00:00
mp.osd_message("Please wait...", 9999)
local ret
local location
2020-08-27 03:29:23 +00:00
2020-10-23 13:26:07 +00:00
if clip_type == 'video' then
location = config.video_folder_path
2020-10-23 13:26:07 +00:00
ret = encoder.create_videoclip(clip_filename)
2020-10-23 07:26:32 +00:00
else
location = config.audio_folder_path
ret = encoder.create_audioclip(clip_filename)
2020-08-27 03:29:23 +00:00
end
2020-09-20 01:11:33 +00:00
if ret.status ~= 0 or string.match(ret.stdout, "could not open") then
mp.osd_message(string.format("Error: couldn't create the clip.\nDoes %s exist?", location), 5)
2020-09-20 00:21:39 +00:00
else
mp.osd_message(string.format("Clip saved to %s.", location), 2)
2020-08-22 00:44:37 +00:00
end
2020-10-23 13:34:23 +00:00
main_menu.timings:reset()
2020-08-22 00:44:37 +00:00
end
------------------------------------------------------------
-- Menu interface
local Menu = {}
Menu.__index = Menu
function Menu:new(parent)
local o = {
parent = parent,
overlay = parent and parent.overlay or mp.create_osd_overlay('ass-events'),
2020-10-23 13:39:53 +00:00
keybindings = { },
}
return setmetatable(o, self)
end
function Menu:overlay_draw(text)
self.overlay.data = text
self.overlay:update()
end
function Menu:open()
if self.parent then
self.parent:close()
end
2020-10-23 13:39:53 +00:00
for _, val in pairs(self.keybindings) do
2020-10-23 13:40:09 +00:00
mp.add_forced_key_binding(val.key, val.key, val.fn)
end
self:update()
end
function Menu:close()
2020-10-23 13:39:53 +00:00
for _, val in pairs(self.keybindings) do
mp.remove_key_binding(val.key)
end
if self.parent then
self.parent:open()
else
self.overlay:remove()
end
end
function Menu:update()
local osd = OSD:new():size(config.font_size):align(4)
osd:append('Dummy menu.'):newline()
self:overlay_draw(osd:get_text())
end
2020-08-21 04:19:06 +00:00
------------------------------------------------------------
2020-10-23 15:34:24 +00:00
-- Main menu
2020-08-21 04:19:06 +00:00
2020-10-23 13:34:23 +00:00
main_menu = Menu:new()
2020-09-20 00:01:38 +00:00
2020-10-23 13:39:53 +00:00
main_menu.keybindings = {
2020-10-23 13:34:23 +00:00
{ key = 's', fn = function() main_menu:set_time('start') end },
{ key = 'e', fn = function() main_menu:set_time('end') end },
{ key = 'S', fn = function() main_menu:set_time_sub('start') end },
{ key = 'E', fn = function() main_menu:set_time_sub('end') end },
{ key = 'r', fn = function() main_menu:reset_timings() end },
2020-09-28 10:40:07 +00:00
{ key = 'c', fn = function() encoder.create_clip('video') end },
2020-10-23 13:32:33 +00:00
{ key = 'C', fn = function() force_resolution(1920, -2, encoder.create_clip, 'video') end },
2020-09-28 10:40:07 +00:00
{ key = 'a', fn = function() encoder.create_clip('audio') end },
2020-10-23 13:32:33 +00:00
{ key = 'p', fn = function() pref_menu:open() end },
2020-08-21 04:19:06 +00:00
{ key = 'o', fn = function() mp.commandv('run', 'xdg-open', 'https://streamable.com/') end },
2020-10-23 13:34:23 +00:00
{ key = 'ESC', fn = function() main_menu:close() end },
2020-08-21 04:19:06 +00:00
}
2020-10-23 13:34:23 +00:00
function main_menu:set_time(property)
2020-10-23 13:32:33 +00:00
self.timings[property] = mp.get_property_number('time-pos')
self:update()
2020-08-21 04:19:06 +00:00
end
2020-10-23 13:34:23 +00:00
function main_menu:set_time_sub(property)
2020-08-21 04:19:06 +00:00
local sub_delay = mp.get_property_native("sub-delay")
local time_pos = mp.get_property_number(string.format("sub-%s", property))
if time_pos == nil then
2020-09-06 16:01:32 +00:00
mp.osd_message("Warning: No subtitles visible.", 2)
2020-08-21 04:19:06 +00:00
return
end
2020-10-23 13:32:33 +00:00
self.timings[property] = time_pos + sub_delay
self:update()
2020-08-21 04:19:06 +00:00
end
2020-10-23 13:34:23 +00:00
function main_menu:reset_timings()
2020-10-23 13:32:33 +00:00
self.timings = Timings:new()
self:update()
2020-08-21 04:19:06 +00:00
end
2020-10-23 13:34:23 +00:00
main_menu.open = function()
main_menu.timings = main_menu.timings or Timings:new()
Menu.open(main_menu)
2020-08-21 04:19:06 +00:00
end
2020-10-23 13:34:23 +00:00
function main_menu:update()
2020-10-23 13:32:33 +00:00
local osd = OSD:new():size(config.font_size):align(4)
osd:bold('Clip creator'):newline()
osd:tab():bold('Start time: '):append(human_readable_time(self.timings['start'])):newline()
osd:tab():bold('End time: '):append(human_readable_time(self.timings['end'])):newline()
osd:bold('Timings: '):italics('(+shift use sub timings)'):newline()
osd:tab():bold('s: '):append('Set start'):newline()
osd:tab():bold('e: '):append('Set end'):newline()
osd:tab():bold('r: '):append('Reset'):newline()
osd:bold('Create clip: '):italics('(+shift to force fullHD preset)'):newline()
osd:tab():bold('c: '):append('video clip'):newline()
osd:tab():bold('a: '):append('audio clip'):newline()
osd:bold('Options: '):newline()
osd:tab():bold('p: '):append('Open preferences'):newline()
osd:tab():bold('o: '):append('Open streamable.com'):newline()
osd:tab():bold('ESC: '):append('Close'):newline()
self:overlay_draw(osd:get_text())
2020-08-21 04:19:06 +00:00
end
2020-10-23 13:33:10 +00:00
------------------------------------------------------------
-- Preferences
pref_menu = Menu:new(main_menu)
2020-10-23 13:39:53 +00:00
pref_menu.keybindings = {
2020-10-23 15:34:24 +00:00
{ key = 'f', fn = function() pref_menu:cycle_video_formats() end },
2020-10-23 13:33:10 +00:00
{ key = 'm', fn = function() pref_menu:toggle_mute_audio() end },
2020-10-23 15:34:24 +00:00
{ key = 'r', fn = function() pref_menu:cycle_resolutions() end },
2021-04-14 20:16:44 +00:00
{ key = 'e', fn = function() pref_menu:toggle_embed_subtitles() end },
2020-10-23 13:33:10 +00:00
{ key = 'ESC', fn = function() pref_menu:close() end },
}
2020-10-23 15:34:24 +00:00
pref_menu.resolutions = {
{ w = config.video_width, h = config.video_height, },
{ w = -2, h = 240, },
{ w = -2, h = 360, },
{ w = -2, h = 480, },
{ w = -2, h = 720, },
{ w = -2, h = 1080, },
{ w = -2, h = 1440, },
{ w = -2, h = 2160, },
selected = 1,
}
2020-10-23 13:33:10 +00:00
2020-10-23 15:34:24 +00:00
pref_menu.formats = { 'mp4', 'vp9', 'vp8' }
function pref_menu:get_selected_resolution()
local w = config.video_width
local h = config.video_height
2021-04-14 15:30:55 +00:00
w = w == -2 and 'auto' or w
h = h == -2 and 'auto' or h
2020-10-23 15:34:24 +00:00
return string.format('%s x %s', w, h)
end
function pref_menu:cycle_resolutions()
self.resolutions.selected = self.resolutions.selected + 1 > #self.resolutions and 1 or self.resolutions.selected + 1
local res = self.resolutions[self.resolutions.selected]
config.video_width = res.w
config.video_height = res.h
self:update()
2020-10-23 13:33:10 +00:00
end
2020-10-23 15:34:24 +00:00
function pref_menu:cycle_video_formats()
local selected = 1
for i, v in ipairs(pref_menu.formats) do
if config.video_format == v then
selected = i
end
end
config.video_format = pref_menu.formats[selected + 1] or pref_menu.formats[1]
2020-10-23 13:33:10 +00:00
set_video_settings()
self:update()
end
function pref_menu:toggle_mute_audio()
config.mute_audio = not config.mute_audio
self:update()
end
2021-04-14 20:16:44 +00:00
function pref_menu:toggle_embed_subtitles()
config.embed_subs = not config.embed_subs
self:update()
end
2020-10-23 15:34:24 +00:00
function pref_menu:update()
local osd = OSD:new():size(config.font_size):align(4)
osd:bold('Preferences'):newline()
osd:bold('Video resolution: '):append(self:get_selected_resolution()):newline()
osd:bold('Video format: '):append(config.video_format):newline()
osd:bold('Mute audio: '):append(config.mute_audio and 'yes' or 'no'):newline()
2021-04-14 20:16:44 +00:00
osd:bold('Embed subtitles: '):append(config.embed_subs and 'yes' or 'no'):newline()
2020-10-23 15:34:24 +00:00
osd:newline()
osd:bold('Bindings:'):newline()
osd:tab():bold('r: '):append('Cycle video resolutions'):newline()
osd:tab():bold('f: '):append('Cycle video formats'):newline()
osd:tab():bold('m: '):append('Toggle mute audio'):newline()
2021-04-14 20:16:44 +00:00
osd:tab():bold('e: '):append('Toggle embed subtitles'):newline()
2020-10-23 15:34:24 +00:00
self:overlay_draw(osd:get_text())
end
2020-08-21 04:19:06 +00:00
------------------------------------------------------------
-- Helper class for styling OSD messages
2020-09-06 15:51:52 +00:00
-- http://docs.aegisub.org/3.2/ASS_Tags/
2020-08-21 04:19:06 +00:00
OSD = {}
OSD.__index = OSD
function OSD:new()
return setmetatable({ text = {} }, self)
2020-08-21 04:19:06 +00:00
end
function OSD:append(s)
table.insert(self.text, s)
2020-08-21 04:19:06 +00:00
return self
end
function OSD:bold(s)
2020-09-20 00:02:51 +00:00
return self:append(string.format([[{\b1}%s{\b0}]], s))
2020-08-21 04:19:06 +00:00
end
2020-09-27 23:54:17 +00:00
function OSD:italics(s)
return self:append('{\\i1}'):append(s):append('{\\i0}')
end
2020-08-21 04:19:06 +00:00
function OSD:newline()
2020-09-20 00:02:51 +00:00
return self:append([[\N]])
2020-08-21 04:19:06 +00:00
end
function OSD:tab()
2020-09-20 00:02:51 +00:00
return self:append([[\h\h\h\h]])
2020-08-21 04:19:06 +00:00
end
function OSD:size(size)
2020-09-20 00:02:51 +00:00
return self:append(string.format([[{\fs%s}]], size))
2020-08-21 04:19:06 +00:00
end
2020-09-06 15:51:36 +00:00
function OSD:align(number)
2020-09-20 00:02:51 +00:00
return self:append(string.format([[{\an%s}]], number))
2020-08-21 04:19:06 +00:00
end
function OSD:get_text()
return table.concat(self.text)
end
2020-08-21 04:19:06 +00:00
------------------------------------------------------------
-- Timings class
Timings = {
['start'] = -1,
['end'] = -1,
}
function Timings:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end
function Timings:reset()
self['start'] = -1
2020-09-19 23:47:10 +00:00
self['end'] = -1
2020-08-21 04:19:06 +00:00
end
function Timings:validate()
return self['start'] >= 0 and self['start'] < self['end']
2020-08-21 04:19:06 +00:00
end
------------------------------------------------------------
-- Finally, set an 'entry point' in mpv
2020-10-24 09:02:32 +00:00
validate_config()
2020-10-23 13:34:23 +00:00
mp.add_key_binding('c', 'videoclip-menu-open', main_menu.open)