FFmpeg is an open source free software that can perform recording, conversion, and streaming functions in multiple formats of audio and video. Here we organize the common commands and parameter meanings of FFmpeg in combination with our own actual use.
Install and use the format
Based on the Ubuntu system, it can be installed with a simple apt command.
sudo apt update
sudo apt -y install ffmpeg
After the installation is complete, you can use FFmpeg, enter ffmpeg
to view its version and configuration information. ffmpeg has many command parameters, which can be divided into five parts:
ffmpeg {1} {2} -i {3} {4} {5}
- 1. Global parameters, e.g.
-y
,-v info
etc. - 2. Input file parameters, such as
-c:v libx264
etc. - 3. Input file
- 4. Output file parameters
- 5. Output file
Example usage:
ffmpeg -y -v info -c:v libx264 -i input.mp4 -c:v libvpx-vp9 -c:a libvorbi output.webm
Common command line parameters
Description of commonly used command line parameters:
-y
Without confirmation, directly overwrite the file with the same name when outputting-v info
Specify the log level, commonly used areinfo
,error
-i
Specify input file or stream address-c
Specify the encoder, commonly used-c copy
to indicate direct copy without re-encoding-c:v
specify video encoder-c:a
Specifies the audio codec-an
remove audio stream-vn
remove video stream-f
Mandatory format output, commonly used are-f mp4
,-f flv
,-f segment
-r
Specifies the frame rate, the default is 25. For example-r 15
Example of use
1. View file or stream information
View meta information of a video file, such as encoding format and bitrate or duration and resolution of a video file
ffmpeg -i input.mp4
2. The video stream is converted to a local video file
Live stream rtsp
or rtmp
record as a video file
ffmpeg -y -i rtmp://ip:port/stream -f mp4 out.mp4
-f
You can specify parameters asmp4
,flv
etc.- If the stream format is,
rtsp
you can use the parameter-f rstp
to specify the input stream format - Can use
-rtsp_transport tcp
specifiedrtsp
usagetcp
agreement
ffmpeg \
-y \
-v info \ # 指定日志级别为info
-rtsp_transport tcp \ # rtsp 使用tcp协议
-i "rtsp://ip:port/stream" \
-f mp4 \ # 指定视频文件格式为mp4
out.mp4
3. Capture a frame from a video file
Capture the first frame of the video file and output it as jpg
a file:
ffmpeg -y -i input.mp4 -vframes 1 -f mjpeg output.jpg
-vframes 1
Specify the number of frames, here specify 1 frame-f
Output image format,-f mjpeg
specified asjpg
;-f image2
specify the output format aspng
If you want to intercept from the specified time, you need to use -ss
the parameter to specify the start time
ffmpeg -y -ss 00:10:00 -i input.mp4 -vframes 1 -f mjpeg output.jpg
-ss
Specify the start time, the format can be:hh:mm:ss.xxx
, it can also be seconds
If you need to continuously capture multiple pictures, you can use the following command:
ffmpeg -y \
-ss 00:00:10 \ # 指定从视频的第10秒开始截取
-i input.mp4 \
-vframes 10 \ # 只取10帧
-t 10 \ # 持续10秒
-f image2 \ # 指定输出格式为png
output_%3d.png # 文件格式为output_001.png
Note -ss
the position of the parameter
When capturing a local video file, the parameter -ss
should be -i
before the input parameter . In the actual process, it is found that as the offset time increases, it takes longer to capture a frame after -ss
placing it many times, but if it is placed before, it will be found that the screenshot time is constant and low. The reason is that the former offsets from the start time of the video file each time, while the latter directly locates the specified time position without processing the previous part.-i
-ss
-i
-ss
4. Video file cropping
5. Mp4 to Mp3
6. Merge multiple video files
Multiple video files need to save the video file names to input_list.txt
the file in sequence, for example
a.mp4
b.mp4
c.mp4
Then merge the video in the file by -f concat
command asinput_list.txt
output.mp4
ffmpeg \
-y -v info \
-f concat -safe 0 \
-i input_list.txt \
-c copy \
-bsf:a aac_adtstoasc -movflags +faststart \
output.mp4
This method has a high success rate and is the best
7. Stream file processing
In actual business, the processed and recorded live stream is divided into multiple video files according to the specified duration.
ffmpeg \
-y \
-v info \
-rtbufsize 1m \
-i rtmp://host:port/stream \
-movflags faststart+frag_keyframe \ # 使mp4支持渐进式下载
-c:v copy \ # 原始编解码数据必须被拷贝
-c:a copy \ # 设定声音编码,降低CPU使用
-f segment \ # 输出流切片
-segment_format mp4 \ # 流输出格式
-strftime 1 \ # 设置切片名为生成切片的时间点
-segment_time 30 \ # 流切分时长30秒
-reset_timestamps 1 \ # 每个切片都重新初始化时间戳
out_file__%Y-%m-%d_%H-%M-%S.mp4 # 输出文件名