Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cutting the videos based on start and end time using ffmpeg

I tried to cut the video using the start and end time of the video by using the following command

ffmpeg -ss 00:00:03 -t 00:00:08 -i movie.mp4 -acodec copy -vcodec copy -async 1 cut.mp4 

By using the above command i want to cut the video from 00:00:03 to 00:00:08. But it is not cutting the video between those times instead of that it is cutting the video with first 11 seconds. can anyone help me how resolve this?

Edit 1:

I have tried to cut by using the following command which is suggested by mark4o

ffmpeg -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 cut.mp4 

But it was shown the following error.

the encoder 'aac' is experimental but experimental codecs are not enabled

so i added the -strict -2 into the command i.e.,

ffmpeg -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 -strict -2 cut.mp4 

Now it is working fine.

like image 671
Kalai Avatar asked Aug 26 '13 12:08

Kalai


People also ask

How do I set video duration in FFmpeg?

Getting the Duration To get the duration with ffprobe , add the -show_entries flag and set its value to format=duration . This tells ffprobe to only return the duration. To convert the above value into minutes and seconds, round it to an integer and divide it by 60.

Can I trim video without re encoding?

To be able to Losslessly Trim without encoding you will need to choose the function "Cut without re-encoding". Then import your footage, choose In and Out points and click on "Start Function". Note that it can work on codecs such as H. 264 by cutting on the nearest keyframe.


2 Answers

You probably do not have a keyframe at the 3 second mark. Because non-keyframes encode differences from other frames, they require all of the data starting with the previous keyframe.

With the mp4 container it is possible to cut at a non-keyframe without re-encoding using an edit list. In other words, if the closest keyframe before 3s is at 0s then it will copy the video starting at 0s and use an edit list to tell the player to start playing 3 seconds in.

If you are using the latest ffmpeg from git master it will do this using an edit list when invoked using the command that you provided. If this is not working for you then you are probably either using an older version of ffmpeg, or your player does not support edit lists. Some players will ignore the edit list and always play all of the media in the file from beginning to end.

If you want to cut precisely starting at a non-keyframe and want it to play starting at the desired point on a player that does not support edit lists, or want to ensure that the cut portion is not actually in the output file (for example if it contains confidential information), then you can do that by re-encoding so that there will be a keyframe precisely at the desired start time. Re-encoding is the default if you do not specify copy. For example:

ffmpeg -i movie.mp4 -ss 00:00:03 -t 00:00:08 -async 1 cut.mp4 

When re-encoding you may also wish to include additional quality-related options or a particular AAC encoder. For details, see ffmpeg's x264 Encoding Guide for video and AAC Encoding Guide for audio.

Also, the -t option specifies a duration, not an end time. The above command will encode 8s of video starting at 3s. To start at 3s and end at 8s use -t 5. If you are using a current version of ffmpeg you can also replace -t with -to in the above command to end at the specified time.

like image 26
mark4o Avatar answered Oct 01 '22 02:10

mark4o


Try using this. It is the fastest and best ffmpeg-way I have figure it out:

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

This command trims your video in seconds!

Explanation of the command:

-i: This specifies the input file. In that case, it is (input.mp4).
-ss: Used with -i, this seeks in the input file (input.mp4) to position.
00:01:00: This is the time your trimmed video will start with.
-to: This specifies duration from start (00:01:40) to end (00:02:12).
00:02:00: This is the time your trimmed video will end with.
-c copy: This is an option to trim via stream copy. (NB: Very fast)

The timing format is: hh:mm:ss

Please note that the current highly upvoted answer is outdated and the trim would be extremely slow. For more information, look at this official ffmpeg article.

like image 152
My Name Avatar answered Oct 01 '22 01:10

My Name