theapache64
theapache64

Reputation: 11734

Why the process time increase as the trim position increases?

I've been trimming some videos recently using ffmpeg, and I've noticed that as the trim position increases, the time taken to trim the video also increases.Even if the duration is same. (5 seconds)

enter image description here

Below given the command to trim the video from 0 to 5 seconds, and it takes only 1 second to process.

ffmpeg -y \
    -i input.mp4 \
    -filter_complex \
        "[0:v]trim=0:5,setpts=PTS-STARTPTS[v0]; 
     [0:a]atrim=0:5, asetpts=PTS-STARTPTS[a0] 
" -map "[v0]" -map "[a0]" output.mp4

But when I try to trim the video with exact same command, but with different index, from 300 second to 305, it takes 5 seconds.

ffmpeg -y \
    -i input.mp4 \
    -filter_complex \
        "[0:v]trim=300:305,setpts=PTS-STARTPTS[v0]; 
     [0:a]atrim=300:305, asetpts=PTS-STARTPTS[a0] 
" -map "[v0]" -map "[a0]" output.mp4

So here are my questions

  1. Why the process time increase as the position increases?
  2. Is there any way to fix this ?

Upvotes: 0

Views: 384

Answers (1)

Gyan
Gyan

Reputation: 92928

You are trimming the streams using filters, so all frames from the start of the file till the trim points still have to be decoded in order to be trimmed.

For MP4 files, perform an input seek, which will only pull frames from the closest keyframe. No need to use filters either since all trimming and timestamp reset happens via input switches.

ffmpeg -y -ss 300 -to 305 -i input.mp4 -map 0:v -map 0:a output.mp4

Upvotes: 1

Related Questions