This article describes how to use ffMPEG command line tools to perform various audio and video processing operations — zooming, clipping, clipping, rotation, formatting, etc… Learn this article, the basic format factory such as audio and video processing software to delete.

Install the FFmpeg command line tool

This article only introduces the installation method under the MAC system, Linux users are easy to install, Win users can also find tutorials online…

1. Install Homebrew

Billed as “the indispensable package manager for OS X,” Homebrew makes it easy to install common command-line tools on a MAC. The official website is http://brew.sh/

The installation and use methods are very detailed on the official website.

2. Install ffmpeg

With Homebrew installed above, ffmpeg can be easily installed with a single command:

Brew install ffmpeg // Install ffmpeg using brewCopy the code

After executing the above command, Brew will start the mad download mode. If you have a fast Internet connection, you can finish downloading in a few minutes. Brew then automatically adds the ffMPEG startup path to the PATH environment variable, so you can use FFMPEG anywhere without having to CD to the FFMpeg installation directory and then execute commands.

Brew automatically adds the ffmpeg startup path to the path environment variable, and may prompt “Permission denied”. This is because BREW does not have permission to change the related files.

$usr/share/ brew $usr/share/ brew $usr/share/ brew $usr/share/ brew $usr/share/ brewCopy the code

The command above will ask you to enter the password of the current logged-in user. Just enter your computer password.

Second, video processing

1. The clip

Sometimes we need to capture a part of a long video. For example, from the 10th second of a video, we need to capture 6 seconds of content, that is, 10 to 16 seconds of content, and 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 // From 00:00:10, cut length 00:00:06Copy the code

Parameter Description:

-I indicates the file to be processed

-ss indicates the start time

-t indicates the truncated length.

-acodec audio codec (audio codec)

-vcodec audio codec, this does not matter if you do not understand, directly copy the line.

2. Zoom

Most of the time we need to process a high resolution video into a low resolution video to achieve the purpose of reducing the volume of the video. An example: Shrink a video from 10801920 to 360640

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

Parameter Description:

-I stands for input,

– VF is video filter. Zooming is to add a filter to a video.

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

3. Cut out

Sometimes we want to capture the middle part of a large video, such as a 10801920 video, we want to capture the middle part of 10801080, this can also be done:

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

Parameter Description:

Crop, like the scale above, is also a type of video filter. Crop is a cropping filter. The four parameters are respectively width:height:x:y, where width and height refer to the width and height of the clipping, x and y represent the coordinates of the upper left corner of the clipping area, and the origin of the coordinate system is the upper left corner of the original video. For example, 0:0 would be the top left corner of the original video, and 50:50 would be the 50:50 position of the frame starting from the top left corner of the original video

4. Rotate

Video can be easily rotated using FFMPEG. An example: Rotate a video 90 degrees clockwise

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

Parameter Description:

Video rotation is also a constant filter.

Rotate =PI/2 Rotate indicates the rotation filter. The value of PI/2 is 90 degrees

Rotate In addition to specifying the rotation Angle, there are several other parameters:

Ow stands for out width, the width of the output video. If not specified, the default is the width of the input video

Oh is out height, the height of the output video, if not specified, the default is the height of the input video

Adjust the frame rate

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 volume of the video.

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

Take, for example, reducing the frame rate of a video to 15

$ ffmpeg -i in.mp4 -r 15 out.mp4
Copy the code

Parameter Description:

– r frame rate

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 --> mp4Copy the code

7. View the details of the video

Sometimes we need to understand the parameters of the video before processing, such as resolution, bit rate and so on. You can use the following command:

$ffmpeg-i in.mp4 // Specifies the input video without any parametersCopy the code

Here’s a screenshot:

Three. Audio processing

Continue later…

4. The Reference

FFmpeg has a powerful audio and video processing capabilities, its official website provides a number of audio and video processing filter introduction, the article only mentioned some common operations, if you do not want, you can directly go to the official website to see the introduction of filters.

Audio and video filters: http://ffmpeg.org/ffmpeg-filters.html