FFmpeg Series: complex filters to trim and concat files
FFmpeg is a great tool and we can't emphasize enough how important it is in the modern video processing stack. As John Carmack himself said:
When I started at Oculus, I didn't have any experience with the practicalities of video at scale, and I was a little surprised when I found that all the giant companies used the open source ffmpeg project in their backends. I expected some unique combination of commercial and \
— John Carmack (@ID_AA_Carmack) May 7, 2020
We are very grateful to all the contributors of FFmpeg and wanted to share some of the ffmpeg tips-and-tricks we use with MediaMachine so we're kicking off this series hoping it will be helpful to people just starting with FFmpeg.
Ready for an interesting bite-sized ffmpeg trick?
Split & Concat the same input files at different time-stamps#
Here is an neat way you can take snippets from an input video and concat different chunks of time.
Let's break this down:
-i input-file.mp4- the
-iflag sets input. You can have multiple inputs which will map to a number starting from 0 (zero).
- the
-filter_complex- this indicates a filter block. Note: filters work on raw data streams so it's a good indicator that ffmpeg will be forced to encode the data again (ie. slower)
[0:v]- this is the ffmpeg mapping syntax. Read this as: Select
input 0, then select its(v)ideostream.
- this is the ffmpeg mapping syntax. Read this as: Select
trim=start=5:duration=10- the trim filter is quite simple, start at
5 secondsand trim till a duration of10 seconds. Note you can use:to chain multiple options within the same filter.
- the trim filter is quite simple, start at
atrim- as you might have guessed,
atrimis an audio filter that works just liketrimbut on audio streams.
- as you might have guessed,
setpts & asetpts- these filters set the timestamps on the output stream (for video and audio streams respectively). Here, we are telling ffmpeg to start from the same point so that we have smooth playback.
[v0][a0][v1][a1]concat=n=2:v=1:a=1[final]- The trailing
[v0],[a0],[v1],[a1]in each filter line are names assigned to each filter stream. - This final line can be read as: take inputs
video0,audio0,video1,audio1and concat them(n=2 segments)and output 1 video streamv=1and 1 audio streama=1and call the output stream as final[final].
- The trailing
Bonus - concat 3 different inputs#
With our understanding of filters and naming, we can extend this idea to work with 3 files like this:
Related reading#
- Concat filter: https://ffmpeg.org/ffmpeg-filters.html#concat
- Trim and ATrim filters: https://ffmpeg.org/ffmpeg-filters.html#trim https://ffmpeg.org/ffmpeg-filters.html#atrim
- How ffmpeg "Map" works: https://trac.ffmpeg.org/wiki/Map
Simplify your video pipelineTry MediaMachine today!
Get started for free →Get access to one of the cheapest Cloud-Transcode pipelines
Engage users early with great Thumbnails and NLP-Like Video summaries
No credit card required