Advanced Video Batch Processing with CSV Input

In the world of video editing and content management, processing multiple video segments efficiently can save hours of tedious work. Whether you're preparing lecture recordings, editing interviews, or managing video assets, automating the process is a game-changer. Today, I'll walk you through a powerful yet simple bash script that uses CSV files to streamline batch video processing.

The Problem with Manual Video Processing

If you've ever needed to extract specific segments from multiple videos, you know the routine: open the video editor, navigate to the start point, mark it, find the end point, mark it, export, and repeat. This becomes exponentially time-consuming as your video library grows.

Enter CSV-Based Batch Processing

The solution is surprisingly simple: a bash script that reads instructions from a CSV file and lets FFmpeg do the heavy lifting. This approach gives you precise control while eliminating repetitive manual tasks.

How It Works

The script reads a CSV file where each line contains four pieces of information:

  • Input video path
  • Output video path
  • Start timestamp
  • End timestamp

For each line in the CSV, the script calls FFmpeg to extract (trim) the specified segment without re-encoding (preserving quality and processing quickly).

The Script (Linux and macOS)

#!/bin/bash
# This script processes videos according to a CSV file
# CSV format: input_file,output_file,start_time,end_time
# Usage: ./process_video_list.sh video_list.csv

csv_file="$1"

while IFS=, read -r input output start end; do
    echo "Processing: $input -> $output (from $start to $end)"
    ffmpeg -nostdin -i "$input" -ss "$start" -to "$end" -c copy "$output" -hide_banner -loglevel warning
done < "$csv_file"

I have already written a guide on how to trim a video using FFmpeg here. In the guide, you’ll find more options and tips for trimming videos with FFmpeg.

Setting Up Your CSV File

Create a plain text file with the .csv extension and add your processing instructions like this:

videos/lecture1.mp4,outputs/lecture1_intro.mp4,00:00:00,00:02:30
videos/lecture1.mp4,outputs/lecture1_main.mp4,00:10:00,00:25:00
videos/lecture2.mp4,outputs/lecture2_summary.mp4,01:15:30,01:20:00

Each line represents one video segment to extract. In this example:

  • The first line extracts the 2.5-minute intro from lecture1.mp4
  • The second line grabs the main content (15 minutes) from the same lecture
  • The third line extracts a 4.5-minute summary from lecture2.mp4

The timestamps use the format HH:MM:SS, but you can also use seconds or HH:MM:SS.mmm for millisecond precision.

Running the Script

  1. Save the script as process_video_list.sh
  2. Make it executable: chmod +x process_video_list.sh
  3. Run it with your CSV file: ./process_video_list.sh video_list.csv

This script will read the CSV file line by line, splitting each line into four fields: input file, output file, start time, and end time. It then will use FFmpeg to trim the input video and save it to the output file.

Understanding the Code: Line-by-Line Breakdown

The heart of our batch processing script is the while loop that reads and processes each line from the CSV file. Let's dissect it to understand exactly how it works:

while IFS=, read -r input output start end; do
  echo "Processing: $input -> $output (from $start to $end)"
  ffmpeg -nostdin -i "$input" -ss "$start" -to "$end" -c copy "$output" -hide_banner -loglevel warning
done < "$csv_file"

Let's breaking it down:

  1. while IFS=, read -r input output start end; do
    • IFS=, sets the Internal Field Separator to a comma. This tells Bash how to split each line into variables.
    • read -r input output start end reads one line from the CSV file and assigns each comma-separated value to the respective variable:
      • input: Path to the source video file
      • output: Path where the trimmed video will be saved
      • start: Timestamp where the clip should begin
      • end: Timestamp where the clip should end
    • The -r flag prevents backslashes from being interpreted as escape characters
  2. echo "Processing: $input -> $output (from $start to $end)"
    • This provides user feedback, displaying which file is currently being processed and the trim points
  3. ffmpeg -nostdin -i "$input" -ss "$start" -to "$end" -c copy "$output" -hide_banner -loglevel warning
    • -nostdin: To prevent ffmpeg from interfering with your loop’s input, it ensures that ffmpeg doesn’t consume any part of your CSV file’s content.
    • -i "$input": Specifies the input file
    • -ss "$start": Sets the start time for the clip
    • -to "$end": Sets the end time for the clip
    • -c copy: Tells FFmpeg to copy the video and audio streams without re-encoding (much faster, preserves quality)
    • -hide_banner -loglevel warning: Reduces console output to only show warnings and errors
    • "$output": Specifies where to save the resulting video
  4. done < "$csv_file"
    • This redirects the contents of the CSV file (specified by $csv_file) into the while loop
    • The loop reads line by line until it reaches the end of the file

Common issues with Windows files

When processing CSV files created on Windows, one common issue is the presence of Windows-style line endings, which use a carriage return followed by a line feed (CRLF, or \r\n). This extra carriage return character (\r) can cause unexpected behavior in Unix-like environments, as the shell may treat it as part of the data rather than as a mere line delimiter.

Possible solutions are:

  • Use tr -d '\r' < video_list.csv > video_list_unix.csv to remove carriage returns by filtering the file’s content and writing it to a new file. Then, use video_list_unix.csv as your input file.
  • Alternatively, you can use sed to remove carriage returns: sed -i 's/\r$//' video_list.csv

What Makes This Approach Powerful

This simple loop is powerful because:

  • It processes an unlimited number of video clips in a single run
  • Each line in the CSV can specify completely different files and time ranges
  • The script handles all the FFmpeg command formatting for you
  • It provides clear feedback during processing
  • By using stream copying (-c copy), it processes videos quickly without quality loss

With just these few lines of code, you can automate complex video trimming tasks that would otherwise require tedious manual work in video editing software.

Practical Applications

This approach is particularly useful for:

  • Creating highlights from longer recordings
  • Breaking long videos into logical segments
  • Extracting key moments from interviews or presentations
  • Preparing clean versions of videos by removing unwanted sections

Taking It Further

You could expand this script to handle additional FFmpeg parameters in your CSV, such as:

  • Resolution changes
  • Format conversions
  • Audio adjustments
  • Adding simple filters

Conclusion

Sometimes the simplest tools can have the biggest impact on your workflow. This bash script and CSV combination creates a powerful, flexible system for batch processing videos with minimal effort. By automating repetitive tasks, you can focus more on content and less on manual processing.

This method is useful for processing a large number of videos with different trim points. You can easily modify the script to add more processing steps, such as adding watermarks or applying filters.

By automating video processing with a simple bash script and CSV file, you can save hours of manual editing work and focus on more important tasks.

Give it a try, and you might be surprised how much time you save on your next video project!