Is it possible to use ffmpeg to trim off X seconds from the beginning of a video with an unspecified length?

21

10

I need to trim the just the first 1 or 2 seconds off of a series of FLV recordings of varying, unspecified lengths. I've found plenty of resources for extracting a specified duration from a video (e.g. 30 second clips), but none for continuing to the end of a video.

Both of these attempts just yield a copied version of the video, sans desired trimming:

ffmpeg -ss 2 -vcodec copy -acodec copy -i input.flv output.flv

ffmpeg -ss 2 -t 120 -vcodec copy -acodec copy -i input.flv output.flv

The thought on the second one was: perhaps if I specified a length beyond what was possible, it'd just go to the end. No dice.

I know it's not an issue with codecs or using seconds instead of timecode since the following worked a charm:

ffmpeg -ss 2 -t 5 -vcodec copy -acodec copy -i input.flv output.flv

Any other ideas? I'm open to using other (Windows-based) command line tools, however am strongly favoring ffmpeg since I'm already using it for thumbnail creation and am familiar with it.

If it helps, my videos will all be under 2 minutes.

UPDATE:

I've switched over to using Mencoder (http://www.mplayerhq.hu/) since it looks like ffmpeg won't accomplish this without some additional hackery.

The Mencoder syntax to accomplish what I set out to do is:

mencoder.exe -ss 2 -oac copy -ovc copy input.flv -o output.flv

marcelebrate

Posted 2011-03-15T20:42:58.843

Reputation: 551

So which do you prefer; the mencoder example, above; or the ffmpeg example, below? – voices – 2017-04-28T00:10:54.080

Answers

17

Try:

ffmpeg -i input.flv -ss 2 -vcodec copy -acodec copy output.flv

I think the input parameter is supposed to be first.

Dave

Posted 2011-03-15T20:42:58.843

Reputation: 318

This works for mp3's too – geotheory – 2015-07-11T16:00:23.470

4

As explained in FFmpeg Seeking, using -ss as an input parameter (before -i) while copying, or even transcoding with an older version of FFmpeg, employs keyframe seeking. This is much faster, but sacrifices accuracy. If you use it as an output parameter, the process is potentially much slower (the input is decoded frame by frame even for the discarded parts), but accurate. Even though the question is old, I think this should be the accepted answer.

– Jonathan Y. – 2015-08-08T23:08:00.387

Thanks for the response. The parameter ordering I used above provides the same result, so I don't think it's the order. After giving up on ffmpeg, my keyframe interval was causing issues with other tools. After revisiting this just now, it was the keyframe interval after all. It was set to 5 seconds before and I've since set it to 1 second. At 5 seconds, both your command and the ones I tried above yielded black space at the beginning (3 seconds worth, unsurprisingly). At 1 second, it works as expected. – marcelebrate – 2011-04-12T17:05:09.940

23

Turns out this command will trim the video from 2 seconds on, as expected:

ffmpeg -i input.flv -ss 2 -vcodec copy -acodec copy output.flv

The issue was with the keyframe interval in input.flv. It was set to 5 seconds, which yielded 3 seconds worth of black frames at the beginning of the video (5 - 2 = 3). I've since changed my keyframe interval to 1 second, though 2 seconds would probably also yield the desired results in my case.

UPDATE: Updated ordering of -i and -ss parameters per Dave's answer, which I've accepted for credit.

marcelebrate

Posted 2011-03-15T20:42:58.843

Reputation: 551

2I just encountered this same thing. I discovered that I cannot just -vcodec copy because the slice doesn't start on a keyframe. I was getting a still image or black image for N seconds until the next keyframe. If I actually re-encode, rather than copy, it works as expected. So even though my input and output are both H264, I still needed to re-encode (using -vcodec libx264 instead of -vcodec copy). – Karl Wilbur – 2016-03-25T22:50:54.807

Daves answer is the correct one as of the latest version of ffmpeg. The -i should come before the -ss. – Arete – 2019-01-05T21:25:16.420

2

I was having this issue as well. This post did it for me. I bit the bullet and stopped trying to use -vcodec copy -acodec copy for all my editing commands. Re-encoding prevented the black frames from the beginning of my video or not cutting at the right spot (the difference of a second without re-encoding was translating to almost a 5 second difference in the video). So, I ended up replacing -vcodec copy with -vcodec libx264 -crf 0.

– Hendy – 2013-10-25T04:54:32.313

0

Use -ss and -to before -i like this:

ffmpeg -ss 00:01:10 -to 00:02:10 -i input -c copy output

Bora Savkar

Posted 2011-03-15T20:42:58.843

Reputation: 1

0

Updating this answer, as of 2019 the command is now:

ffmpeg -i input.flv -ss 2 -c copy output.flv

Matt Zabojnik

Posted 2011-03-15T20:42:58.843

Reputation: 123