Using a FFmpeg trim filter to trim a video, while preserving its audio (with filter_complex)

Viewed 8283

I reached a part where I can cut the video with

ffmpeg -i test.mp4 -filter_complex
'[0:v] trim=start=5:end=10,setpts=PTS-STARTPTS [cut]' -map [cut]  output.mp4

And it successfully trims the video. However, it completely removes the audio. I'm trying to chain a couple of different filters, so I'd like to keep the similar syntax, just to somehow preserve the audio.

2 Answers

You can also use the atrim filter to trim the audio and the asetpts filter to modify the audio timestamp:

ffmpeg -i test.mp4 -filter_complex \
'[0:v] trim=start=5:end=10, setpts=PTS-STARTPTS [v0]; \
 [0:a]atrim=start=5:end=10,asetpts=PTS-STARTPTS [a0]'\
 -map [v0] -map [a0]  output.mp4

A simple method for audio is to declare input twice and use -ss & -t, and then map this stream:

ffmpeg -i test.mp4 -ss 5 -t 5 -i test.mp4 -filter_complex 
'[0:v] trim=start=5:end=10,setpts=PTS-STARTPTS [cut]' -map [cut] -map 1:a -c:a copy output.mp4
Related