Skip to content

Commit 653a28c

Browse files
committed
select.lua: asynchronous execution of ffmpeg subprocesses
Avoid thread cloging
1 parent df1e71a commit 653a28c

1 file changed

Lines changed: 50 additions & 33 deletions

File tree

player/lua/select.lua

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -254,38 +254,7 @@ mp.add_key_binding(nil, "select-edition", function ()
254254
})
255255
end)
256256

257-
mp.add_key_binding(nil, "select-subtitle-line", function ()
258-
local sub = mp.get_property_native("current-tracks/sub")
259-
260-
if sub == nil then
261-
show_warning("No subtitle is loaded.")
262-
return
263-
end
264-
265-
if sub.external and sub["external-filename"]:find("^edl://") then
266-
sub["external-filename"] = sub["external-filename"]:match('https?://.*')
267-
or sub["external-filename"]
268-
end
269-
270-
local r = mp.command_native({
271-
name = "subprocess",
272-
capture_stdout = true,
273-
args = sub.external
274-
and {"ffmpeg", "-loglevel", "error", "-i", sub["external-filename"],
275-
"-f", "lrc", "-map_metadata", "-1", "-fflags", "+bitexact", "-"}
276-
or {"ffmpeg", "-loglevel", "error", "-i", mp.get_property("path"),
277-
"-map", "s:" .. sub["id"] - 1, "-f", "lrc", "-map_metadata",
278-
"-1", "-fflags", "+bitexact", "-"}
279-
})
280-
281-
if r.error_string == "init" then
282-
show_error("Failed to extract subtitles: ffmpeg not found.")
283-
return
284-
elseif r.status ~= 0 then
285-
show_error("Failed to extract subtitles.")
286-
return
287-
end
288-
257+
local function select_subtitle_line(data)
289258
local sub_lines = {}
290259
local sub_times = {}
291260
local default_item
@@ -294,7 +263,7 @@ mp.add_key_binding(nil, "select-subtitle-line", function ()
294263
local duration = mp.get_property_native("duration", math.huge)
295264

296265
-- Strip HTML and ASS tags.
297-
for line in r.stdout:gsub("<.->", ""):gsub("{\\.-}", ""):gmatch("[^\n]+") do
266+
for line in data:gsub("<.->", ""):gsub("{\\.-}", ""):gmatch("[^\n]+") do
298267
-- ffmpeg outputs LRCs with minutes > 60 instead of adding hours.
299268
sub_times[#sub_times + 1] = line:match("%d+") * 60 + line:match(":([%d%.]*)")
300269
sub_lines[#sub_lines + 1] = format_time(sub_times[#sub_times], duration) ..
@@ -319,6 +288,54 @@ mp.add_key_binding(nil, "select-subtitle-line", function ()
319288
mp.commandv("seek", sub_times[index] + delay, "absolute")
320289
end,
321290
})
291+
end
292+
293+
mp.add_key_binding(nil, "select-subtitle-line", function ()
294+
local sub = mp.get_property_native("current-tracks/sub")
295+
296+
if sub == nil then
297+
show_warning("No subtitle is loaded.")
298+
return
299+
end
300+
301+
if sub.external and sub["external-filename"]:find("^edl://") then
302+
sub["external-filename"] = sub["external-filename"]:match('https?://.*')
303+
or sub["external-filename"]
304+
end
305+
306+
if not sub.external or sub["external-filename"]:find("://") then
307+
local screenx, screeny = mp.get_osd_size()
308+
local osd_align_x = mp.get_property("osd-align-x")
309+
mp.osd_message("Subtitle export in progress.", 3)
310+
mp.set_osd_ass(screenx, screeny, string.format("{\\an%d}● ",
311+
osd_align_x == "right" and 7 or 9))
312+
end
313+
314+
mp.command_native_async({
315+
name = "subprocess",
316+
capture_stdout = true,
317+
args = sub.external
318+
and {"ffmpeg", "-loglevel", "error", "-i", sub["external-filename"],
319+
"-f", "lrc", "-map_metadata", "-1", "-fflags", "+bitexact", "-"}
320+
or {"ffmpeg", "-loglevel", "error", "-i", mp.get_property("path"),
321+
"-map", "s:" .. sub["id"] - 1, "-f", "lrc", "-map_metadata",
322+
"-1", "-fflags", "+bitexact", "-"}
323+
}, function (success, result)
324+
if not sub.external or sub["external-filename"]:find("://") then
325+
mp.osd_message("")
326+
mp.set_osd_ass(0, 0, "")
327+
end
328+
329+
if result and result.error_string == "init" then
330+
show_error("Failed to extract subtitles: ffmpeg not found.")
331+
return
332+
elseif not success or not result or result.status ~= 0 then
333+
show_error("Failed to extract subtitles.")
334+
return
335+
end
336+
337+
select_subtitle_line(result.stdout)
338+
end)
322339
end)
323340

324341
mp.add_key_binding(nil, "select-audio-device", function ()

0 commit comments

Comments
 (0)