Edit videos using FFmpeg concat

Video editing includes a lot of stitching together of different clips to make a longer video. This process is often easily done within a video editing application by dragging and dropping different video clips and placing them along a timeline. But how would you go about this if you wanted to do this with code?

We'll see how to do exactly that using FFmpeg — a command-line utility that can be used to create, edit and process different types of media. We'll look at two different ways to use FFmpeg and we'll end by looking at how you can achieve the same results using Shotstack — a cloud-based video editing API.

Concatenate videos with FFmpeg concat

When using FFmpeg to join a series of video clips, the process is called concatenation, or concat for short. There are several ways that videos can be concatenated, depending on whether the individual videos share the same codec or not. We are going to look at how to concat videos that share the same codec, for the latter, please refer to the documentation.

For videos that have the same codec, you can concatenate them using concat demuxer or concat protocol.

Concatenate a list of videos with FFmpeg using concat demuxer

To concatenate using the demuxer, first create a text file with a list of videos you want concatenated. Below is an example file videos.txt. You can add comments to the file, they will be ignored by the demuxer.

# videos.txt
file 'input1.mp4'
file 'input2.mp4'
file 'input3.mp4'

With that done, if you run the following command, the videos listed in that file will be concatenated, according to the order they are listed.

$ ffmpeg -f concat -i videos.txt -c copy output8.mp4

Concatenate all videos in a directory with FFmpeg using concat demuxer

If you have a large number of videos in a directory, it might be easier to write a command that will create a file with the list of videos for you before concatenating them. To determine the order that the videos will be listed, you can name them alphabetically or numerically. They will be listed according to LC_COLLATE.

$ for f in *.mp4; do echo "file '$f'" >> videos.txt; done

The above will create a file videos.txt with a list of files with the extension .mp4. After the file is generated, you can then concatenate the listed video with the command we used earlier:

$ ffmpeg -f concat -i videos.txt -c copy output9.mp4

Concatenate videos with the concat protocol

You can also concatenate videos with the concat protocol. The following code concatenates four videos without re-encoding them.

ffmpeg -i "concat:input1.mp4|input2.mp4|input3.mp4|input4.mp4" -c copy output10.mp4

Stitching videos with Shotstack

An alternative to FFmpeg is the Shotstack video editing API that provides an opinionated framework to edit your videos. It's media generation infrastructure can be used to code scalable applications and workflows that can be used to automate the creation and editing of videos and images at scale.

To use Shotstack, you describe your video edit in JSON that you post to the video editing API, either via a script or the command line. The video will be processed in the cloud and you'll be able to download the resulting video.

To learn more on how Shotstack works check out the Hello World tutorial.

Concatenate videos using JSON

To use Shotstack, first register for a free account to get an API key. You get 20 minutes render time per month for free.

Create a file labelled concatenate.json and add the following to it:

{
"timeline": {
"background": "#000000",
"tracks": [
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/footage/hot-air-balloons-1.mp4"
},
"start": 0,
"length": 5
}
]
},
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/footage/hot-air-balloons-2.mp4"
},
"start": 5,
"length": 5
}
]
},
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/footage/hot-air-balloons-3.mp4"
},
"start": 10,
"length": 5
}
]
}
]
},
"output": {
"format": "mp4",
"resolution": "sd"
}
}

We include three videos that will be concatenated or stitched together, adding a start time and length for each.

Next, using cURL, post the JSON to Shotstack for processing (paste in your API key):

curl -X POST \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
-d @concat.json \
https://api.shotstack.io/stage/render

You'll get a response back containing JSON data that has an id key. Copy the id and make another cURL request (paste in your API key and render ID):

curl -X GET \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_API_KEY" \
https://api.shotstack.io/stage/render/YOUR_ID

After making the above call, once the video has completed rendering, you should get a response containing JSON data that has a url key. Copy its value and open the link in a browser. You should see your concatenated video.

You can even make the video a bit more interesting by adding some transitions:

{
"timeline": {
"background": "#000000",
"tracks": [
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/footage/hot-air-balloons-1.mp4"
},
"start": 0,
"length": 5,
"transition": {
"in": "fade",
"out": "fade"
}
}
]
},
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/footage/hot-air-balloons-2.mp4"
},
"start": 4,
"length": 5,
"transition": {
"out": "fade"
}
}
]
},
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/footage/hot-air-balloons-3.mp4"
},
"start": 8,
"length": 5,
"transition": {
"out": "fade"
}
}
]
}
]
},
"output": {
"format": "mp4",
"resolution": "sd"
}
}

Below is the resulting video:

Next steps

If you want to learn more about FFmpeg take a look through our other articles about common ffMPEG operations such as how to convert images to video, rotating your videos or trimming your videos

Jeff Shillitto

BY JOYCE ECHESSA
28th June, 2021