@@ -261,38 +261,7 @@ mp.add_key_binding(nil, "select-edition", function ()
261261 })
262262end )
263263
264- mp .add_key_binding (nil , " select-subtitle-line" , function ()
265- local sub = mp .get_property_native (" current-tracks/sub" )
266-
267- if sub == nil then
268- show_warning (" No subtitle is loaded." )
269- return
270- end
271-
272- if sub .external and sub [" external-filename" ]:find (" ^edl://" ) then
273- sub [" external-filename" ] = sub [" external-filename" ]:match (' https?://.*' )
274- or sub [" external-filename" ]
275- end
276-
277- local r = mp .command_native ({
278- name = " subprocess" ,
279- capture_stdout = true ,
280- args = sub .external
281- and {" ffmpeg" , " -loglevel" , " error" , " -i" , sub [" external-filename" ],
282- " -f" , " lrc" , " -map_metadata" , " -1" , " -fflags" , " +bitexact" , " -" }
283- or {" ffmpeg" , " -loglevel" , " error" , " -i" , mp .get_property (" path" ),
284- " -map" , " s:" .. sub [" id" ] - 1 , " -f" , " lrc" , " -map_metadata" ,
285- " -1" , " -fflags" , " +bitexact" , " -" }
286- })
287-
288- if r .error_string == " init" then
289- show_error (" Failed to extract subtitles: ffmpeg not found." )
290- return
291- elseif r .status ~= 0 then
292- show_error (" Failed to extract subtitles." )
293- return
294- end
295-
264+ local function select_subtitle_line (data )
296265 local sub_lines = {}
297266 local sub_times = {}
298267 local default_item
@@ -301,11 +270,14 @@ mp.add_key_binding(nil, "select-subtitle-line", function ()
301270 local duration = mp .get_property_native (" duration" , math.huge )
302271
303272 -- Strip HTML and ASS tags.
304- for line in r . stdout :gsub (" <.->" , " " ):gsub (" {\\ .-}" , " " ):gmatch (" [^\n ]+" ) do
273+ for line in data :gsub (" <.->" , " " ):gsub (" {\\ .-}" , " " ):gmatch (" [^\n ]+" ) do
305274 -- ffmpeg outputs LRCs with minutes > 60 instead of adding hours.
306- sub_times [# sub_times + 1 ] = line :match (" %d+" ) * 60 + line :match (" :([%d%.]*)" )
307- sub_lines [# sub_lines + 1 ] = format_time (sub_times [# sub_times ], duration ) ..
308- " " .. line :gsub (" .*]" , " " , 1 )
275+ local sub_line = line :gsub (" .*]" , " " , 1 )
276+ if sub_line ~= " " and sub_line :match (" ^%s+$" ) == nil then
277+ sub_times [# sub_times + 1 ] = line :match (" %d+" ) * 60 + line :match (" :([%d%.]*)" )
278+ sub_lines [# sub_lines + 1 ] = format_time (sub_times [# sub_times ], duration ) ..
279+ " " .. sub_line
280+ end
309281
310282 if sub_times [# sub_times ] <= time_pos then
311283 default_item = # sub_times
@@ -326,6 +298,54 @@ mp.add_key_binding(nil, "select-subtitle-line", function ()
326298 mp .commandv (" seek" , sub_times [index ] + delay , " absolute" )
327299 end ,
328300 })
301+ end
302+
303+ mp .add_key_binding (nil , " select-subtitle-line" , function ()
304+ local sub = mp .get_property_native (" current-tracks/sub" )
305+
306+ if sub == nil then
307+ show_warning (" No subtitle is loaded." )
308+ return
309+ end
310+
311+ if sub .external and sub [" external-filename" ]:find (" ^edl://" ) then
312+ sub [" external-filename" ] = sub [" external-filename" ]:match (' https?://.*' )
313+ or sub [" external-filename" ]
314+ end
315+
316+ if not sub .external or sub [" external-filename" ]:find (" ://" ) then
317+ local screenx , screeny = mp .get_osd_size ()
318+ local osd_align_x = mp .get_property (" osd-align-x" )
319+ mp .osd_message (" Subtitle extract in progress." , 3 )
320+ mp .set_osd_ass (screenx , screeny , string.format (" {\\ an%d}● " ,
321+ osd_align_x == " right" and 7 or 9 ))
322+ end
323+
324+ mp .command_native_async ({
325+ name = " subprocess" ,
326+ capture_stdout = true ,
327+ args = sub .external
328+ and {" ffmpeg" , " -loglevel" , " error" , " -i" , sub [" external-filename" ],
329+ " -f" , " lrc" , " -map_metadata" , " -1" , " -fflags" , " +bitexact" , " -" }
330+ or {" ffmpeg" , " -loglevel" , " error" , " -i" , mp .get_property (" path" ),
331+ " -map" , " s:" .. sub [" id" ] - 1 , " -f" , " lrc" , " -map_metadata" ,
332+ " -1" , " -fflags" , " +bitexact" , " -" }
333+ }, function (success , result )
334+ if not sub .external or sub [" external-filename" ]:find (" ://" ) then
335+ mp .osd_message (" " )
336+ mp .set_osd_ass (0 , 0 , " " )
337+ end
338+
339+ if result and result .error_string == " init" then
340+ show_error (" Failed to extract subtitles: ffmpeg not found." )
341+ return
342+ elseif not success or not result or result .status ~= 0 then
343+ show_error (" Failed to extract subtitles." )
344+ return
345+ end
346+
347+ select_subtitle_line (result .stdout )
348+ end )
329349end )
330350
331351mp .add_key_binding (nil , " select-audio-device" , function ()
0 commit comments