Ubuntu – How to remove a few seconds from .mp4 file using ffmpeg

ffmpeg

How to remove a few seconds from .mp4 file using ffmpeg? I have test.mp4 file and I want to remove 10s from 11:00 to 11:10

Best Answer

Fast but possibly not accurate

This avoids re-encoding, but you can only cut on key frames, so it may not choose the duration you want.

  1. Create segments:

    ffmpeg  -t 00:11:00 -i input.mp4 -map 0 -c copy segment1.mp4
    ffmpeg -ss 00:11:10 -i input.mp4 -map 0 -c copy segment2.mp4
    
  2. Create input.txt:

    file 'segment1.mp4'
    file 'segment2.mp4'
    
  3. Concatenate with the concat demuxer:

    ffmpeg -f concat -i input.txt -map 0 -c copy output.mp4
    

Slow but accurate

You can try to do this without re-encoding, but since you can only cut on keyframes it will possibly not align with your desired cut times, so if you need accuracy you'll need to re-encode:

ffmpeg -i input.mp4 -filter_complex \
  "[0:v]trim=end=660,setpts=N/FRAME_RATE/TB[v0]; \
   [0:a]atrim=end=660,asetpts=N/SR/TB[a0]; \
   [0:v]trim=start=670,setpts=N/FRAME_RATE/TB[v1]; \
   [0:a]atrim=start=670,asetpts=N/SR/TB[a1]; \
   [v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]" \
  -map "[v]" -map "[a]" output.mp4
  • (a)trim will allow you to choose your clips.
  • (a)setpts resets timestamps.
  • concat concatenates the clips.

See FFmpeg Filter Documentation for details on each filter.