Skip to content

Commit

Permalink
Remember old progress bar when starting new one
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattBlue committed Sep 18, 2024
1 parent e240c8f commit 6085aa1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 63 deletions.
4 changes: 2 additions & 2 deletions auto_editor/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from auto_editor.render.subtitle import make_new_subtitles
from auto_editor.render.video import render_av
from auto_editor.timeline import v1, v3
from auto_editor.utils.bar import Bar
from auto_editor.utils.bar import initBar
from auto_editor.utils.chunks import Chunk, Chunks
from auto_editor.utils.cmdkw import ParserError, parse_with_palet, pAttr, pAttrs
from auto_editor.utils.container import Container, container_constructor
Expand Down Expand Up @@ -146,7 +146,7 @@ def parse_export(export: str, log: Log) -> dict[str, Any]:


def edit_media(paths: list[str], ffmpeg: FFmpeg, args: Args, log: Log) -> None:
bar = Bar(args.progress)
bar = initBar(args.progress)
tl = None

if paths:
Expand Down
2 changes: 1 addition & 1 deletion auto_editor/ffwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def initFileInfo(path: str, log: Log) -> FileInfo:

desc = cont.metadata.get("description", None)
bitrate = 0 if cont.bit_rate is None else cont.bit_rate
dur = 0 if cont.duration is None else cont.duration / 1_000_000
dur = 0 if cont.duration is None else cont.duration / av.time_base

cont.close()

Expand Down
8 changes: 4 additions & 4 deletions auto_editor/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def audio(self, src: FileInfo, stream: int) -> str:
astream = in_container.streams.audio[stream]

if astream.duration is None or astream.time_base is None:
dur = 1
dur = 1.0
else:
dur = int(astream.duration * astream.time_base)
dur = float(astream.duration * astream.time_base)

bar.start(dur, "Extracting audio")

Expand All @@ -58,8 +58,8 @@ def audio(self, src: FileInfo, stream: int) -> str:

resampler = AudioResampler(format="s16", layout="stereo", rate=sample_rate)
for i, frame in enumerate(in_container.decode(astream)):
if i % 1500 == 0:
bar.tick(0 if frame.time is None else frame.time)
if i % 1500 == 0 and frame.time is not None:
bar.tick(frame.time)

for new_frame in resampler.resample(frame):
for packet in output_astream.encode(new_frame):
Expand Down
4 changes: 2 additions & 2 deletions auto_editor/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from auto_editor.analyze import Levels
from auto_editor.timeline import v3
from auto_editor.utils.bar import Bar
from auto_editor.utils.bar import initBar
from auto_editor.utils.func import to_timecode
from auto_editor.utils.log import Log

Expand Down Expand Up @@ -65,7 +65,7 @@ def preview(tl: v3, log: Log) -> None:

in_len = 0
for src in all_sources:
in_len += Levels(src, tb, Bar("none"), False, log, False).media_length
in_len += Levels(src, tb, initBar("none"), False, log, False).media_length

out_len = tl.out_len()

Expand Down
4 changes: 2 additions & 2 deletions auto_editor/subcommands/levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from auto_editor.ffwrapper import initFileInfo
from auto_editor.lang.palet import env
from auto_editor.lib.contracts import is_bool, is_nat, is_nat1, is_str, is_void, orc
from auto_editor.utils.bar import Bar
from auto_editor.utils.bar import initBar
from auto_editor.utils.cmdkw import (
ParserError,
Required,
Expand Down Expand Up @@ -83,7 +83,7 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
parser = levels_options(ArgumentParser("levels"))
args = parser.parse_args(LevelArgs, sys_args)

bar = Bar("none")
bar = initBar("none")
log = Log(quiet=True)

sources = [initFileInfo(path, log) for path in args.input]
Expand Down
5 changes: 2 additions & 3 deletions auto_editor/subcommands/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from auto_editor.lang.stdenv import make_standard_env
from auto_editor.lib.data_structs import print_str
from auto_editor.lib.err import MyError
from auto_editor.utils.bar import Bar
from auto_editor.utils.bar import initBar
from auto_editor.utils.log import Log
from auto_editor.utils.types import frame_rate
from auto_editor.vanparse import ArgumentParser
Expand Down Expand Up @@ -64,9 +64,8 @@ def main(sys_args: list[str] = sys.argv[1:]) -> None:
sources = [initFileInfo(path, log) for path in args.input]
src = sources[0]
tb = src.get_fps() if args.timebase is None else args.timebase
bar = Bar("modern")
env["timebase"] = tb
env["@levels"] = Levels(src, tb, bar, False, log, strict)
env["@levels"] = Levels(src, tb, initBar("modern"), False, log, strict)

env.update(make_standard_env())
print(f"Auto-Editor {auto_editor.__version__}")
Expand Down
105 changes: 56 additions & 49 deletions auto_editor/utils/bar.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
from __future__ import annotations

import sys
from dataclasses import dataclass
from math import floor
from shutil import get_terminal_size
from time import localtime, time

from .func import get_stdout_bytes


class Bar:
def __init__(self, bar_type: str) -> None:
self.machine = False
self.hide = False
def initBar(bar_type: str) -> Bar:
icon = "⏳"
chars: tuple[str, ...] = (" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█")
brackets = ("|", "|")
machine = hide = False

if bar_type == "classic":
icon = "⏳"
chars = ("░", "█")
brackets = ("[", "]")
if bar_type == "ascii":
icon = "& "
chars = ("-", "#")
brackets = ("[", "]")
if bar_type == "machine":
machine = True
if bar_type == "none":
hide = True

part_width = len(chars) - 1

ampm = True
if sys.platform == "darwin" and bar_type in ("modern", "classic", "ascii"):
try:
date_format = get_stdout_bytes(
["defaults", "read", "com.apple.menuextra.clock", "Show24Hour"]
)
ampm = date_format == b"0\n"
except FileNotFoundError:
pass

self.icon = "⏳"
self.chars: tuple[str, ...] = (" ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█")
self.brackets = ("|", "|")
return Bar(icon, chars, brackets, machine, hide, part_width, ampm, [])

if bar_type == "classic":
self.icon = "⏳"
self.chars = ("░", "█")
self.brackets = ("[", "]")
if bar_type == "ascii":
self.icon = "& "
self.chars = ("-", "#")
self.brackets = ("[", "]")
if bar_type == "machine":
self.machine = True
if bar_type == "none":
self.hide = True

self.part_width = len(self.chars) - 1

self.ampm = True
if sys.platform == "darwin" and bar_type in ("modern", "classic", "ascii"):
try:
date_format = get_stdout_bytes(
["defaults", "read", "com.apple.menuextra.clock", "Show24Hour"]
)
self.ampm = date_format == b"0\n"
except FileNotFoundError:
pass

@dataclass(slots=True)
class Bar:
icon: str
chars: tuple[str, ...]
brackets: tuple[str, str]
machine: bool
hide: bool
part_width: int
ampm: bool
stack: list[tuple[str, int, float, float]]

@staticmethod
def pretty_time(my_time: float, ampm: bool) -> str:
Expand All @@ -62,28 +74,25 @@ def tick(self, index: float) -> None:
if self.hide:
return

progress = 0.0 if self.total == 0 else min(1, max(0, index / self.total))
rate = 0.0 if progress == 0 else (time() - self.begin_time) / progress
title, len_title, total, begin = self.stack[-1]
progress = 0.0 if total == 0 else min(1, max(0, index / total))
rate = 0.0 if progress == 0 else (time() - begin) / progress

if self.machine:
index = min(index, self.total)
secs_til_eta = round(self.begin_time + rate - time(), 2)
print(
f"{self.title}~{index}~{self.total}~{secs_til_eta}",
end="\r",
flush=True,
)
index = min(index, total)
secs_til_eta = round(begin + rate - time(), 2)
print(f"{title}~{index}~{total}~{secs_til_eta}", end="\r", flush=True)
return

new_time = self.pretty_time(self.begin_time + rate, self.ampm)
new_time = self.pretty_time(begin + rate, self.ampm)

percent = round(progress * 100, 1)
p_pad = " " * (4 - len(str(percent)))
columns = get_terminal_size().columns
bar_len = max(1, columns - (self.len_title + 32))
bar_len = max(1, columns - (len_title + 32))
bar_str = self._bar_str(progress, bar_len)

bar = f" {self.icon}{self.title} {bar_str} {p_pad}{percent}% ETA {new_time}"
bar = f" {self.icon}{title} {bar_str} {p_pad}{percent}% ETA {new_time}"

if len(bar) > columns - 2:
bar = bar[: columns - 2]
Expand All @@ -93,10 +102,7 @@ def tick(self, index: float) -> None:
sys.stdout.write(bar + "\r")

def start(self, total: float, title: str = "Please wait") -> None:
self.title = title
self.len_title = len(title)
self.total = total
self.begin_time = time()
self.stack.append((title, len(title), total, time()))

try:
self.tick(0)
Expand Down Expand Up @@ -124,6 +130,7 @@ def _bar_str(self, progress: float, width: int) -> str:
)
return line

@staticmethod
def end() -> None:
def end(self) -> None:
sys.stdout.write(" " * (get_terminal_size().columns - 2) + "\r")
if self.stack:
self.stack.pop()

0 comments on commit 6085aa1

Please sign in to comment.