[FFmpeg-user] fast trimming of short videos

Gal Kahana gal.kahana at xmpie.com
Tue Mar 18 15:27:47 CET 2014


I am looking for a method to use ffmpeg for trimming little of the beginning and little of the end of videos. for mp4 files

One can normally do this, to achieve trimming:

ffmpeg -y -i myInput.mp4 -ss 4 -t 4 -c copy myOutput.mp4

However, i to trim about 2 frames or less than half a second. doing something like this:

ffmpeg -y -i myInput.mp4 -ss 0.5 -t 0.5 -c copy myOutput.mp4

will trim more than half a second (at least from start) because 0.5 a second is less than the GOP of my vids. (normally it's about 1.4 GOP).

so i have to re-encode. so the following works:

ffmpeg -y -i myInput.mp4 -ss 0.5 -t 0.5 myOutput.mp4

note that copying is avoided.

This takes time. i want to speed this up. figured i can do so be encoding only the edges using the folloing process:

1. use the segment muxer to generate segments, using segment_times with [1.5,totalvidduration-1.5]. This should create 3 parts - short start, long middle, short end

2. trim start while encoding on the beginning segment, trim end while encoding on the end segment

3. concat using the concat demuxer going through ts files.

Implementing this i found that i need to correct the timestamps of the ts files to get an mp4 that works.

the mp4 will work on my vlc, but not on my PC media player. i also try to run the trimmed converted parts (ts) via an hls player, to verify them, and there is some skipping [maybe that last bit is unimportant].

i'm wondering if you can see where the algorithm may be fault, or whether there are other methods that may prove to be better. i can think of many options where this can go wrong... like maybe concating encoded and not encoded parts ain't such a good idea. If my algoirhtm can be corrected by using specific ffmpeg options i'd like to know as well.



More information about the ffmpeg-user mailing list