What is FFmpeg
FFmpeg is an open source cross-platform audio and video processing toolset that can be used to process, convert, edit and stream audio and video files. It provides a set of powerful command line tools that can realize audio and video decoding, encoding, encapsulation, filtering, transcoding and other functions.
FFmpeg features
FFmpeg has many functions, only some of them are listed below:
1️⃣Audio and video transcoding and conversion: You can convert audio and video formats, and implement operations such as intercepting, editing, and merging audio and video.
2️⃣Streaming media server: It can be used to build a streaming media server to realize live broadcast, on-demand and other functions.
3️⃣Code stream analysis: You can perform code stream analysis on audio and video to obtain the encoding information, frame rate, bit rate and other parameters of audio and video.
4️⃣Multimedia processing: It can be used to extract key frames in audio and video, add watermarks and subtitles, etc.
5️⃣Audio and video parameter adjustment: You can adjust audio and video parameters, such as resolution, frame rate, bit rate, etc.
6️⃣Video clipping and merging: You can extract specific segments from video files or merge multiple video files into one.
7️⃣Convert picture sequence to video: You can convert a series of picture files into video files to achieve animation effects.
8️⃣Video screenshots and thumbnail generation: You can extract static images from the video, that is, capture a certain frame of the video as a picture, or you can generate a thumbnail of the video.
9️⃣Video recording: can be used to record screen and camera videos.
Install ffmpeg
The methods of installing ffmpeg in Windows and Linux are slightly different, which are introduced below.
Install ffmpeg in Windows
- Visit the ffmpeg official website (https://ffmpeg.org/download.html) to download the latest version of the binary file.
- Unzip the downloaded file to a specified directory,
C:\Program Files\ffmpeg
e.g. - Add ffmpeg to the system environment variables for direct use from the command line. The specific method is: right-click "This Computer" -> "Properties" -> "Advanced System Settings" -> "Environment Variables", find "Path" in "System Variables" and edit it, and add the bin directory path of ffmpeg to in.
- To verify whether the installation is successful, open the command prompt or PowerShell and enter
ffmpeg -version
. If the version number can be output, the installation is successful.
Install ffmpeg in Linux
The following are the commands to install ffmpeg on various Linux distributions:
Debian/Ubuntu
sudo apt update
sudo apt install ffmpeg
Fedora
sudo dnf update
sudo dnf install ffmpeg
CentOS/RHEL
sudo yum update
sudo yum install ffmpeg
Arch Linux
sudo pacman -Syu
sudo pacman -S ffmpeg
openSUSE
sudo zypper refresh
sudo zypper install ffmpeg
Note that these commands may vary slightly between different Linux distributions. If you are using another distribution, please consult its official documentation for the correct installation commands
FFmpeg file description
There are only three files in the bin folder of the Static version of FFmpeg, namely: ffmpeg, ffplay, and ffprobe. These three are executable files generated after compiling the FFmpeg source code, which are the three major tools of FFmpeg.
ffmpeg: used for encoding and decoding. The main workflow of ffmpeg is relatively simple. First, the input source is read.
ffplay: used for real-time audio and video processing and playback.
ffprobe: used to analyze the code stream.
1. Format conversion:
FFmpeg provides rich audio and video format conversion functions. Its format conversion command format is:
ffmpeg -i 输入文件路径 -b:v 输出视频码率 输出文件路径
2. Streaming media server
The commands to use FFmpeg to build a streaming media server are related to the specific operating system and the network protocol used. The following is an example command using the RTMP protocol on the Ubuntu system:
ffmpeg -i input_file.mp4 -f flv rtmp://server_address/app_name/stream_name
Among them, input_file.mp4 is the video file to be transcoded, rtmp://server_address/app_name/stream_name is the address of the RTMP stream, server_address is the server address, app_name is the application name, and stream_name is the name of the stream.
If you need more detailed information, please refer to FFmpeg official documentation or search for related tutorials.
3. Code stream analysis
FFmpeg's code stream analysis command is ffprobe.
FFprobe is a tool for analyzing audio and video format information. It can read and analyze the metadata of multimedia files, such as file format, encoding method, packaging format, video parameters, audio parameters, etc.
The usage method is: ffprobe [options] input_file.
ffprobe -show_format [FORMAT] filename=[/path/to/]file。
4. Multimedia processing
FFmpeg has many multimedia processing commands, such as:
- Cut a certain video:
ffmpeg -ss 开始时间 -t 持续时间 -i input_file -vcodec copy -acodec copy output_file
- Separate video and audio streams and save them as different files:
ffmpeg -i input_file -vcodec copy -an output_file_videoffmpeg -i input_file -acodec copy -vn output_file_audio
- Video demultiplexing:
ffmpeg -i input_file -vcodec copy -an output_file
- Video transcoding:
ffmpeg -i input_file -vcodec new_codec -s new_size -an output_file
5. Video merging
The commands for using FFmpeg video merging vary depending on your specific needs, but here are some common command examples:
- Merge multiple videos horizontally:
ffmpeg -i input1.mp4 -i input2.mp4 -lavfi hstack output.mp4
Among them, input1.mp4 and input2.mp4 are the video files to be merged, and output.mp4 is the output merged video file.
- Merge multiple videos vertically:
ffmpeg -i input1.mp4 -i input2.mp4 -lavfi vstack output.mp4
Among them, input1.mp4 and input2.mp4 are the video files to be merged, and output.mp4 is the output merged video file.
- Grid merge multiple videos:
ffmpeg -i input1.mp4 -i input2.mp4 -lavfi grid=rows=2:columns=2 output.mp4
Among them, input1.mp4, input2.mp4, etc. are the video files to be merged, and output.mp4 is the output merged video file.
6. Record screen
To use ffmpeg to record the screen, you can use the following command:
ffmpeg -f x11grab -s 分辨率 -i :0.0 -framerate 帧率 输出文件名
Parameter Description:
-f
Specify the input format as X11 Grab (X11 screen capture).-s
Set the resolution of the output video, such as "1280x720".-i
Followed by ":0.0", it means capturing the entire screen. If you only want to capture a specific window, you can replace ":0.0" with the corresponding window number.-framerate
Set the frame rate, for example "30".- Finally, there is the output file name, such as "output.mp4".
For example, if you want to record a video file with a resolution of 1280x720 and a frame rate of 30, you can run the following command:
ffmpeg -f x11grab -s 1280x720 -i :0.0 -framerate 30 output.mp4
7. Recording camera
To record camera video using ffmpeg, you can use the following command:
ffmpeg -f v4l2 -i /dev/video0 output.mp4
Among them, -f v4l2
it means using the Video4Linux2 (v4l2) driver for input device selection. /dev/video0
It is the path of the camera device and can be modified according to the actual situation. output.mp4
is the output video file name.
If you need to specify parameters such as resolution and frame rate, you can use a command similar to the following:
ffmpeg -f v4l2 -i /dev/video0 -s WIDTHxHEIGHT -r FRAME_RATE output.mp4
Among them, WIDTHxHEIGHT
represents the resolution of the video, for example 1280x720
. FRAME_RATE
Indicates the frame rate of the video, for example 30
.