From 72904f4412d8955b19c4283e4d06ed78f9d6f56b Mon Sep 17 00:00:00 2001 From: Klagarge Date: Sun, 27 Apr 2025 14:38:15 +0200 Subject: [PATCH] feat: Add option to remove source files after encoding --- encode_video.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/encode_video.py b/encode_video.py index 6ecabc7..b796111 100755 --- a/encode_video.py +++ b/encode_video.py @@ -14,7 +14,7 @@ def get_duration(file_path): ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) return float(result.stdout.strip()) -def encode(input_file, codec): +def encode(input_file, codec, remove_source=False): if codec == "x265": ffmpeg_codec = "libx265" crf = 26 @@ -82,8 +82,14 @@ def encode(input_file, codec): os.remove(output_file) else: print("\nāœ… Done.") + if remove_source: + try: + os.remove(input_file) + print(f"šŸ—‘ļø Source file {input_file} removed.") + except Exception as e: + print(f"āš ļø Could not delete source file {input_file}: {e}") -def encode_batch(directory, codec): +def encode_batch(directory, codec, remove_source=False): if not os.path.isdir(directory): print(f"āŒ Not a valid directory: {directory}") return @@ -93,7 +99,7 @@ def encode_batch(directory, codec): if file.lower().endswith(SUPPORTED_EXTENSIONS): filepath = os.path.join(root, file) try: - encode(filepath, codec) + encode(filepath, codec, remove_source) except Exception as e: print(f"\nāŒ Error with file {file}: {e}") continue @@ -103,15 +109,16 @@ def main(): parser.add_argument("input", nargs="?", help="Path to input file") parser.add_argument("-d", "--directory", help="Path to a directory for batch encoding") parser.add_argument("--codec", choices=["x265", "av1"], default="x265", help="Codec to use (default: x265)") + parser.add_argument("--remove-source", action="store_true", help="Remove the source file after successful encoding") args = parser.parse_args() if args.directory: - encode_batch(args.directory, args.codec) + encode_batch(args.directory, args.codec, args.remove_source) elif args.input: if not os.path.isfile(args.input): print(f"āŒ File not found: {args.input}") sys.exit(1) - encode(args.input, args.codec) + encode(args.input, args.codec, args.remove_source) else: print("āŒ Please provide a file path or use -d for a directory.") parser.print_help()