Skip to content

Commit acdebf7

Browse files
committed
Experimenting
1 parent a5fff11 commit acdebf7

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

auto_editor/render/subtitle.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from auto_editor.timeline import v3
44
from auto_editor.utils.log import Log
55

6+
import numpy as np
67

78
def find_chunk(target: int, chunks: list[tuple[int, int, float]]) -> int:
89
low = 0
@@ -22,6 +23,15 @@ def find_chunk(target: int, chunks: list[tuple[int, int, float]]) -> int:
2223
raise ValueError("")
2324

2425

26+
def chunk_unroll(chunks: list[tuple[int, int, float]]) -> np.ndarray:
27+
result = np.zeros((chunks[-1][1]), dtype=np.float64)
28+
29+
for chunk in chunks:
30+
result[chunk[0]:chunk[1]] = chunk[2]
31+
32+
return result
33+
34+
# TODO: This
2535
def make_new_subtitles(tl: v3, old_out: str, out_path: str, log: Log) -> None:
2636
if tl.v1 is None:
2737
return None
@@ -35,53 +45,49 @@ def make_new_subtitles(tl: v3, old_out: str, out_path: str, log: Log) -> None:
3545

3646
out_streams = {}
3747
for stream in processed.streams:
38-
if stream.type in ("video", "audio", "data", "attachment"):
48+
if stream.type in ("video", "audio", "data", "attachment", ""):
3949
out_streams[stream.index] = output.add_stream(template=stream)
4050

4151
for stream in input_cont.streams:
4252
if stream.type == "subtitle":
4353
out_streams[stream.index] = output.add_stream(template=stream)
4454

4555
for packet in processed.demux():
46-
if packet.stream.index in out_streams:
47-
packet.stream = out_streams[packet.stream.index]
48-
output.mux_one(packet)
56+
packet.stream = out_streams[packet.stream.index]
57+
output.mux(packet)
4958

50-
chunks = tl.v1.chunks
51-
new_start = 0
59+
scroll = chunk_unroll(tl.v1.chunks)
60+
lock = 0
5261
for packet in input_cont.demux():
5362
if packet.dts is None:
5463
continue
5564

5665
if packet.stream.type == "subtitle":
5766
if packet.pts is None or packet.duration is None:
5867
continue
68+
5969
if not packet.decode():
6070
continue # Skip empty packets
6171

6272
start = round(packet.pts * packet.time_base * tl.tb)
6373
end = round((packet.pts + packet.duration) * packet.time_base * tl.tb)
6474

65-
index = find_chunk(start, chunks)
66-
6775
new_duration = 0.0
68-
for chunk in chunks[index:]:
69-
if chunk[1] >= end:
70-
break
71-
if chunk[2] == 0.0 or chunk[2] >= 99999.0:
72-
pass
73-
else:
74-
new_duration += (chunk[1] - chunk[0]) / chunk[2]
75-
76-
new_start += packet.pts
77-
packet.pts = new_start
78-
packet.dts = packet.pts
79-
80-
if new_duration > 0:
81-
packet.duration = round(new_duration / tl.tb / packet.time_base)
76+
for i in range(start, end):
77+
if i < len(scroll) and scroll[i] != 99999.0:
78+
new_duration += 1 * scroll[i]
79+
80+
new_duration = int(new_duration)
81+
if new_duration > 0.0:
82+
packet.pts = lock
83+
packet.dts = lock
84+
85+
lock += 1000
86+
87+
packet.duration = int(new_duration / (packet.time_base * tl.tb))
8288
packet.stream = out_streams[packet.stream.index]
8389
output.mux_one(packet)
8490

85-
input_cont.close()
86-
processed.close()
8791
output.close()
92+
processed.close()
93+
input_cont.close()

0 commit comments

Comments
 (0)