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
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/
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
- 463
- 4
- 6
- 559
-
2Something 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." https://trac.ffmpeg.org/wiki/Seeking I got more accurate cutting by leaving out
-c copy– joe Jan 17 '20 at 18:33 -
Why is
-async 1 -strict -2required? Seems to work fine without them. Is this tweet relevant? https://twitter.com/ffmpeg/status/943266612047577089 – joe Jan 17 '20 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 '22 at 23:56
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.
- 101
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
-ss ... -t ...needs to be before the-i infileoption. See this answer for the real one-liner. You almost got it, just not the order... – Hendy Nov 20 '12 at 22:29-tflag must be after the-ioption. Source: http://superuser.com/questions/138331/using-ffmpeg-to-cut-up-video – Marco Sulla Aug 25 '15 at 21:44avconvthe -t option is not necessary if you want the start point to end of video – Antonios Hadjigeorgalis Oct 18 '16 at 23:33mplayer-2:1.2.1-1ubuntu1.1, but ffmpeg-7:2.8.15-0ubuntu0.16.04.1 worked like a charm: thanks! – Tomislav Nakic-Alfirevic Jun 05 '20 at 15:48ffmpeg -ss 00:00:30 -i orginalfile.mp4 -t 00:00:05 -vcodec copy -acodec copy newfile.mp4– Salma Gomaa Jan 25 '21 at 20:35