Use FFmpeg to process audio and video

This article mainly introduces how to use the ffmpeg command line tool to perform various audio and video processing operations - scaling, cropping, clipping, rotation, format conversion, etc. . . After learning this article, you can basically delete audio and video processing software such as Format Factory. .

1. Install the ffmpeg command line tool

This article only introduces the installation method under the mac system. The installation for Linux users is also very simple. Win users can also search for tutorials online. . .

1. Install Homebrew

Homebrew claims to be an "indispensable package manager for OS X". Through homebrew, you can easily install common command-line tools on your Mac. Give the official website: http://brew.sh/

The official website of the installation and use method is very detailed, um. .

2. Install ffmpeg

We have installed Homebrew above, and then we can easily install ffmpeg with just one line of command:

$ brew install ffmpeg  //使用brew安装ffmpeg

After executing the above command, brew will start the crazy download mode. . If the internet speed is fast, the download can be completed in a while. Then brew will automatically add the startup path of ffmpeg to the path environment variable, so that you can use ffmpeg anywhere, and you don't need to cd to the ffmpeg installation directory before executing the command.

When brew automatically adds the startup path of ffmpeg to the path environment variable, it may prompt "permission denied", this is because brew does not change the permissions of the relevant files, just add it manually, for example:

$ sudo chmod 777 /usr/share/  // 这个命令是给所有程序添加/usr/share/的读、写、执行权限,执行成功之后就brew就可以更改/usr/share/下的内容了

The above command will let you enter the password of the currently logged in user, just enter the password of your computer.

2. Video processing

1. Clip

Sometimes we need to capture a certain segment of a long video, for example, starting from the 10th second of a video, capture 6 seconds of content, that is, 10 to 16 seconds of content, enter an out.mp4 file

$ ffmpeg -i in.mp4 -ss 00:00:10 -t 00:00:06 -acodec aac -vcodec h264 -strict -2 out.mp4   //从00:00:10开始,截取的长度为00:00:06

Parameter explanation:

-i stands for input file to be processed

-ss represents the start time

-t represents the length of the interception.

-acodec audio codec, it doesn't matter if you don't understand this, just copy it directly. .

-vcodec audio codec, it doesn't matter if you don't understand this, just copy it directly. .

2. Zoom

Many times we need to process a high-resolution video into a low-resolution video in order to reduce the size of the video. For example: downscale a 1080 1920 video to 360 640

$ ffmpeg -i in.mp4 -vf scale=360:640 -acodec aac -vcodec h264 out.mp4  // 1080*1920-->360*640

Parameter explanation:

-i stands for input,

The full name of -vf is video filter, that is: video filter, zooming is actually adding a filter to the video.

scale=360:640 Scale is a filter, zoom filter, the format is: scale=width:height, where width and height are the processed width and height respectively

3. Crop

Sometimes we want to capture the middle part of a large video, such as a 1080 1920 video, we want to capture the middle 1080 1080 part, this can also be achieved:

$ ffmpeg -i in.mp4 -strict -2 -vf crop=1080:1080:0:420 out.mp4 

Parameter explanation:

Like the scale above, crop is also a type of video filter, and crop is a cropping filter . The four parameters are width:height:x:y, where width and height refer to the width and height of the crop, x and y represent the coordinates of the upper left corner of the cropped area, and the origin of the coordinate system is the upper left corner of the original video. For example, 0:0 represents the upper left corner of the original video, and 50:50 represents the 50:50 position of the coordinate system with the upper left corner of the original video as the origin.

4. Rotate

Rotating videos is easy with ffmpeg. Example: Rotate a video 90 degrees clockwise

$ ffmpeg -i in.mp4 -vf rotate=PI/2:ow=1080:oh=1920 out.mp4

Parameter explanation:

Video rotation is actually a constant filter.

rotate=PI/2 rotate is a rotation filter, the "PI/2" rotation angle behind it (positive number means clockwise), here is 90 degrees

In addition to the parameters that specify the rotation angle, rotate has some other parameters:

The full name of ow is out width, the width of the output video, if not specified, the default is the width of the input video

The full name of oh is out height, the height of the output video, if not specified, the default is the height of the input video

5. Adjust the frame rate

The frame rate will greatly affect the smoothness of the picture and the volume of the video. The higher the frame rate, the smoother the picture and the larger the video volume.

We sometimes need to reduce the size of the video by reducing the frame rate.

For example: reduce the frame rate of a video to 15

$ ffmpeg -i in.mp4 -r 15 out.mp4

Parameter explanation:

-r framerate

6. Format conversion

ffmpeg has a powerful format conversion function, here are a few common examples.

$ ffmpeg -i in.mov -vcodec copy -acodec copy out.mp4  // mov --> mp4
$ ffmpeg -i in.flv -vcodec copy -acodec copy out.mp4  // flv --> mp4
$ ffmpeg -i in.gif -vf scale=420:-2,format=yuv420p out.mp4  // gif --> mp4

7. View video details

Sometimes we need to understand the parameter information of the video before processing, such as resolution, bit rate and so on. The following commands can be used:

$ ffmpeg -i in.mp4   // 不加任何参数,只指定输入的视频

Take a screenshot:

3. Audio processing

Continue later. . .

四. Reference

FFmpeg has powerful audio and video processing capabilities. Its official website gives an introduction to the use of many audio and video processing filters. The article only mentions some commonly used operations. If you don’t have what you want, you can go directly to the official website. Filter introduction .

Introduction to audio and video filters: http://ffmpeg.org/ffmpeg-filters.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326692077&siteId=291194637