Parallel Process MP4 Files Using FFmpeg and Bash

Processing video files sequentially can be incredibly slow, especially when dealing with a large directory of high-resolution MP4s. This guide provides a robust, straight-to-the-point Bash script that uses FFmpeg to process multiple MP4 files simultaneously. By utilizing background processes and controlling the maximum number of parallel jobs, you can maximize your CPU usage and drastically reduce your total encoding time.

The Bash Script

Save the following script as parallel_ffmpeg.sh. This script scans the current directory for .mp4 files, processes them in parallel up to a specified limit, and saves the output to a designated directory.

#!/bin/bash

# Define the maximum number of parallel FFmpeg processes
MAX_JOBS=4

# Define the output directory
OUTPUT_DIR="./processed_videos"
mkdir -p "$OUTPUT_DIR"

# Loop through all MP4 files in the current directory
for file in *.mp4; do
    # Check if files exist to prevent errors in empty directories
    [ -e "$file" ] || continue

    echo "Starting: $file"

    # Run FFmpeg in the background (&)
    # Modify the FFmpeg parameters here to fit your specific needs
    ffmpeg -y -i "$file" -c:v libx264 -crf 23 -c:a aac "$OUTPUT_DIR/${file%.mp4}_processed.mp4" -nostdin -loglevel error &

    # Keep track of active background jobs
    # If the limit is reached, wait before spawning more
    while [ $(jobs -r | wc -l) -ge "$MAX_JOBS" ]; do
        sleep 1
    done
done

# Wait for all remaining background processes to complete
wait
echo "All MP4 files have been processed."

How to Run the Script

  1. Make the script executable: Open your terminal and run the following command in the directory where you saved the script:

    chmod +x parallel_ffmpeg.sh
  2. Execute the script: Place the script in the directory containing your MP4 files and run it:

    ./parallel_ffmpeg.sh

Key Components Explained