Skip to content

Instantly share code, notes, and snippets.

@paulonteri
Created April 5, 2024 08:01
Show Gist options
  • Save paulonteri/0e6cd7dd54a144b2ba962db0e3d71122 to your computer and use it in GitHub Desktop.
Save paulonteri/0e6cd7dd54a144b2ba962db0e3d71122 to your computer and use it in GitHub Desktop.
Convert videos in working folder for Audi MMI
import subprocess
import os
def convert_videos(folder_path):
# Change the current working directory to the target folder
os.chdir(folder_path)
# List all files in the current directory
files = os.listdir(".")
print(f"Files in '{folder_path}': {files}")
# create a directory to store the converted videos (if it doesn't exist)
if not os.path.exists("output_main"):
os.makedirs("output_main")
for file in files:
print("\n" * 10)
# Check if the file is a video based on its extension
if file.endswith((".mp4", ".avi", ".mov", ".mkv")):
print(f"Converting '{file}'...")
# Construct the output filename
output_file = (
"output_main/" + os.path.splitext(file)[0] + " (converted).mp4"
)
# Check if the output file already exists
if os.path.exists(output_file):
print(f"File '{output_file}' already exists. Skipping...")
continue
# Construct the FFmpeg command
command = [
"ffmpeg",
"-i",
file,
"-vf",
"scale=720:-2",
"-c:v",
"mpeg4",
"-r",
"30",
"-c:a",
"libmp3lame",
"-ar",
"48000",
"-b:a",
"320k",
output_file,
]
# Execute the FFmpeg command
try:
subprocess.run(command, check=True)
print(f"Converted '{file}' successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to convert '{file}'. FFmpeg error:\n{e}")
else:
print(f"Skipping '{file}' (not a video file).")
# Use the current working directory
current_working_dir = os.getcwd()
convert_videos(current_working_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment