5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
IanDess
  • 657
  • 2
  • 11
  • 26
  • Do you intend to also filter the audio eventually? – llogan Nov 22 '17 at 18:57
  • nope, i'm combining the video with a couple of still images, but i want to preserve the video's original audio (from the trimmed part of course, so audio from 5th to 10th seconds in the example above) – IanDess Nov 22 '17 at 18:59

2 Answers2

18

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
king flight
  • 180
  • 2
  • 7
  • I need to add that if you're running this command on Win and you're using this as a reference, don't use a single quote around the filter complex ['] use a double quote ["] or else you'll be yelling at your screen trying to understand why it's not working. Also for windows users don't use backslash [\\] as shown here. It's a way in linux to make long commands look cleaner. – julesverne May 05 '20 at 04:07
  • 2
    The last `;` in the filter description shouldn't be there, cause FFmpeg thinks that there will be another filter: `[AVFilterGraph @ 0x7fcc3ec2aa80] No such filter: ''` – Gleb Ignatev Oct 17 '20 at 09:45
4

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
llogan
  • 121,796
  • 28
  • 232
  • 243