Skip to content

Commit

Permalink
Experimenting
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed May 9, 2024
1 parent a5fff11 commit 0bf550e
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions auto_editor/render/subtitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from auto_editor.timeline import v3
from auto_editor.utils.log import Log

import numpy as np

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


def chunk_unroll(chunks: list[tuple[int, int, float]]) -> np.ndarray:
result = np.zeros((chunks[-1][1]), dtype=np.float64)

for chunk in chunks:
result[chunk[0]:chunk[1]] = chunk[2]

return result

# TODO: This
def make_new_subtitles(tl: v3, old_out: str, out_path: str, log: Log) -> None:
if tl.v1 is None:
return None
Expand All @@ -35,53 +45,49 @@ def make_new_subtitles(tl: v3, old_out: str, out_path: str, log: Log) -> None:

out_streams = {}
for stream in processed.streams:
if stream.type in ("video", "audio", "data", "attachment"):
if stream.type in ("video", "audio", "data", "attachment", ""):
out_streams[stream.index] = output.add_stream(template=stream)

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

for packet in processed.demux():
if packet.stream.index in out_streams:
packet.stream = out_streams[packet.stream.index]
output.mux_one(packet)
packet.stream = out_streams[packet.stream.index]
output.mux(packet)

chunks = tl.v1.chunks
new_start = 0
scroll = chunk_unroll(tl.v1.chunks)
lock = 0
for packet in input_cont.demux():
if packet.dts is None:
continue

if packet.stream.type == "subtitle":
if packet.pts is None or packet.duration is None:
continue

if not packet.decode():
continue # Skip empty packets

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

index = find_chunk(start, chunks)

new_duration = 0.0
for chunk in chunks[index:]:
if chunk[1] >= end:
break
if chunk[2] == 0.0 or chunk[2] >= 99999.0:
pass
else:
new_duration += (chunk[1] - chunk[0]) / chunk[2]

new_start += packet.pts
packet.pts = new_start
packet.dts = packet.pts

if new_duration > 0:
packet.duration = round(new_duration / tl.tb / packet.time_base)
for i in range(start, end):
if scroll[i] != 99999.0:
new_duration += 1 * scroll[i]

new_duration = int(new_duration)
if new_duration > 0.0:
packet.pts = lock
packet.dts = lock

lock += 1000

packet.duration = int(new_duration / (packet.time_base * tl.tb))
packet.stream = out_streams[packet.stream.index]
output.mux_one(packet)

input_cont.close()
processed.close()
output.close()
processed.close()
input_cont.close()

0 comments on commit 0bf550e

Please sign in to comment.