use async when making clips

This commit is contained in:
Ren Tatsumoto 2021-04-15 01:17:31 +03:00
parent f4d4ccc52c
commit 19b3f3fd74

View file

@ -117,14 +117,15 @@ local function construct_filename()
return filename return filename
end end
local function subprocess(args) local function subprocess_async(args, on_complete)
return mp.command_native { local command_table = {
name = "subprocess", name = "subprocess",
playback_only = false, playback_only = false,
capture_stdout = true, capture_stdout = true,
capture_stderr = true, capture_stderr = true,
args = args args = args
} }
return mp.command_native_async(command_table, on_complete)
end end
local function force_resolution(width, height, clip_fn, ...) local function force_resolution(width, height, clip_fn, ...)
@ -201,7 +202,7 @@ function encoder.append_embed_subs_args(args)
return args return args
end end
encoder.create_videoclip = function(clip_filename) encoder.mkargs_video = function(clip_filename)
local clip_path = utils.join_path(config.video_folder_path, clip_filename .. config.video_extension) local clip_path = utils.join_path(config.video_folder_path, clip_filename .. config.video_extension)
local args = { local args = {
'mpv', 'mpv',
@ -232,12 +233,12 @@ encoder.create_videoclip = function(clip_filename)
args = encoder.append_embed_subs_args(args) args = encoder.append_embed_subs_args(args)
end end
return subprocess(args) return args
end end
encoder.create_audioclip = function(clip_filename) encoder.mkargs_audio = function(clip_filename)
local clip_path = utils.join_path(config.audio_folder_path, clip_filename .. '.ogg') local clip_path = utils.join_path(config.audio_folder_path, clip_filename .. '.ogg')
return subprocess { return {
'mpv', 'mpv',
mp.get_property('path'), mp.get_property('path'),
'--loop-file=no', '--loop-file=no',
@ -273,22 +274,26 @@ encoder.create_clip = function(clip_type)
local clip_filename = construct_filename() local clip_filename = construct_filename()
mp.osd_message("Please wait...", 9999) mp.osd_message("Please wait...", 9999)
local ret local args
local location local location
if clip_type == 'video' then if clip_type == 'video' then
args = encoder.mkargs_video(clip_filename)
location = config.video_folder_path location = config.video_folder_path
ret = encoder.create_videoclip(clip_filename)
else else
args = encoder.mkargs_audio(clip_filename)
location = config.audio_folder_path location = config.audio_folder_path
ret = encoder.create_audioclip(clip_filename)
end end
if ret.status ~= 0 or string.match(ret.stdout, "could not open") then local process_result = function(_, ret, _)
mp.osd_message(string.format("Error: couldn't create the clip.\nDoes %s exist?", location), 5) if ret.status ~= 0 or string.match(ret.stdout, "could not open") then
else mp.osd_message(string.format("Error: couldn't create the clip.\nDoes %s exist?", location), 5)
mp.osd_message(string.format("Clip saved to %s.", location), 2) else
mp.osd_message(string.format("Clip saved to %s.", location), 2)
end
end end
subprocess_async(args, process_result)
main_menu.timings:reset() main_menu.timings:reset()
end end