Common parameters and usage examples of FFmpeg

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 ffmpegto 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 infoetc.
  • 2. Input file parameters, such as -c:v libx264etc.
  • 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:

  • -yWithout confirmation, directly overwrite the file with the same name when outputting
  • -v infoSpecify the log level, commonly used are info,error
  • -iSpecify input file or stream address
  • -cSpecify the encoder, commonly used -c copyto indicate direct copy without re-encoding
  • -c:vspecify video encoder
  • -c:aSpecifies the audio codec
  • -anremove audio stream
  • -vnremove video stream
  • -fMandatory format output, commonly used are -f mp4, -f flv,-f segment
  • -rSpecifies 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 rtspor rtmprecord as a video file

ffmpeg -y -i rtmp://ip:port/stream -f mp4 out.mp4
  • -fYou can specify parameters as mp4, flvetc.
  • If the stream format is, rtspyou can use the parameter -f rstpto specify the input stream format
  • Can use -rtsp_transport tcpspecified rtspusage tcpagreement
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 jpga file:

ffmpeg -y -i input.mp4 -vframes 1 -f mjpeg output.jpg
  • -vframes 1Specify the number of frames, here specify 1 frame
  • -fOutput image format, -f mjpegspecified as jpg; -f image2specify the output format aspng

If you want to intercept from the specified time, you need to use -ssthe parameter to specify the start time

ffmpeg -y -ss 00:10:00 -i input.mp4 -vframes 1 -f mjpeg output.jpg
  • -ssSpecify 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 -ssthe position of the parameter

When capturing a local video file, the parameter -ssshould be -ibefore the input parameter . In the actual process, it is found that as the offset time increases, it takes longer to capture a frame after -ssplacing 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.txtthe file in sequence, for example

a.mp4
b.mp4
c.mp4

Then merge the video in the file by -f concatcommand asinput_list.txtoutput.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           # 输出文件名

8. Output to pipeline

Guess you like

Origin blog.csdn.net/MrTeacher/article/details/118077235