@@ -261,23 +261,93 @@ mp.add_key_binding(nil, "select-edition", function ()
261261 })
262262end )
263263
264- local function select_subtitle_line (data )
264+ local function select_subtitle_line (data , codec )
265265 local sub_lines = {}
266266 local sub_times = {}
267267 local default_item
268268 local delay = mp .get_property_native (" sub-delay" )
269269 local time_pos = mp .get_property_native (" time-pos" ) - delay
270270 local duration = mp .get_property_native (" duration" , math.huge )
271+ local sub_content = {}
272+
273+ -- Strip HTML and ASS tags and process subtitles
274+ for line in data :gmatch (" [^\n ]+" ) do
275+ -- Clean up tags
276+ local sub_line = line :gsub (" <.->" , " " ) -- Strip HTML tags
277+ :gsub (" \\ h+" , " " ) -- Replace '\h' tag
278+ :gsub (" {[\\ =].-}" , " " ) -- Remove ASS formatting
279+ :gsub (" .-]" , " " , 1 ) -- Remove time info prefix
280+ :gsub (" ^%s*(.-)%s*$" , " %1" ) -- Strip whitespace
281+ :gsub (" ^m%s[mbl%s%-%d%.]+$" , " " ) -- Remove graphics code
282+
283+ if codec == " subrip" or (sub_line ~= " " and sub_line :match (" ^%s+$" ) == nil ) then
284+ local sub_time = line :match (" %d+" ) * 60 + line :match (" :([%d%.]*)" )
285+ local time_seconds = math.floor (sub_time )
286+
287+ -- Use a dictionary to store the timestamp and content
288+ if not sub_content [time_seconds ] then
289+ -- Initialize content list for that timestamp
290+ sub_content [time_seconds ] = {}
291+ end
292+
293+ -- If the subtitle content is not already in the list for that timestamp, add it
294+ local content_exists = false
295+ for _ , content in ipairs (sub_content [time_seconds ]) do
296+ if content == sub_line then
297+ content_exists = true
298+ break
299+ end
300+ end
301+
302+ if not content_exists then
303+ sub_content [time_seconds ][# sub_content [time_seconds ] + 1 ] = sub_line
304+ end
305+ end
306+ end
271307
272- -- Strip HTML and ASS tags.
273- for line in data :gsub (" <.->" , " " ):gsub (" {\\ .-}" , " " ):gmatch (" [^\n ]+" ) do
274- -- ffmpeg outputs LRCs with minutes > 60 instead of adding hours.
275- sub_times [# sub_times + 1 ] = line :match (" %d+" ) * 60 + line :match (" :([%d%.]*)" )
276- sub_lines [# sub_lines + 1 ] = format_time (sub_times [# sub_times ], duration ) ..
277- " " .. line :gsub (" .*]" , " " , 1 )
308+ -- Process all timestamps and content into selectable subtitle list
309+ for time_seconds , contents in pairs (sub_content ) do
310+ for _ , sub_line in ipairs (contents ) do
311+ sub_times [# sub_times + 1 ] = time_seconds
312+ sub_lines [# sub_lines + 1 ] = format_time (time_seconds , duration ) .. " " .. sub_line
313+ end
314+ end
278315
279- if sub_times [# sub_times ] <= time_pos then
280- default_item = # sub_times
316+ -- Sort by timestamp
317+ local function compare_times (a , b )
318+ return a < b
319+ end
320+ local sorted_sub_times = {}
321+ for i = 1 , # sub_times do
322+ sorted_sub_times [i ] = sub_times [i ]
323+ end
324+ table.sort (sorted_sub_times , compare_times )
325+
326+ -- Use a helper table to avoid duplicates
327+ local added_times = {}
328+
329+ -- Rebuild sub_lines and sub_times based on the sorted timestamps
330+ local sorted_sub_lines = {}
331+ for _ , sub_time in ipairs (sorted_sub_times ) do
332+ -- Iterate over all subtitle content for this timestamp
333+ if not added_times [sub_time ] then
334+ added_times [sub_time ] = true
335+ for i , time in ipairs (sub_times ) do
336+ if time == sub_time then
337+ sorted_sub_lines [# sorted_sub_lines + 1 ] = sub_lines [i ]
338+ end
339+ end
340+ end
341+ end
342+
343+ -- Use the sorted subtitle list
344+ sub_lines = sorted_sub_lines
345+ sub_times = sorted_sub_times
346+
347+ -- Get the default item (last subtitle before current time position)
348+ for i , sub_time in ipairs (sub_times ) do
349+ if sub_time <= time_pos then
350+ default_item = i
281351 end
282352 end
283353
@@ -341,7 +411,7 @@ mp.add_key_binding(nil, "select-subtitle-line", function ()
341411 return
342412 end
343413
344- select_subtitle_line (result .stdout )
414+ select_subtitle_line (result .stdout , sub . codec )
345415 end )
346416end )
347417
0 commit comments