added ffmpeg progressbar

This commit is contained in:
Hellow2
2023-06-15 09:58:48 +02:00
parent 5c31d6f7a8
commit 805017fea5
3 changed files with 49 additions and 64 deletions

View File

@@ -1,47 +1,18 @@
from typing import List, Tuple
import ffmpeg
from tqdm import tqdm
from ffmpeg_progress_yield import FfmpegProgress
import subprocess
from ..utils.shared import BITRATE, AUDIO_FORMAT, CODEX_LOGGER as LOGGER
from ..objects import Target
def remove_intervals(target: Target, interval_list: List[Tuple[float, float]]):
def correct_codec(target: Target, bitrate_kb: int = BITRATE, audio_format: str = AUDIO_FORMAT, interval_list: List[Tuple[float, float]] = None):
if not target.exists:
LOGGER.warning(f"Target doesn't exist: {target.file_path}")
return
output_target = Target(
path=target._path,
file=str(target._file) + "." + AUDIO_FORMAT
)
# https://stackoverflow.com/questions/50594412/cut-multiple-parts-of-a-video-with-ffmpeg
aselect_list: List[str] = []
start = 0
next_start = 0
for end, next_start in interval_list:
aselect_list.append(f"between(t,{start},{end})")
start = next_start
aselect_list.append(f"gte(t,{next_start})")
select = f"aselect='{'+'.join(aselect_list)}',asetpts=N/SR/TB"
r = subprocess.run(["ffmpeg", "-i", str(target.file_path), "-af", select, str(output_target.file_path)])
if r.stderr != "":
LOGGER.debug(r.stderr)
output_target.copy_content(target)
output_target.delete()
def correct_codec(target: Target, bitrate_kb: int = BITRATE, audio_format: str = AUDIO_FORMAT):
if not target.exists:
LOGGER.warning(f"Target doesn't exist: {target.file_path}")
return
interval_list = interval_list or []
bitrate_b = int(bitrate_kb / 1024)
@@ -49,18 +20,36 @@ def correct_codec(target: Target, bitrate_kb: int = BITRATE, audio_format: str =
path=target._path,
file=str(target._file) + "." + audio_format
)
# get the select thingie
# https://stackoverflow.com/questions/50594412/cut-multiple-parts-of-a-video-with-ffmpeg
aselect_list: List[str] = []
start = 0
next_start = 0
for end, next_start in interval_list:
aselect_list.append(f"between(t,{start},{end})")
start = next_start
aselect_list.append(f"gte(t,{next_start})")
select = f"aselect='{'+'.join(aselect_list)}',asetpts=N/SR/TB"
# build the ffmpeg command
ffmpeg_command = [
"ffmpeg",
"-i", str(target.file_path),
"-af", select,
"-b", str(bitrate_b),
str(output_target.file_path)
]
stream = ffmpeg.input(target.file_path)
stream = stream.audio
stream = ffmpeg.output(
stream,
str(output_target.file_path),
audio_bitrate=bitrate_b,
format=audio_format
)
out, err = ffmpeg.run(stream, quiet=True, overwrite_output=True)
if err != "":
LOGGER.debug(err)
# run the ffmpeg command with a progressbar
ff = FfmpegProgress(ffmpeg_command)
with tqdm(total=100, position=1, desc="ffmpeg") as pbar:
for progress in ff.run_command_with_progress():
pbar.update(progress - pbar.n)
LOGGER.debug(ff.stderr)
output_target.copy_content(target)
output_target.delete()