54

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
1
  • So which do you prefer; the mencoder example, above; or the ffmpeg example, below?
    – voices
    Apr 28, 2017 at 0:10

5 Answers 5

61

Try:

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

I think the input parameter is supposed to be first.

4
  • 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.
    – Marcel Ray
    Apr 12, 2011 at 17:05
  • 1
    This works for mp3's too
    – geotheory
    Jul 11, 2015 at 16:00
  • 11
    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. Aug 8, 2015 at 23:08
  • 1
    Moving the -ss 2 arg before -i input.flv helps fix time coding issues for other codecs like h264 quicktime mp4s Oct 27, 2023 at 20:43
30

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.

3
  • 4
    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
    Oct 25, 2013 at 4:54
  • 3
    I 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). Mar 25, 2016 at 22:50
  • 1
    Daves answer is the correct one as of the latest version of ffmpeg. The -i should come before the -ss.
    – Arete
    Jan 5, 2019 at 21:25
12

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

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

It sounds like there might be additional codec parameters required for video, but this worked to trim the first 5s off an mp3 file for me (similar to Bora Savkars Answer, though his solution output the part I wanted to trim instead of the file I wanted, minus the first 5s)

ffmpeg -ss 00:00:05 -i input -c copy output

https://ffmpeg.org/ffmpeg.html

0

Using -vcodec copy was also causing me to have a poorly functioning video at the point of the trim.

ffmpeg -i nick.mp4 -ss 52 -vcodec libx264 0 -acodec copy nick4.mp4

Is what I was able to use to accomplish a properly working video trimmed where I wanted it. (Thanks to Karl Wilbur for the hint in one of the comments to an answer)

1
  • this solved the issue for me where cropping would mess up (probably) the keyframes, leading to broken video. this obviously takes a longer time since the whole video is re-encoded, so it should be noted that the runtime increases and video quality will degrade due to transcoding
    – pzkpfw
    Mar 26 at 8:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .