feat(ffmpeg_handling): convert all files with suffix to new_suffix

change convert-av to subprocess
add test file for .avi
This commit is contained in:
Lucas Vivo committed 2026-11-05 14:54:40 +01:00
1 parent fa249771b8
commit d2b4667eb5
2 files changed
+165 -83

No files matched your search

+165 -83
View File
@@ -30,6 +30,7 @@ commands = {
"test": {"name": "test", "output": False},
"info": {"name": "info", "output": False},
"convert-av": {"name": "convert-av", "output": True},
"convert-av-all": {"name": "convert-av-all", "output": True},
"convert-audio": {"name": "convert-audio", "output": True},
"convert-video": {"name": "convert-video", "output": True},
"remove-subs": {"name": "remove-subs", "output": True},
@@ -155,7 +156,8 @@ class ArgsBuffer:
num: int | None = None
start: str | None = None
duration: str | None = None
suffix: str | None = None
new_suffix: str | None = None
CYRILLIC_RE = re.compile(r"[А-Яа-яЁё]")
@@ -432,43 +434,63 @@ def cmd_info(args):
info = ffmpeg.probe(str(args.input))
logger.info(json.dumps(info, indent=2))
except ffmpeg.Error as e:
logger.error("Error probing file:", e)
logger.error(f"Error probing file:{e}")
return False, "nooutputneeded"
def cmd_convert_av_all_suffix(args):
"""
Re-encodes both video and audio for all files of suffix <cmd_convert_av_all_suffix>.
"""
if args.input.suffix != args.suffix:
return False, "nooutputneeded"
return cmd_convert_av(args)
def cmd_convert_av(args):
"""
Re-encodes both video and audio.
FFmpeg Equivalent:
ffmpeg -i [input] -c:v [video_codec] -c:a [audio_codec] -preset [preset] -crf [quality] [output]
Parameters:
- vcodec/c:v: Sets the video encoder (e.g., libx264).
- acodec/c:a: Sets the audio encoder (e.g., aac).
- preset: Trade-off between compression speed and efficiency (ultrafast to veryslow).
- qp/crf: Constant Rate Factor. Lower is higher quality (18-28 is standard).
ffmpeg -i input -c:v libx264 -c:a aac -preset medium -qp 23 output.mkv
"""
if args.suffix:
output = args.output.with_suffix(args.suffix)
if args.new_suffix:
output = args.output.with_suffix(args.new_suffix)
else:
output = args.output
inp = ffmpeg.input(args.input)
out = ffmpeg.output(
inp,
str(args.output),
vcodec=args.video_codec,
acodec=args.audio_codec,
scodec=get_scodec(output),
preset=args.preset,
qp=args.quality,
# This maps all streams from the first input once
map="0",
# Prevents FFmpeg from trying to guess/add extra default streams
map_metadata=0,
logger.debug(f"Convert Audio/Video for {args.input} -> {output}")
cmd = [
"ffmpeg",
"-i",
str(args.input),
"-map",
"0",
"-map_metadata",
"0",
"-c:v",
args.video_codec,
"-c:a",
args.audio_codec,
"-c:s",
get_scodec(output),
"-preset",
args.preset,
"-qp",
str(args.quality),
str(output),
]
result = subprocess.run(
cmd,
check=False,
capture_output=True,
text=True,
)
return True, out
return result.returncode == 0, "nooutputneeded"
def cmd_convert_audio(args):
@@ -1156,6 +1178,7 @@ def cmd_test(args):
test_files = [
"test_auto.mkv",
"test_auto.mp4",
"test_auto.avi",
"test_auto_only_russian.mkv",
"test_auto_no_subs.mkv",
"test_auto_no_subs.mp4",
@@ -1407,20 +1430,20 @@ def cmd_test(args):
logger.info(f"{green_tag('PASS')} {file_path}")
return return_value
ret = True
# ---------- INFO ----------
# args = ArgsBuffer()
# args.input = test_file_mp4
# test_dict = {}
# run_and_probe(cmd["name"], cmd_info, args, test_dict)
# run_and_probe(cmd_test["name"], cmd_info, args, test_dict)
# args.input = test_file_mkv
# ret = ret and run_and_probe(cmd["name"], cmd_info, args, test_dict)
# ret = ret and run_and_probe(cmd_test["name"], cmd_info, args, test_dict)
# ---------- DOWNSIZE ----------
# ---------- DOWNSIZE ----------#
ret = True
args_test = ArgsBuffer()
cmd = commands["downsize"]
if cmd["name"] in args.choices:
cmd_test = commands["downsize"]
if cmd_test["name"] in args.choices:
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
test_file
@@ -1428,7 +1451,7 @@ def cmd_test(args):
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.width = 640
args_test.height = 360
@@ -1441,11 +1464,11 @@ def cmd_test(args):
"video_codecs": ["h264"],
}
]
ret = ret and run_and_probe(cmd["name"], cmd_downsize, args_test, test_dict)
ret = ret and run_and_probe(cmd_test["name"], cmd_downsize, args_test, test_dict)
# ---------- REMOVE AUDIO TRACK ----------
cmd = commands["remove-audio"]
if cmd["name"] in args.choices:
cmd_test = commands["remove-audio"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
args_test.audio_index = [1]
@@ -1463,16 +1486,16 @@ def cmd_test(args):
]
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
ret = ret and run_and_probe(
cmd["name"], cmd_remove_audio_track, args_test, test_dict
cmd_test["name"], cmd_remove_audio_track, args_test, test_dict
)
# ---------- EXTRACT AUDIO TRACK ----------
cmd = commands["extract-audio"]
if cmd["name"] in args.choices:
cmd_test = commands["extract-audio"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
args_test.audio_index = [1]
@@ -1483,21 +1506,23 @@ def cmd_test(args):
Path(test_dir) / f"extracted_audio_{Path(test_file).suffix[1:]}.mp3"
)
ret = ret and run_and_probe(
cmd["name"], cmd_extract_audio_track, args_test, test_dict
cmd_test["name"], cmd_extract_audio_track, args_test, test_dict
)
# ---------- CONVERT AV ----------
cmd = commands["convert-av"]
if cmd["name"] in args.choices:
cmd_test = commands["convert-av"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
test_file
)
args_test.input = test_file
args_test.suffix = ".mkv"
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
Path(test_dir)
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}_"
f"{Path(test_file).suffix.replace(".","")}_{args_test.suffix}"
)
args_test.video_codec = "libx264"
args_test.audio_codec = "aac"
@@ -1512,14 +1537,52 @@ def cmd_test(args):
"audio_codecs": ["aac"],
}
]
logger.trace(f"Convert file {args_test.input}")
ret = ret and run_and_probe(
cmd["name"], cmd_convert_av, args_test, test_dict
cmd_test["name"], cmd_convert_av, args_test, test_dict
)
# ---------- CONVERT AV ALL ----------
cmd_test = commands["convert-av-all"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
test_file
)
args_test.input = test_file
args_test.suffix = ".avi"
args_test.new_suffix = ".mp4"
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{args_test.new_suffix}"
)
args_test.video_codec = "libx264"
args_test.audio_codec = "aac"
args_test.preset = "fast"
args_test.quality = 23
test_dict = [
{
"video_count": len(video_streams),
"audio_count": len(audio_streams),
"subtitle_count": len(subtitle_streams),
}
]
if test_file.suffix == args_test.suffix:
test_dict[0]["video_codecs"] = ["h264"]
test_dict[0]["audio_codecs"] = ["aac"]
if test_file.suffix != args_test.suffix:
test_dict[0]["video_codecs"] = [s.get("codec_name") for s in video_streams]
test_dict[0]["audio_codecs"] = [s.get("codec_name") for s in video_streams]
ret = ret and run_and_probe(
cmd_test["name"], cmd_convert_av_all_suffix, args_test, test_dict
)
# ---------- CONVERT AUDIO ----------
cmd = commands["convert-audio"]
cmd_test = commands["convert-audio"]
if cmd["name"] in args.choices:
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1528,7 +1591,7 @@ def cmd_test(args):
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.audio_codec = "aac"
test_dict = [
@@ -1540,12 +1603,12 @@ def cmd_test(args):
}
]
ret = ret and run_and_probe(
cmd["name"], cmd_convert_audio, args_test, test_dict
cmd_test["name"], cmd_convert_audio, args_test, test_dict
)
# ---------- CONVERT VIDEO ----------
cmd = commands["convert-video"]
if cmd["name"] in args.choices:
cmd_test = commands["convert-video"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1554,7 +1617,7 @@ def cmd_test(args):
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.video_codec = "libx264"
test_dict = [
@@ -1566,12 +1629,12 @@ def cmd_test(args):
}
]
ret = ret and run_and_probe(
cmd["name"], cmd_convert_video, args_test, test_dict
cmd_test["name"], cmd_convert_video, args_test, test_dict
)
# ---------- REMOVE SUBS ----------
cmd = commands["remove-subs"]
if cmd["name"] in args.choices:
cmd_test = commands["remove-subs"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1588,15 +1651,15 @@ def cmd_test(args):
]
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
ret = ret and run_and_probe(
cmd["name"], cmd_remove_subs, args_test, test_dict
cmd_test["name"], cmd_remove_subs, args_test, test_dict
)
# ---------- FILTER AUDIO ----------
cmd = commands["filter-audio"]
if cmd["name"] in args.choices:
cmd_test = commands["filter-audio"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1605,7 +1668,7 @@ def cmd_test(args):
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.audio_index = [1]
test_dict = [
@@ -1617,12 +1680,12 @@ def cmd_test(args):
}
]
ret = ret and run_and_probe(
cmd["name"], cmd_filter_audio, args_test, test_dict
cmd_test["name"], cmd_filter_audio, args_test, test_dict
)
# ---------- CUT ----------
cmd = commands["cut"]
if cmd["name"] in args.choices:
cmd_test = commands["cut"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1631,7 +1694,7 @@ def cmd_test(args):
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.start = "0:0:5"
args_test.duration = "10s"
@@ -1644,12 +1707,12 @@ def cmd_test(args):
}
]
ret = ret and run_and_probe(
cmd["name"], cmd_cut_segment, args_test, test_dict
cmd_test["name"], cmd_cut_segment, args_test, test_dict
)
# ---------- SET PRIMARY AUDIO ----------
cmd = commands["set-primary-audio"]
if cmd["name"] in args.choices:
cmd_test = commands["set-primary-audio"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1658,7 +1721,7 @@ def cmd_test(args):
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.audio_index = [2]
test_dict = [
@@ -1669,12 +1732,12 @@ def cmd_test(args):
}
]
ret = ret and run_and_probe(
cmd["name"], cmd_set_primary_audio, args_test, test_dict
cmd_test["name"], cmd_set_primary_audio, args_test, test_dict
)
# ---------- APPLY CHAPTERS-----
cmd = commands["chapters"]
if cmd["name"] in args.choices:
cmd_test = commands["chapters"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
test_file = test_file_dir / "test_auto.m4b"
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1683,7 +1746,7 @@ def cmd_test(args):
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.chapter_file = test_file_dir / "test_auto_chapters.txt"
lines = Path(args_test.chapter_file).read_text(encoding="utf-8").splitlines()
@@ -1696,12 +1759,12 @@ def cmd_test(args):
}
]
ret = ret and run_and_probe(
cmd["name"], cmd_apply_chapters, args_test, test_dict
cmd_test["name"], cmd_apply_chapters, args_test, test_dict
)
# ---------- WRITE METADATA-----
cmd = commands["metadata-set"]
if cmd["name"] in args.choices:
cmd_test = commands["metadata-set"]
if cmd_test["name"] in args.choices:
args_test = ArgsBuffer()
test_file = test_file_dir / "test_auto.m4b"
video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1710,7 +1773,7 @@ def cmd_test(args):
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.key = "artist"
args_test.value = "TEST"
@@ -1722,22 +1785,22 @@ def cmd_test(args):
"metadata": {"artist": "TEST"},
}
]
ret = ret and run_and_probe(cmd["name"], cmd_set_metadata, args_test, test_dict)
ret = ret and run_and_probe(cmd_test["name"], cmd_set_metadata, args_test, test_dict)
# ---------- REMOVE RUSSIAN AUDIO -----
cmd = commands["remove-russian-from-db"]
if cmd["name"] in args.choices:
cmd_test = commands["remove-russian-from-db"]
if cmd_test["name"] in args.choices:
for test_file in test_files:
args_test = ArgsBuffer()
args_test.input = test_file
args_test.output = (
Path(test_dir)
/ f"out_{str(cmd['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
/ f"out_{str(cmd_test['name']).replace('-', '_')}_{Path(test_file).stem}{Path(test_file).suffix}"
)
args_test.loud = False
test_dict = [{"no_russian_dir": test_dir}]
ret = ret and run_and_probe(
cmd["name"], cmd_remove_russian_from_db, args_test, test_dict
cmd_test["name"], cmd_remove_russian_from_db, args_test, test_dict
)
## -----------FOLDER TEST---------------
@@ -1881,9 +1944,28 @@ def main():
p.add_argument("--video-codec", default="libx264")
p.add_argument("--audio-codec", default="aac")
p.add_argument("--preset", default="medium")
p.add_argument("--preset", default="slow")
p.add_argument("--quality", type=int)
p.add_argument("--suffix",type=str)
p.add_argument("--new-suffix", type=str)
# =========================================================
# CONVERT AV ALL
# =========================================================
p = add_command(
sub,
"convert-av-all",
cmd_convert_av_all_suffix,
"Full video/audio re-encode for all files of suffx <suffix>",
aliases=["call"],
parents=[base_p],
)
p.add_argument("--video-codec", default="libx264")
p.add_argument("--audio-codec", default="aac")
p.add_argument("--preset", default="slow")
p.add_argument("--quality", type=int)
p.add_argument("--suffix", type=str)
p.add_argument("--new-suffix", type=str)
# =========================================================
# CONVERT AUDIO