Skip to content

Commit ccddfc1

Browse files
committed
Only generate subtitle files when needed
1 parent 621bfac commit ccddfc1

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

auto_editor/edit.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,6 @@ def edit_media(
212212
ensure = Ensure(ffmpeg, samplerate, temp, log)
213213

214214
if tl is None:
215-
# Extract subtitles in their native format.
216-
if src is not None and len(src.subtitles) > 0 and not args.sn:
217-
cmd = ["-i", f"{src.path}", "-hide_banner"]
218-
for s, sub in enumerate(src.subtitles):
219-
cmd.extend(["-map", f"0:s:{s}"])
220-
for s, sub in enumerate(src.subtitles):
221-
cmd.extend([os.path.join(temp, f"{s}s.{sub.ext}")])
222-
ffmpeg.run(cmd)
223-
224215
tl = make_timeline(sources, ensure, args, samplerate, bar, temp, log)
225216

226217
if export["export"] == "timeline":
@@ -282,7 +273,7 @@ def make_media(tl: v3, output: str) -> None:
282273
apply_later = False
283274

284275
if ctr.allow_subtitle and not args.sn:
285-
sub_output = make_new_subtitles(tl, ffmpeg, temp, log)
276+
sub_output = make_new_subtitles(tl, ffmpeg, ensure, temp, log)
286277

287278
if ctr.allow_audio:
288279
audio_output = make_new_audio(tl, ensure, args, ffmpeg, bar, temp, log)

auto_editor/output.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@ class Ensure:
1616
_sr: int
1717
temp: str
1818
log: Log
19-
labels: list[tuple[FileInfo, int]] = field(default_factory=list)
20-
sub_labels: list[tuple[FileInfo, int]] = field(default_factory=list)
19+
_audios: list[tuple[FileInfo, int]] = field(default_factory=list)
20+
_subtitles: list[tuple[FileInfo, int]] = field(default_factory=list)
2121

2222
def audio(self, src: FileInfo, stream: int) -> str:
2323
try:
24-
label = self.labels.index((src, stream))
24+
label = self._audios.index((src, stream))
2525
first_time = False
2626
except ValueError:
27-
self.labels.append((src, stream))
28-
label = len(self.labels) - 1
27+
self._audios.append((src, stream))
28+
label = len(self._audios) - 1
2929
first_time = True
3030

3131
out_path = os.path.join(self.temp, f"{label:x}.wav")
3232

3333
if first_time:
34+
self.log.debug(f"Making external audio: {out_path}")
3435
self.log.conwrite("Extracting audio")
35-
self.log.debug(f"Making external audio for stream: {stream}")
3636

3737
cmd = ["-i", f"{src.path}", "-map", f"0:a:{stream}"]
3838
cmd += ["-ac", "2", "-ar", f"{self._sr}", "-rf64", "always", out_path]
@@ -42,18 +42,18 @@ def audio(self, src: FileInfo, stream: int) -> str:
4242

4343
def subtitle(self, src: FileInfo, stream: int) -> str:
4444
try:
45-
label = self.sub_labels.index((src, stream))
45+
self._subtitles.index((src, stream))
4646
first_time = False
4747
except ValueError:
48-
self.sub_labels.append((src, stream))
49-
label = len(self.sub_labels) - 1
48+
self._subtitles.append((src, stream))
5049
first_time = True
5150

52-
out_path = os.path.join(self.temp, f"{label:x}.vtt")
51+
sub = src.subtitles[stream]
52+
out_path = os.path.join(self.temp, f"{stream}s.{sub.ext}")
5353

5454
if first_time:
55-
self.log.conwrite("Extracting subtitle")
5655
self.log.debug(f"Making external subtitle: {out_path}")
56+
self.log.conwrite("Extracting subtitle")
5757
self._ffmpeg.run(["-i", f"{src.path}", "-map", f"0:s:{stream}", out_path])
5858

5959
return out_path

auto_editor/render/subtitle.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from fractions import Fraction
1212

1313
from auto_editor.ffwrapper import FFmpeg
14+
from auto_editor.output import Ensure
1415
from auto_editor.timeline import v3
1516
from auto_editor.utils.chunks import Chunks
1617
from auto_editor.utils.log import Log
@@ -122,22 +123,24 @@ def write(self, file_path: str) -> None:
122123
file.write(self.footer)
123124

124125

125-
def make_new_subtitles(tl: v3, ffmpeg: FFmpeg, temp: str, log: Log) -> list[str]:
126+
def make_new_subtitles(
127+
tl: v3, ffmpeg: FFmpeg, ensure: Ensure, temp: str, log: Log
128+
) -> list[str]:
126129
if tl.v1 is None:
127130
return []
128131

129132
new_paths = []
130133

131134
for s, sub in enumerate(tl.v1.source.subtitles):
132-
file_path = os.path.join(temp, f"{s}s.{sub.ext}")
133135
new_path = os.path.join(temp, f"new{s}s.{sub.ext}")
134-
135136
parser = SubtitleParser(tl.tb)
136137

137138
if sub.codec in parser.supported_codecs:
139+
file_path = ensure.subtitle(tl.v1.source, s)
138140
with open(file_path, encoding="utf-8") as file:
139141
parser.parse(file.read(), sub.codec)
140142
else:
143+
file_path = os.path.join(temp, f"{s}s.{sub.ext}")
141144
convert_path = os.path.join(temp, f"{s}s_convert.vtt")
142145
ffmpeg.run(["-i", file_path, convert_path])
143146
with open(convert_path, encoding="utf-8") as file:

0 commit comments

Comments
 (0)