Skip to content

Instantly share code, notes, and snippets.

@martinbowling
Created March 2, 2023 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbowling/2671762ec9dd1107a25aa13903d4fc6b to your computer and use it in GitHub Desktop.
Save martinbowling/2671762ec9dd1107a25aa13903d4fc6b to your computer and use it in GitHub Desktop.
Multithreaded Python Script for AVI to MP4 conversion
import os
import multiprocessing
from pathlib import Path
import subprocess
# Specify the folder containing the .avi files
input_folder = Path("/path/to/avi/files")
# Create the subfolder for the converted files if it doesn't exist
converted_folder = input_folder / "converted"
if not converted_folder.exists():
converted_folder.mkdir()
# Function to convert a single file
def convert_file(filepath):
output_filepath = converted_folder / (filepath.stem + ".mp4")
subprocess.run(["ffmpeg", "-i", str(filepath), "-c:v", "libx264", "-c:a", "aac", "-b:a", "192k", str(output_filepath)])
os.remove(filepath) # Remove the original .avi file after converting
# Convert all .avi files in parallel
with multiprocessing.Pool() as pool:
pool.map(convert_file, input_folder.glob("*.avi"))
print("Conversion complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment