Created
March 2, 2023 20:23
-
-
Save martinbowling/2671762ec9dd1107a25aa13903d4fc6b to your computer and use it in GitHub Desktop.
Multithreaded Python Script for AVI to MP4 conversion
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 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