diff --git a/media_editing/ffmpeg_handling.py b/media_editing/ffmpeg_handling.py index b82b347..5f62767 100755 --- a/media_editing/ffmpeg_handling.py +++ b/media_editing/ffmpeg_handling.py @@ -137,6 +137,13 @@ def is_russian_track(track: dict) -> bool: return descr or lang or language +def video_files(path: Path, recursive: bool): + iterator = path.rglob("*") if recursive else path.iterdir() + yield from ( + p for p in iterator if p.is_file() and p.suffix.lower() in VIDEO_SUFFIXES + ) + + class ArgsBuffer: input: Path | None = None output: Path | None = None @@ -209,7 +216,7 @@ def move_overwrite_file(files_move_from, files_move_to, args): backup_files = args.backup silent = not args.loud for file_to, file_from in zip(files_move_to, files_move_from): - if not file_to.is_file(): + if not file_to.is_file() and not hasattr(args, "new_suffix"): logger.error(f"overwrite Target File does not exist: {file_to}") return False @@ -464,6 +471,7 @@ def cmd_convert_av(args): logger.debug(f"Convert Audio/Video for {args.input} -> {output}") cmd = [ "ffmpeg", + "-y", "-i", str(args.input), "-map", @@ -488,6 +496,8 @@ def cmd_convert_av(args): check=False, capture_output=True, text=True, + encoding="utf-8", + errors="replace", ) return result.returncode == 0, "nooutputneeded" @@ -1960,11 +1970,11 @@ def main(): parents=[base_p], ) + p.add_argument("suffix", type=str) 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) # ========================================================= @@ -2285,24 +2295,12 @@ def main(): if input_path.is_dir(): counter = 0 logger.info(f"Processing dir: {input_path}") - if args.recursive: - sorted_files = sorted( - ( - p - for p in input_path.rglob("*") - if p.is_file() and p.suffix.lower() in VIDEO_SUFFIXES - ), - key=natural_sort_key, - ) - else: - sorted_files = sorted( - ( - p - for p in input_path.iterdir() - if p.is_file() and p.suffix.lower() in VIDEO_SUFFIXES - ), - key=natural_sort_key, - ) + + sorted_files = sorted( + video_files(input_path, args.recursive), + key=natural_sort_key, + ) + for file in sorted_files: if ( file.suffix.lower() not in VIDEO_SUFFIXES @@ -2323,6 +2321,12 @@ def main(): if output_str != "nooutputneeded": outputs.append(output_str) if args.overwrite and return_value: + if hasattr(args, "new_suffix") and args.new_suffix: + out_path = file.with_name( + f"{file.stem}_overwrite{args.new_suffix}" + ) + safe_delete(file, logger) + file = file.with_suffix(args.new_suffix) files_move_from.append(out_path) files_move_to.append(file) counter += 1