Skip to content

Commit 63536a6

Browse files
committed
Fix sub analyze
1 parent 11f663c commit 63536a6

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

auto_editor/analyze.py

+17-23
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,9 @@ def subtitle(
343343
for packet in container.demux(subtitle_stream):
344344
if packet.pts is None or packet.duration is None:
345345
continue
346-
for subset in packet.decode():
347-
# See definition of `AVSubtitle`
348-
# in: https://ffmpeg.org/doxygen/trunk/avcodec_8h_source.html
349-
start = float(packet.pts * subtitle_stream.time_base)
350-
dur = float(packet.duration * subtitle_stream.time_base)
351-
352-
end = round((start + dur) * self.tb)
353-
sub_length = max(sub_length, end)
346+
sub_length = max(sub_length, packet.pts + packet.duration)
354347

348+
sub_length = round(sub_length * subtitle_stream.time_base * self.tb)
355349
result = np.zeros((sub_length), dtype=np.bool_)
356350
del sub_length
357351

@@ -363,25 +357,25 @@ def subtitle(
363357
continue
364358
if early_exit:
365359
break
366-
for subset in packet.decode():
367-
if max_count is not None and count >= max_count:
368-
early_exit = True
369-
break
370360

371-
start = float(packet.pts * subtitle_stream.time_base)
372-
dur = float(packet.duration * subtitle_stream.time_base)
361+
if max_count is not None and count >= max_count:
362+
early_exit = True
363+
break
364+
365+
start = float(packet.pts * subtitle_stream.time_base)
366+
dur = float(packet.duration * subtitle_stream.time_base)
373367

374-
san_start = round(start * self.tb)
375-
san_end = round((start + dur) * self.tb)
368+
san_start = round(start * self.tb)
369+
san_end = round((start + dur) * self.tb)
376370

377-
for sub in subset:
378-
if not isinstance(sub, AssSubtitle):
379-
continue
371+
for sub in packet.decode():
372+
if not isinstance(sub, AssSubtitle):
373+
continue
380374

381-
line = sub.dialogue.decode(errors="ignore")
382-
if line and re.search(re_pattern, line):
383-
result[san_start:san_end] = 1
384-
count += 1
375+
line = sub.dialogue.decode(errors="ignore")
376+
if line and re.search(re_pattern, line):
377+
result[san_start:san_end] = 1
378+
count += 1
385379

386380
container.seek(0)
387381
return result

0 commit comments

Comments
 (0)