Skip to content

Commit bc9f2a8

Browse files
committed
Add encoders to bar title
1 parent 2b0bbec commit bc9f2a8

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

auto_editor/edit.py

+54-3
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ def set_audio_codec(
136136
return codec
137137

138138

139+
def normalize_encoder(codec: str) -> tuple[str, bool]:
140+
is_hardware = False
141+
if "_" in codec:
142+
is_hardware = codec.split("_")[-1] in {"videotoolbox", "cuvid", "nvec"}
143+
144+
id = av.Codec(codec, "w").id
145+
name = {
146+
27: "h264",
147+
97: "gif",
148+
173: "hevc",
149+
167: "vp9",
150+
139: "vp8",
151+
86018: "aac",
152+
}.get(id, codec)
153+
154+
return name, is_hardware
155+
156+
139157
def parse_export(export: str, log: Log) -> dict[str, Any]:
140158
exploded = export.split(":", maxsplit=1)
141159
if len(exploded) == 1:
@@ -365,17 +383,46 @@ def make_media(tl: v3, output_path: str) -> None:
365383

366384
# Setup video
367385
if ctr.default_vid != "none" and tl.v:
368-
vframes = render_av(output, tl, args, bar, log)
386+
vframes = render_av(output, tl, args, log)
369387
output_stream = next(vframes)
370388
else:
371389
output_stream, vframes = None, iter([])
372390

391+
no_color = log.no_color or log.machine
392+
encoder_titles = []
393+
if output_stream is not None:
394+
name, h = normalize_encoder(output_stream.name)
395+
if no_color:
396+
encoder_titles.append(name)
397+
else:
398+
is_bold = ";1" if h else ""
399+
encoder_titles.append(f"\033[95{is_bold}m{name}")
400+
if audio_streams:
401+
name, h = normalize_encoder(audio_streams[0].name) # type: ignore
402+
if no_color:
403+
encoder_titles.append(name)
404+
else:
405+
is_bold = ";1" if h else ""
406+
encoder_titles.append(f"\033[96{is_bold}m{name}")
407+
if subtitle_streams:
408+
name = subtitle_streams[0].name # type: ignore
409+
if no_color:
410+
encoder_titles.append(name)
411+
else:
412+
encoder_titles.append(f"\033[32m{name}")
413+
414+
title = f"({os.path.splitext(output_path)[1][1:]}) "
415+
if no_color:
416+
title += "+".join(encoder_titles)
417+
else:
418+
title += "\033[0m+".join(encoder_titles) + "\033[0m"
419+
bar.start(tl.end, title)
420+
373421
# Process frames
374422
while True:
375423
audio_frames = [next(frames, None) for frames in audio_gen_frames]
376-
video_frame = next(vframes, None)
424+
index, video_frame = next(vframes, (0, None))
377425
subtitle_frames = [next(packet, None) for packet in sub_gen_frames]
378-
379426
if (
380427
all(frame is None for frame in audio_frames)
381428
and video_frame is None
@@ -405,12 +452,16 @@ def make_media(tl: v3, output_path: str) -> None:
405452
except av.FFmpegError as e:
406453
log.error(e)
407454

455+
bar.tick(index)
456+
408457
# Flush streams
409458
if output_stream is not None:
410459
output.mux(output_stream.encode(None))
411460
for audio_stream in audio_streams:
412461
output.mux(audio_stream.encode(None))
413462

463+
bar.end()
464+
414465
# Close resources
415466
for audio_input in audio_inputs:
416467
audio_input.close()

auto_editor/render/video.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
from auto_editor.ffwrapper import FileInfo
1717
from auto_editor.timeline import v3
18-
from auto_editor.utils.bar import Bar
1918
from auto_editor.utils.log import Log
2019
from auto_editor.utils.types import Args
2120

@@ -31,7 +30,7 @@ class VideoFrame:
3130

3231
def make_solid(width: int, height: int, pix_fmt: str, bg: str) -> av.VideoFrame:
3332
hex_color = bg.lstrip("#").upper()
34-
rgb_color = tuple(int(hex_color[i : i + 2], 16) for i in {0, 2, 4})
33+
rgb_color = tuple(int(hex_color[i : i + 2], 16) for i in (0, 2, 4))
3534

3635
rgb_array = np.full((height, width, 3), rgb_color, dtype=np.uint8)
3736
rgb_frame = av.VideoFrame.from_ndarray(rgb_array, format="rgb24")
@@ -62,7 +61,7 @@ def make_image_cache(tl: v3) -> dict[tuple[FileInfo, int], np.ndarray]:
6261

6362

6463
def render_av(
65-
output: av.container.OutputContainer, tl: v3, args: Args, bar: Bar, log: Log
64+
output: av.container.OutputContainer, tl: v3, args: Args, log: Log
6665
) -> Any:
6766
from_ndarray = av.VideoFrame.from_ndarray
6867

@@ -191,8 +190,6 @@ def render_av(
191190
seek_frame = None
192191
frames_saved = 0
193192

194-
bar.start(tl.end, "Creating new video")
195-
196193
bg = args.background
197194
null_frame = make_solid(target_width, target_height, target_pix_fmt, bg)
198195
frame_index = -1
@@ -313,11 +310,7 @@ def render_av(
313310

314311
if frame.format.name != target_pix_fmt:
315312
frame = frame.reformat(format=target_pix_fmt)
316-
bar.tick(index)
317-
elif index % 3 == 0:
318-
bar.tick(index)
319313

320-
yield from_ndarray(frame.to_ndarray(), format=frame.format.name)
314+
yield (index, from_ndarray(frame.to_ndarray(), format=frame.format.name))
321315

322-
bar.end()
323316
log.debug(f"Total frames saved seeking: {frames_saved}")

0 commit comments

Comments
 (0)