Last active
March 17, 2024 09:53
-
-
Save Solarflame5/1ec8eaaf5b48d7aa5c5290680df3d5e5 to your computer and use it in GitHub Desktop.
Python script to batch convert media files with ffmpeg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import subprocess | |
arguments = sys.argv[1:] # py batchconvert.py [input directory] [output directory] [target file extension] [additional arguments] | |
if(arguments == []): | |
print( | |
""" Usage: | |
py batchconvert.py [input directory] [output directory] [target file extension] [additional arguments]""" | |
) | |
sys.exit() | |
try: | |
input_directory = arguments[0] | |
except: | |
print("Missing input directory, run script without arguments for help") | |
sys.exit() | |
try: | |
output_directory = arguments[1] | |
except: | |
print("Missing output directory, run script without arguments for help") | |
sys.exit() | |
try: | |
target_extension = arguments[2] | |
except: | |
print("Missing target file extension, run script without arguments for help") | |
sys.exit() | |
try: | |
additional_args = arguments[3] | |
except: | |
additional_args = "" | |
if(input_directory[-1] != "/"): | |
input_directory = input_directory + "/" | |
if(output_directory[-1] != "/"): | |
output_directory = output_directory + "/" | |
input_files_list = os.listdir(input_directory) | |
for filename in input_files_list: | |
ffmpeg_command = f"ffmpeg -i \"{input_directory + filename}\" \"{output_directory + filename + "." + target_extension}\" {additional_args}" | |
subprocess.run(ffmpeg_command) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment