FFmpeg video cropping and merging

Source: Internet
Author: User

Here, cropping refers to timeline cropping, not space cropping.

 

For example, you want to crop the video from 1 minute 20 seconds to 30 seconds and save it as a video. This is the question to be discussed in this article.

 

IVideo Cropping

 

FFmpeg provides simple command parameters:

FFmpeg-SS start-T duration-I input-vcodec copy-acodec copy output

The preceding command is explained in a bit.

-SS start time, for example, 00:00:20, which indicates that the time starts from 20 seconds;

-T duration, for example, 00:00:10, indicates a video clip of 10 seconds;

-I: enter a space followed by the input video file;

-Vcodec copy and-acodec copy indicate the encoding formats of the video and audio to be used. Here, copy indicates the original copy;

Input: Enter the video file;

Output: output the video file;

 

For example:

FFmpeg-SS 00:00:20-T 00:00:10-I d:/myvideo. mpg-vcodec copy-acopy Copy D:/split. mpg

This command is used to crop videos from 20 seconds to 20 + 10 = 30 seconds, with a total of 10 seconds. This command is executed very quickly because it is only a copy of the original data and there is no encoding or decoding process in the middle.

After executing this command, you can get the split. mpg output file. You can use the video play software to play this video. There may be some video effects after cropping, such as the expected consistency, starting in 20 seconds, ending in 30 seconds, a total of 10 seconds of video, however, after cropping a video, you may find that the start and end operations are not very accurate, but may start from 18 seconds and end from 33 seconds. Why?

Because 20 seconds and 30 seconds in these videos are not key frames, and FFMPEG will round to the nearest key frame near the two time points you entered, then do the following. If you don't know what a key frame is, it doesn't matter. This does not affect your use of this command.

 

If your request can accept the error of several seconds, this command can fully meet your needs, and you do not need to read the following content.

 

However, the requirements in my project are very strict and it must be determined by the time. So we need to use another method.

The reason for this is that the selected time is not a key frame. If we convert the input video to a video with all the frames being a key frame, in fact, it is to convert the encoding methods of all frames into intra-frame encoding (it doesn't matter if you don't understand the intra-frame encoding, so you don't need to see it and then look down ), this problem can be solved. FFmpeg can also help us do this.

 

FFmpeg-I input-sameq-intra output

-I: enter a space followed by the input video file;

Input input file;

-Sameq indicates that the same video quality is maintained;

-Intra: Intra-frame encoding;

Output file name.

 

For example:

FFmpeg-I d:/myvideo. mpg-sameq-intra D:/temp. mpg

The result file of this command is D:/temp. mpg. the video of this file and D:/myvideo. the MPG is the same, but you will find that this file is better than D:/myvideo. the MPG is many times larger, because the inter-frame encoding is generally used before the conversion, which is converted into intra-frame encoding. The reason is that some video files adopt intra-frame encoding.

 

Next we will start cropping.

FFmpeg-SS start-vsync 0-T duration-I input-vcodec videocodec-acodec audiocodec output

-SS start time, for example, 00:00:20, which indicates that the time starts from 20 seconds;

-T duration, for example, 00:00:10, indicates a video clip of 10 seconds;

-I: enter a space followed by the input video file;

-The encoding format of the vcodec video indicates the video format to be used;

-The acodec audio encoding format indicates the video format to be used;

Input: Enter the video file;

Output: output the video file;

 

For example:

FFmpeg-SS 00:00:30-vsync 0-T 00:00:30-I d:/temp. mpg-vcodec libx264-acodec libfaac D:/result. mpg

Here, both audio and video use AAC and hsf.

 

This gives us the final result we want.

 

Merge videos

 

We can crop the part of a video that we are interested in. For example, we can crop three videos, but we don't want them to be one by one. The general requirement is to specify a video. You can select the desired time period, start cropping, and finally obtain the video composed of the selected time periods.

 

To complete this task, you can simply crop videos based on the previous method. You can use the previous method to crop videos of interest. In this way, you can get multiple small videos, then we can use the following method to implement it:

I am sorry that the red section below is an earlier version and is incorrect.

Therefore, ignore the red part and view the following content.

[Merge into a complete video:

FFmpeg-I input1-I input2-F format-acodec audiocodec-vcodec videocodec-sameq output

 

Almost all parameters used here have been explained above, except-f.

 

-F format indicates the video format. For example,-F MP4, our video format is MP4.

 

In addition, you can add several-I [input file] to the desired merged videos. For example, I have two videos to merge.

 

FFmpeg-I d:/myvideo1.avi-I d:/myvideo2.avi-F MP4-acodec libfaac-vcodec libx264-sameq D:/result. Avi


The result. Avi here can also be changed to result.mp4.

 

If you find that the result video (result. avi) not merge two videos. It may be because the formats of the two videos are inconsistent when you merge the video, or the FFMPEG version you use has this bug, you can try again with the following method.

1. The video to be merged is first converted to a unified format, including the encoding format, frame rate, and size.

FFmpeg-I [input]-F MPEG-R 25 [out]

 

2. Use the copy or cat command to merge the logs .]

 

1. First, convert all videos to the MPEG format:

FFmpeg-I input-F MPEG output

For example:

FFmpeg-I d:/temp1.avi-F MPEG D:/result1.mpg

FFmpeg-I d:/temp2.mp4-F MPEG D:/result2.mpg

2. Merge videos using copy or cat commands

Copy-B input + Input Output

For example:

Copy/B "D:/result1.mpg" + "D:/result1.mpg" "D:/result. mpge"


3. encode the merged video to generate the final result video.

FFmpeg-I input-F format output

For example:

FFmpeg-I "d:/result. mpge"-F MP4 "D:/result.mp4"

.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.