Skip to content

Commit

Permalink
Use sys.stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Oct 22, 2023
1 parent 2a2825d commit 62022b9
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions auto_editor/preview.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import sys
from fractions import Fraction
from statistics import fmean, median
from typing import TextIO

from auto_editor.analyze import Levels
from auto_editor.output import Ensure
Expand All @@ -11,14 +13,17 @@
from auto_editor.utils.log import Log


def time_frame(title: str, ticks: float, tb: Fraction, per: str | None = None) -> None:
def time_frame(
fp: TextIO, title: str, ticks: float, tb: Fraction, per: str | None = None
) -> None:
tc = to_timecode(ticks / tb, "ass")

tp = 9 if tc.startswith("-") else 10
tcp = 12 if tc.startswith("-") else 11
preci = 0 if int(ticks) == ticks else 2
end = "" if per is None else f" {per:>7}"
print(f" - {f'{title}:':<{tp}} {tc:<{tcp}} {f'({ticks:.{preci}f})':<6}{end}")

fp.write(f" - {f'{title}:':<{tp}} {tc:<{tcp}} {f'({ticks:.{preci}f})':<6}{end}\n")


def all_cuts(tl: v3, in_len: int) -> list[int]:
Expand Down Expand Up @@ -50,36 +55,38 @@ def preview(ensure: Ensure, tl: v3, temp: str, log: Log) -> None:
# Calculate input videos length
in_len = 0
for src in tl.sources.values():
levels = Levels(ensure, src, tb, Bar("none"), temp, log).media_length
in_len += levels
in_len += Levels(ensure, src, tb, Bar("none"), temp, log).media_length

out_len = tl.out_len()

diff = out_len - in_len

print("\nlength:")
time_frame("input", in_len, tb, per="100.0%")
time_frame("output", out_len, tb, per=f"{round((out_len / in_len) * 100, 2)}%")
time_frame("diff", diff, tb, per=f"{round((diff / in_len) * 100, 2)}%")
fp = sys.stdout
fp.write("\nlength:\n")
time_frame(fp, "input", in_len, tb, "100.0%")
time_frame(fp, "output", out_len, tb, f"{round((out_len / in_len) * 100, 2)}%")
time_frame(fp, "diff", diff, tb, f"{round((diff / in_len) * 100, 2)}%")

clip_lens = [clip.dur / clip.speed for clip in tl.a[0]]
log.debug(clip_lens)

print(f"clips:\n - amount: {len(clip_lens)}")
fp.write(f"clips:\n - amount: {len(clip_lens)}\n")
if len(clip_lens) > 0:
time_frame("smallest", min(clip_lens), tb)
time_frame("largest", max(clip_lens), tb)
time_frame(fp, "smallest", min(clip_lens), tb)
time_frame(fp, "largest", max(clip_lens), tb)
if len(clip_lens) > 1:
time_frame("median", median(clip_lens), tb)
time_frame("average", fmean(clip_lens), tb)
time_frame(fp, "median", median(clip_lens), tb)
time_frame(fp, "average", fmean(clip_lens), tb)

cut_lens = all_cuts(tl, in_len)
log.debug(cut_lens)
print(f"cuts:\n - amount: {len(clip_lens)}")
fp.write(f"cuts:\n - amount: {len(clip_lens)}\n")
if len(cut_lens) > 0:
time_frame("smallest", min(cut_lens), tb)
time_frame("largest", max(cut_lens), tb)
time_frame(fp, "smallest", min(cut_lens), tb)
time_frame(fp, "largest", max(cut_lens), tb)
if len(cut_lens) > 1:
time_frame("median", median(cut_lens), tb)
time_frame("average", fmean(cut_lens), tb)
print("")
time_frame(fp, "median", median(cut_lens), tb)
time_frame(fp, "average", fmean(cut_lens), tb)

fp.write("\n")
fp.flush()

0 comments on commit 62022b9

Please sign in to comment.