Member-only story

Creating a Robust Video Concatenation Script with ffmpeg on Linux

Siva
2 min readJan 29, 2024

Introduction

Combining multiple video files into a single cohesive unit is a common requirement in multimedia projects. Leveraging the power of the ffmpeg command-line tool on Linux, we can create a robust script to handle video concatenation seamlessly. In this blog post, we’ll walk through the process of building a stable script, addressing common issues and ensuring a smooth execution.

Prerequisites

Before we get started, make sure you have ffmpeg installed on your Linux system. You can install it using the package manager specific to your distribution:

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install ffmpeg

# CentOS
sudo yum install ffmpeg

The Script:

In the script below (combine_videos.sh), we’ve enhanced it to exclude empty video files before concatenating:

#!/bin/bash

# Script for combining videos using ffmpeg

# Specify the directory containing your video files
video_directory="path/to/videos"

# Specify the output file
output_file="output.mp4"

# Change to the video directory
cd "$video_directory" || exit

# Generate filelist.txt excluding empty files
find . -type f -iname "*.mp4" -size +0c -printf "file '%f'\n" > filelist.txt

# Check if filelist.txt is empty
if [ -s filelist.txt ]; then
# Combine videos using ffmpeg
ffmpeg -f concat -safe 0 -i filelist.txt -c copy "$output_file" 2>/dev/null

#…

--

--

No responses yet