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
+163 -81

No files matched your search

+163 -81
View File
@@ -30,6 +30,7 @@ commands = {
"test": {"name": "test", "output": False}, "test": {"name": "test", "output": False},
"info": {"name": "info", "output": False}, "info": {"name": "info", "output": False},
"convert-av": {"name": "convert-av", "output": True}, "convert-av": {"name": "convert-av", "output": True},
"convert-av-all": {"name": "convert-av-all", "output": True},
"convert-audio": {"name": "convert-audio", "output": True}, "convert-audio": {"name": "convert-audio", "output": True},
"convert-video": {"name": "convert-video", "output": True}, "convert-video": {"name": "convert-video", "output": True},
"remove-subs": {"name": "remove-subs", "output": True}, "remove-subs": {"name": "remove-subs", "output": True},
@@ -155,7 +156,8 @@ class ArgsBuffer:
num: int | None = None num: int | None = None
start: str | None = None start: str | None = None
duration: str | None = None duration: str | None = None
suffix: str | None = None
new_suffix: str | None = None
CYRILLIC_RE = re.compile(r"[А-Яа-яЁё]") CYRILLIC_RE = re.compile(r"[А-Яа-яЁё]")
@@ -432,43 +434,63 @@ def cmd_info(args):
info = ffmpeg.probe(str(args.input)) info = ffmpeg.probe(str(args.input))
logger.info(json.dumps(info, indent=2)) logger.info(json.dumps(info, indent=2))
except ffmpeg.Error as e: except ffmpeg.Error as e:
logger.error("Error probing file:", e) logger.error(f"Error probing file:{e}")
return False, "nooutputneeded" 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): def cmd_convert_av(args):
""" """
Re-encodes both video and audio. Re-encodes both video and audio.
FFmpeg Equivalent: FFmpeg Equivalent:
ffmpeg -i [input] -c:v [video_codec] -c:a [audio_codec] -preset [preset] -crf [quality] [output] ffmpeg -i input -c:v libx264 -c:a aac -preset medium -qp 23 output.mkv
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).
""" """
if args.suffix: if args.new_suffix:
output = args.output.with_suffix(args.suffix) output = args.output.with_suffix(args.new_suffix)
else: else:
output = args.output output = args.output
inp = ffmpeg.input(args.input)
out = ffmpeg.output( logger.debug(f"Convert Audio/Video for {args.input} -> {output}")
inp, cmd = [
str(args.output), "ffmpeg",
vcodec=args.video_codec, "-i",
acodec=args.audio_codec, str(args.input),
scodec=get_scodec(output), "-map",
preset=args.preset, "0",
qp=args.quality, "-map_metadata",
# This maps all streams from the first input once "0",
map="0", "-c:v",
# Prevents FFmpeg from trying to guess/add extra default streams args.video_codec,
map_metadata=0, "-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): def cmd_convert_audio(args):
@@ -1156,6 +1178,7 @@ def cmd_test(args):
test_files = [ test_files = [
"test_auto.mkv", "test_auto.mkv",
"test_auto.mp4", "test_auto.mp4",
"test_auto.avi",
"test_auto_only_russian.mkv", "test_auto_only_russian.mkv",
"test_auto_no_subs.mkv", "test_auto_no_subs.mkv",
"test_auto_no_subs.mp4", "test_auto_no_subs.mp4",
@@ -1407,20 +1430,20 @@ def cmd_test(args):
logger.info(f"{green_tag('PASS')} {file_path}") logger.info(f"{green_tag('PASS')} {file_path}")
return return_value return return_value
ret = True
# ---------- INFO ---------- # ---------- INFO ----------
# args = ArgsBuffer() # args = ArgsBuffer()
# args.input = test_file_mp4 # args.input = test_file_mp4
# test_dict = {} # 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 # 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() args_test = ArgsBuffer()
cmd = commands["downsize"] cmd_test = commands["downsize"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
for test_file in test_files: for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
test_file test_file
@@ -1428,7 +1451,7 @@ def cmd_test(args):
args_test.input = test_file args_test.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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.width = 640
args_test.height = 360 args_test.height = 360
@@ -1441,11 +1464,11 @@ def cmd_test(args):
"video_codecs": ["h264"], "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 ---------- # ---------- REMOVE AUDIO TRACK ----------
cmd = commands["remove-audio"] cmd_test = commands["remove-audio"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
args_test.audio_index = [1] args_test.audio_index = [1]
@@ -1463,16 +1486,16 @@ def cmd_test(args):
] ]
args_test.output = ( args_test.output = (
Path(test_dir) 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( 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 ---------- # ---------- EXTRACT AUDIO TRACK ----------
cmd = commands["extract-audio"] cmd_test = commands["extract-audio"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
args_test.audio_index = [1] 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" Path(test_dir) / f"extracted_audio_{Path(test_file).suffix[1:]}.mp3"
) )
ret = ret and run_and_probe( 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 ---------- # ---------- CONVERT AV ----------
cmd = commands["convert-av"] cmd_test = commands["convert-av"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
for test_file in test_files: for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
test_file test_file
) )
args_test.input = test_file args_test.input = test_file
args_test.suffix = ".mkv"
args_test.output = ( args_test.output = (
Path(test_dir) 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}_"
f"{Path(test_file).suffix.replace(".","")}_{args_test.suffix}"
) )
args_test.video_codec = "libx264" args_test.video_codec = "libx264"
args_test.audio_codec = "aac" args_test.audio_codec = "aac"
@@ -1512,14 +1537,52 @@ def cmd_test(args):
"audio_codecs": ["aac"], "audio_codecs": ["aac"],
} }
] ]
logger.trace(f"Convert file {args_test.input}")
ret = ret and run_and_probe( 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 ---------- # ---------- 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() args_test = ArgsBuffer()
for test_file in test_files: for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( 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.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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" args_test.audio_codec = "aac"
test_dict = [ test_dict = [
@@ -1540,12 +1603,12 @@ def cmd_test(args):
} }
] ]
ret = ret and run_and_probe( 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 ---------- # ---------- CONVERT VIDEO ----------
cmd = commands["convert-video"] cmd_test = commands["convert-video"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
for test_file in test_files: for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( 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.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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" args_test.video_codec = "libx264"
test_dict = [ test_dict = [
@@ -1566,12 +1629,12 @@ def cmd_test(args):
} }
] ]
ret = ret and run_and_probe( 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 ---------- # ---------- REMOVE SUBS ----------
cmd = commands["remove-subs"] cmd_test = commands["remove-subs"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
for test_file in test_files: for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( video_streams, audio_streams, subtitle_streams, cover_image = get_streams(
@@ -1588,15 +1651,15 @@ def cmd_test(args):
] ]
args_test.output = ( args_test.output = (
Path(test_dir) 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( 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 ---------- # ---------- FILTER AUDIO ----------
cmd = commands["filter-audio"] cmd_test = commands["filter-audio"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
for test_file in test_files: for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( 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.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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] args_test.audio_index = [1]
test_dict = [ test_dict = [
@@ -1617,12 +1680,12 @@ def cmd_test(args):
} }
] ]
ret = ret and run_and_probe( 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 ---------- # ---------- CUT ----------
cmd = commands["cut"] cmd_test = commands["cut"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
for test_file in test_files: for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( 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.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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.start = "0:0:5"
args_test.duration = "10s" args_test.duration = "10s"
@@ -1644,12 +1707,12 @@ def cmd_test(args):
} }
] ]
ret = ret and run_and_probe( 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 ---------- # ---------- SET PRIMARY AUDIO ----------
cmd = commands["set-primary-audio"] cmd_test = commands["set-primary-audio"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
for test_file in test_files: for test_file in test_files:
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( 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.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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] args_test.audio_index = [2]
test_dict = [ test_dict = [
@@ -1669,12 +1732,12 @@ def cmd_test(args):
} }
] ]
ret = ret and run_and_probe( 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----- # ---------- APPLY CHAPTERS-----
cmd = commands["chapters"] cmd_test = commands["chapters"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
test_file = test_file_dir / "test_auto.m4b" test_file = test_file_dir / "test_auto.m4b"
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( 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.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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" args_test.chapter_file = test_file_dir / "test_auto_chapters.txt"
lines = Path(args_test.chapter_file).read_text(encoding="utf-8").splitlines() 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( 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----- # ---------- WRITE METADATA-----
cmd = commands["metadata-set"] cmd_test = commands["metadata-set"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
args_test = ArgsBuffer() args_test = ArgsBuffer()
test_file = test_file_dir / "test_auto.m4b" test_file = test_file_dir / "test_auto.m4b"
video_streams, audio_streams, subtitle_streams, cover_image = get_streams( 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.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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.key = "artist"
args_test.value = "TEST" args_test.value = "TEST"
@@ -1722,22 +1785,22 @@ def cmd_test(args):
"metadata": {"artist": "TEST"}, "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 ----- # ---------- REMOVE RUSSIAN AUDIO -----
cmd = commands["remove-russian-from-db"] cmd_test = commands["remove-russian-from-db"]
if cmd["name"] in args.choices: if cmd_test["name"] in args.choices:
for test_file in test_files: for test_file in test_files:
args_test = ArgsBuffer() args_test = ArgsBuffer()
args_test.input = test_file args_test.input = test_file
args_test.output = ( args_test.output = (
Path(test_dir) 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 args_test.loud = False
test_dict = [{"no_russian_dir": test_dir}] test_dict = [{"no_russian_dir": test_dir}]
ret = ret and run_and_probe( 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--------------- ## -----------FOLDER TEST---------------
@@ -1881,9 +1944,28 @@ def main():
p.add_argument("--video-codec", default="libx264") p.add_argument("--video-codec", default="libx264")
p.add_argument("--audio-codec", default="aac") 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("--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("--quality", type=int)
p.add_argument("--suffix", type=str) p.add_argument("--suffix", type=str)
p.add_argument("--new-suffix", type=str)
# ========================================================= # =========================================================
# CONVERT AUDIO # CONVERT AUDIO
Binary file not shown.