133

If I have a video, normally avi, but could be any, and I want to create another one in the same format but just a part, the position i seconds to f seconds into the video, what is the one-line command to do this?

4 Answers 4

167

This can be done using mencoder or ffmpeg.

mencoder

Say that you want to cut out a part starting at 00:00:30 into the original file with a 5 seconds length and that it shouldn't be reencoded (if you want to re-encode it you'll have to replace copy with audio and video codecs, for a list of available audio codecs issue mencoder -oac help, for a list of available video codecs issue mencoder -ovc help), then you issue:

mencoder -ss 00:30:00 -endpos 00:00:05 -oac pcm -ovc copy originalfile -o newfile

You'll have to replace orginalfile and newfile with actual file names, the latter is the name of the file that is created to hold the cut-out part.

ffmpeg

Say that you want to cut out a part starting at 00:00:30 into the original file with a 5 seconds length and that it shouldn't be re-encoded (if you want to re-encode it you'll have to replace copy with audio and video codecs, for a list of available codecs issue ffmpeg -formats -E), then you issue:

ffmpeg -ss 00:00:30 -i orginalfile -t 00:00:05 -vcodec copy -acodec copy newfile

You'll have to replace orginalfile and newfile with actual file names, the later is the name of the file that is created to hold the cut out part.


For reference see http://lazyxiong-tech.blogspot.com/2007/05/using-mencoder-to-cut-out-pieces-of.html and "7. Copy Only A Part Of Video" in http://segfault.in/2010/10/ffmpeg-tricks-you-should-know-about/

10
  • I tried it and failed. Can you show me what the command would be for you with your typical codecs to use for avi, flv and mpg? I listed the codecs but couldn't make much sense of it. Sep 1, 2011 at 5:07
  • @strapakowsky It depends on the input file and what codecs you have installed. Maybe you should make a new question for that and add information about what file you've got and what command you tried.
    – N.N.
    Sep 1, 2011 at 6:36
  • @strapakowsky I cannot get it to work with ffmpeg either but all the guides on cutting out parts with ffmpeg suggests that it should work somehow so I keep it in my answer. However, I've also added a mencoder line which I get to work.
    – N.N.
    Sep 1, 2011 at 8:35
  • Just used a version of this. It's not working for you because -ss ... -t ... needs to be before the -i infile option. See this answer for the real one-liner. You almost got it, just not the order...
    – Hendy
    Nov 20, 2012 at 22:29
  • 1
    @Hendy Edited accordingly
    – N.N.
    Nov 21, 2012 at 5:50
43

Do you need to cut video with re-encoding or without re-encoding mode? You can try to following below command.

Synopsis: ffmpeg -i [input_file] -ss [start_seconds] -t [duration_seconds] [output_file]

use FFmpeg cut mp4 video without re-encoding

Example:
ffmpeg -i source.mp4 -ss 00:00:05 -t 00:00:10 -c copy cut_video.mp4

use FFmpeg cut mp4 video with re-encoding

Example:
ffmpeg -i source.mp4 -ss 00:00:05 -t 00:00:10 -async 1 -strict -2 cut_video.mp4

If you want to cut off section from the beginning, simply drop -t 00:00:10 from the command

3
  • 2
    Something to consider: "Using -ss as input option together with -c:v copy might not be accurate since ffmpeg is forced to only use/split on i-frames. Though it will—if possible—adjust the start time of the stream to a negative value to compensate for that. Basically, if you specify "second 157" and there is no key frame until second 159, it will include two seconds of audio (with no video) at the start, then will start from the first key frame. So be careful when splitting and doing codec copy." trac.ffmpeg.org/wiki/Seeking I got more accurate cutting by leaving out -c copy
    – joe
    Jan 17, 2020 at 18:33
  • Why is -async 1 -strict -2 required? Seems to work fine without them. Is this tweet relevant? twitter.com/ffmpeg/status/943266612047577089
    – joe
    Jan 17, 2020 at 18:35
  • What does "cut" mean? Cutting and leaving a "hole" in the original file or simply copying a section from the original file?
    – chx101
    Jul 12, 2022 at 23:56
0

medipack is a very simple command-line app as a wrapper over ffmpeg.

you can achieve trimming your video using these commands:

medipack trim input.mp4 -s 01:04 -e 14:08 -o output.mp4
medipack trim input.mp4 -s 01:04 -t 13:04 -o output.mp4

you can view options of trim subcommand as:

srb@srb-pc:$ medipack trim -h
usage: medipack trim [-h] [-s START] [-e END | -t TIME] [-o OUTPUT] [inp]

positional arguments:
  inp                   input video file ex: input.mp4

optional arguments:
  -h, --help            show this help message and exit
  -s START, --start START
                        start time for cuting in format hh:mm:ss or mm:ss
  -e END, --end END     end time for cuting in format hh:mm:ss or mm:ss
  -t TIME, --time TIME  clip duration in format hh:mm:ss or mm:ss
  -o OUTPUT, --output OUTPUT

you could also explore other options using medipack -h

srb@srb-pc:$ medipack --help
usage: medipack.py [-h] [-v] {trim,crop,resize,extract} ...

positional arguments:
  {trim,crop,resize,extract}

optional arguments:
  -h, --help            show this help message and exit
  -v, --version         Display version number

you may visit https://github.com/srbcheema1/medipack and checkout examples in README.

-1

To be more precise you can: MANUALLY Open the file in a media player that will frame by frame advance (potplayer my favorite but MPC-HC works)and play an AVISynth file with data such as:

DirectShowSource(("C:\Downloads\Video\Do you want him.flv"), Pixel_Type="yuy2").Crop(0,0,-0,-0)
Subtitle("C:\Downloads\Video\Do you want him.flv", font="Arial", size=24, text_color=$ff0000, align=3)
ShowFrameNumber(scroll=true, x=336, y=27, font="Arial", size=24, text_color=$ff0000)

ShowTime(x=398, y=44, font="Arial", size=24, text_color=$ff0000)

Then cut with the EXACT time format:

ffmpeg -i "Path\do you want him.flv" -ss 00:00:05.240 -to 00:00:08.360 -vcodec libx264 -acodec libvo_aacenc "Path\Do you want him1.flv"

and

ffmpeg -i "Path\do you want him.flv" -ss 00:00:10.240 -to 00:00:14.360 -vcodec libx264 -acodec libvo_aacenc "Path\Do you want him2.flv"

If you wish to join, make a txt file with the video files with contents like:

file 'C:\Downloads\Video\Do you want him1.flv'

file 'C:\Downloads\Video\Do you want him2.flv'

Run ffmpeg:

ffmpeg -f concat -i FileList.txt -c copy "Path\NewName_joined.flv"

PROGRAMMATICALLY I wrote a program but have no level 10 to show it, sorry

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.