feat: Add option to remove source files after encoding

This commit is contained in:
Rémi Heredero 2025-04-27 14:38:15 +02:00
parent f10d1208f8
commit 72904f4412
Signed by: Klagarge
GPG Key ID: 735B36B074A65F0F

View File

@ -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()