3
3
from auto_editor .timeline import v3
4
4
from auto_editor .utils .log import Log
5
5
6
+ import numpy as np
6
7
7
8
def find_chunk (target : int , chunks : list [tuple [int , int , float ]]) -> int :
8
9
low = 0
@@ -22,6 +23,15 @@ def find_chunk(target: int, chunks: list[tuple[int, int, float]]) -> int:
22
23
raise ValueError ("" )
23
24
24
25
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
25
35
def make_new_subtitles (tl : v3 , old_out : str , out_path : str , log : Log ) -> None :
26
36
if tl .v1 is None :
27
37
return None
@@ -35,53 +45,49 @@ def make_new_subtitles(tl: v3, old_out: str, out_path: str, log: Log) -> None:
35
45
36
46
out_streams = {}
37
47
for stream in processed .streams :
38
- if stream .type in ("video" , "audio" , "data" , "attachment" ):
48
+ if stream .type in ("video" , "audio" , "data" , "attachment" , "" ):
39
49
out_streams [stream .index ] = output .add_stream (template = stream )
40
50
41
51
for stream in input_cont .streams :
42
52
if stream .type == "subtitle" :
43
53
out_streams [stream .index ] = output .add_stream (template = stream )
44
54
45
55
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 )
49
58
50
- chunks = tl .v1 .chunks
51
- new_start = 0
59
+ scroll = chunk_unroll ( tl .v1 .chunks )
60
+ lock = 0
52
61
for packet in input_cont .demux ():
53
62
if packet .dts is None :
54
63
continue
55
64
56
65
if packet .stream .type == "subtitle" :
57
66
if packet .pts is None or packet .duration is None :
58
67
continue
68
+
59
69
if not packet .decode ():
60
70
continue # Skip empty packets
61
71
62
72
start = round (packet .pts * packet .time_base * tl .tb )
63
73
end = round ((packet .pts + packet .duration ) * packet .time_base * tl .tb )
64
74
65
- index = find_chunk (start , chunks )
66
-
67
75
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 ))
82
88
packet .stream = out_streams [packet .stream .index ]
83
89
output .mux_one (packet )
84
90
85
- input_cont .close ()
86
- processed .close ()
87
91
output .close ()
92
+ processed .close ()
93
+ input_cont .close ()
0 commit comments