Advertisement

ffmpeg cut video or audio with or without re-encoding

top computer
You only need the -ss and -t options. -ss is the starttime and -t the duration, so if you want 10 seconds from a video starting from minute one, you would use this.
1
ffmpeg -i INFILE.mp4 -vcodec copy -acodec copy -ss 00:01:00.000 -t 00:00:10.000 OUTFILE.mp4
You can use seconds or hh:mm:ss[.xxx] as arguments for the start time and duration, i prefer the second option.
The options -vcodec copy and -acodec copy are used to only cut the video and disable the re-encoding, this really speed things up.😉
I use this command
ffmpeg -i INFILE.mp4 -vcodec copy -acodec copy -ss 00:01:00.000 -t 00:00:10.000 OUTFILE.mp4

ffmpeg -i "NON STOP CHRISTMAS MEDLEY VOLUME 1 & 2-RFTYZgBNprg.mp3" -vcodec copy -acodec copy -ss 00:00:10.000 -t 01:00:00.000 "NON STOP CHRISTMAS MEDLEY VOLUME 1 & 2-1 hours.mp3"

without re-encoding

Install ffmpeg

Make sure you download a recent version of ffmpeg, and don't use the one that comes with your distribution (e.g. Ubuntu). Packaged versions from various distributions are often outdated and do not behave as expected.

How to cut a video, without re-encoding

Use this to cut video from [start] for [duration]:
ffmpeg -ss [start] -i in.mp4 -t [duration] -c copy out.mp4
Here, the options mean the following:
  • -ss specifies the start time, e.g. 00:01:23.000 or 83 (in seconds)
  • -t specifies the duration of the clip (same format).
  • Recent ffmpeg also has a flag to supply the end time with -to.
  • -c copy copies the first video, audio, and subtitle bitstream from the input to the output file without re-encoding them. This won't harm the quality and make the command run within seconds.
Note that with older ffmpeg versions, this command may not be frame-accurate when copying the bitstreams.

How to cut a video, without re-encoding — accurate method

If you have an old ffmpeg version and you want frame accuracy, use this instead:
ffmpeg -i in.mp4 -ss [start] -t [duration] -c copy out.mp4
However, there's one drawback. The video will have to be decoded until it reaches the position implied by -ss, and only then it starts copying the bitstream. This can take a long time, especially for really long videos.

How to cut a video, with re-encoding

If you leave out the -c copy option, ffmpeg will automatically re-encode the output video and audio according to the format you chose. For high quality video and audio, read the x264 Encoding Guide and the AAC Encoding Guide, respectively.
For example:
ffmpeg -ss [start] -i in.mp4 -t [duration] -c:v libx264 -c:a aac -strict experimental -b:a 128k out.mp4
As explained by Seeking with FFmpeg): When using -ss before -i on FFmpeg 2.1 and newer, seeking is frame-accurate when re-encoding the video (i.e., not using -c copy). So you don't have to worry about putting -ss after -i for speed.

Converting end points to duration

If you need to use an old version of ffmpeg and can't use the -to option, you can create an edit list of in and out points, and convert these into in points with duration. For this you can use this Ruby script I wrote, which calculates the difference:
require "Time"
def time_diff(time1_str, time2_str)
  t = Time.at( Time.parse(time2_str) - Time.parse(time1_str) )
  (t - t.gmt_offset).strftime("%H:%M:%S.%L")
end
For example:
time_diff("00:09:23.000", "00:25:33.000")
=> "00:16:10.000" 
shareimprove this answer
1
On ubuntu 12:10, -c:v and -c:a didn't work. I had to use "-acodec copy and -vcodec copy" instead. â€“ Samuel Lampa Apr 2 '13 at 14:21
1
@Samuel Then you're using an outdated version, which isn't even real FFmpeg, but the Libav fork. See: stackoverflow.com/a/9477756/1109017 â€“ slhck Apr 2 '13 at 14:45
  
Thanks for this! if you don't specify a duration, will it go to the end? â€“ Jeff Mar 31 '14 at 17:03
  
@Jeff Yes! (character minimum) â€“ slhck Apr 1 '14 at 5:34
1
@Prateek When copying the video bitstream (instead of re-encoding), you can only cut at keyframes. If there are keyframes at seconds 1, 2, and 3, but you want to cut at second 1.5, then ffmpeg will wait until second 2 before it can start cutting. That makes it inaccurate. â€“ slhck Jul 3 '15 at 12:36

 
I think you can use the following command now.
ffmpeg -i inputFile -vcodec copy -acodec copy -ss 00:09:23 -to 00:25:33 outputFile
Have a look also ffmpeg Doc, or this wiki page.
shareimprove this answer
1
ffmpeg -i inputFile -c copy -ss 00:09:23 -to 00:25:33 outputFile simpler version â€“ wiak Nov 20 at 16:37
There are two ways how to split video files by ffmpeg. The first one is good in itself, more than that - it is faster, but sometimes creates output files with certain flaws. So for those cases there is the second way of splitting video files: it is considerably slower, the output files are bigger, but it seems they are always of the same quality level as input files used.
Way 1:
ffmpeg -ss <start> -i in1.avi -t <duration> -c copy out1.avi
Way 2:
ffmpeg -ss <start> -i in1.avi -t <duration> out1.avi
  • <start> â€“ the beginning of the part of a video ffmpeg is to cut out. Format: 00:00:00.0000, meaning hours:minutes:seconds:milliseconds.
  • <duration> â€“ the duration of the part of a video ffmpeg is to cut out. Same format as above.
Examples:
ffmpeg -ss 01:19:00 -i in1.avi -t 00:05:00 -c copy out1.avi
ffmpeg -ss 01:19:00 -i in1.avi -t 00:05:00 out1.avi
ffmpeg cuts out a part of the video file starting from 1 hour 19 minutes 0 seconds. The duration of the video sequence cut out is 5 minutes 0 seconds.
shareimprove this answer
  
Ok, thnx. Will check out the docs and give it a try tomorrow. â€“ juGGaKNot Oct 3 '13 at 19:18
3
-sameq does not mean "same quality" and has been removed from ffmpeg. Use -codec copy instead. Also, don't forget about the -to option. â€“ LordNeckbeard Oct 3 '13 at 20:47 
  
I corrected the commands in your quote, as they were quite outdated. Please don't copy-paste information without properly quoting it. â€“ slhck Oct 4 '13 at 10:08 
This is odd that no-one suggested the trim filter.
Drop everything except the second minute of input:
ffmpeg -i INPUT -vf trim=60:120
Keep only the first second:
ffmpeg -i INPUT -vf trim=duration=1
Drop everything except from second 13 to second 58:
ffmpeg -i INPUT -vf trim=13:58 OUTPUT
shareimprove this answer
1
Yes, good to mention trim and atrim. I usually use these when filtering, but users should note that filtering requires re-encoding. â€“ LordNeckbeard Apr 7 '15 at 17:28
You can use this
ffmpeg -sameq -ss clip_start -t duration  -i  original.ogg  clip.ogg
Here you have to give duration of video. ie. 00:25:33-00:09:23
shareimprove this answer
  
As I mentioned on the other answer: With your command, you're re-encoding the file, which is absolutely not necessary and results in reduced quality and increased processing time. â€“ slhck Jan 11 '12 at 21:18 
I use the following syntax to cut video with ffmpef:
ffmpeg -sameq -ss [start_seconds] -t [duration_seconds] -i [input_file] [outputfile]
-t is used to set the duration in seconds - you can't specify the end time but this should work for you.