使用ffmpeg根据开始和结束时间剪切视频

本文翻译自: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 . 通过使用以上命令,我想将视频从00:00:0300:00:08 But it is not cutting the video between those times instead of that it is cutting the video with first 11 seconds. 但是,这并不是在这段时间之间剪切视频,而是在前11秒内剪切视频。 can anyone help me how resolve this? 谁能帮我解决这个问题?

Edit 1: 编辑1:

I have tried to cut by using the following command which is suggested by mark4o 我试图通过使用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 编码器“ aac”是实验性的,但未启用实验性编解码器

so i added the -strict -2 into the command ie, 所以我在命令中添加-strict -2

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

Now it is working fine. 现在工作正常。


#1楼

参考:https://stackoom.com/question/1FOB0/使用ffmpeg根据开始和结束时间剪切视频


#2楼

You probably do not have a keyframe at the 3 second mark. 您可能在3秒标记处没有关键帧。 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. 使用mp4容器,可以在非关键帧处进行剪切,而无需使用编辑列表进行重新编码。 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. 换句话说,如果最接近3s的关键帧位于0s,则它将复制从0s开始的视频,并使用编辑列表告诉播放器开始播放3秒。

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. 如果您使用的是git master中的最新ffmpeg ,则在使用提供的命令调用该列表时,将使用编辑列表来执行此操作。 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. 如果这不适合您,则可能是您使用的是较旧版本的ffmpeg,或者您的播放器不支持编辑列表。 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 . 如果不指定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. 重新编码时,您可能还希望包括其他与质量相关的选项或特定的AAC编码器。 For details, see ffmpeg's x264 Encoding Guide for video and AAC Encoding Guide for audio. 有关详细信息,请参阅ffmpeg的x264视频编码指南AAC音频编码指南

Also, the -t option specifies a duration, not an end time. 另外, -t选项指定持续时间,而不是结束时间。 The above command will encode 8s of video starting at 3s. 上面的命令将从3s开始对8s的视频进行编码。 To start at 3s and end at 8s use -t 5 . 要以3s开始并以8s结束,请使用-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. 如果您使用的是ffmpeg的当前版本,则还可以在上述命令中将-t替换为-to在指定时间结束。


#3楼

ffmpeg -i movie.mp4 -vf trim=3:8 cut.mp4

将所有内容从秒3减至秒8。


#4楼

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

Use -c copy for make in instantly. 使用-c复制即可即时制作。 In that case ffmpeg will not re-encode video, just will cut to according size. 在这种情况下,ffmpeg不会重新编码视频,只会切成适当的大小。


#5楼

Try using this. 尝试使用这个。 It is the fastest and best ffmpeg-way I have figure it out: 这是我弄清楚的最快最好的ffmpeg方式:

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

This command trims your video in seconds! 此命令可在几秒钟内修剪您的视频!

I have explained it on my blog here : 我已经解释它在我的博客在这里

-i: This specifies the input file. -i:这指定输入文件。 In that case, it is (input.mp4). 在这种情况下,它是(input.mp4)。
-ss: Used with -i, this seeks in the input file (input.mp4) to position. -ss:与-i一起使用,它在输入文件(input.mp4)中查找位置。
00:01:00: This is the time your trimmed video will start with. 00:01:00:这是您修剪的视频开始的时间。
-to: This specifies duration from start (00:01:40) to end (00:02:12). -至:这指定从开始(00:01:40)到结束(00:02:12)的持续时间。
00:02:00: This is the time your trimmed video will end with. 00:02:00:这是您修剪的视频结束的时间。
-c copy: This is an option to trim via stream copy. -c复制:这是通过流复制修剪的选项。 (NB: Very fast) (注:非常快)

The timing format is: hh:mm:ss 计时格式为: 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 . 有关更多信息,请参见ffmpeg官方文章


#6楼

To cut based on start and end time from the source video and avoid having to do math, specify the end time as the input option and the start time as the output option. 要基于源视频的开始时间和结束时间进行剪切并避免进行数学运算,请指定结束时间作为输入选项,并指定开始时间作为输出选项。

ffmpeg -t 1:00 -i input.mpg -ss 45 output.mpg

This will produce a 15 second cut from 0:45 to 1:00. 这将在0:45到1:00之间产生15秒的切换。

This is because when -ss is given as an output option, the discarded time is still included in the total time read from the input, which -t uses to know when to stop. 这是因为当-ss作为输出选项给出时,丢弃的时间仍包含在从输入中读取的总时间中, -t用于了解何时停止。 Whereas if -ss is given as an input option, the start time is seeked and not counted , which is where the confusion comes from. 而如果将-ss作为输入选项,则会查找开始时间, 而不计算开始时间,这是造成混乱的原因。

It's slower than seeking since the omitted segment is still processed before being discarded, but this is the only way to do it as far as I know. 这比查找要慢,因为省略的段在丢弃之前仍会进行处理,但是据我所知,这是唯一的方法。 If you're clipping from deep within a large file, it's more prudent to just do the math and use -ss for the input. 如果要从一个大文件中深切,则最好进行数学运算,并使用-ss作为输入。

(0)

相关推荐