ffmpeg Tutorial - Teach you how to process audio and video screens easily and quickly

1. Getting Started:

参考视频链接:
https://www.bilibili.com/video/av40146374/?p=9

# ffmpeg支持很多视频格式,此处用mp4举例

# 显示所有的操作命令,以及介绍
ffmpeg -h 

# 播放视频、音频 按esc退出
ffplay example.mp4
# 键盘上的左右可以进行前进后退播放,点击窗口的任意一个位置,可以跳转播放位置
# f键是全屏 p或空格是暂停 double右键是切换显示模式
ffplay example.mp3
# 查看视频的具体信息
ffprobe example.mp4

2. Audio and video format conversion

# -i是input的缩写,后面接输入文件,之后接输出文件及其格式,这是一条视频文件格式转换命令
ffmpeg -i example.mov output.mp4

# -acodec an audio(音频) codec(编解码器) 这条指令是用来指定音频编解码器
# -ar 设置音频采样率
# -ab 设置音频的比特率 如320k
# -ac 声道 c是channel的缩写,用来设置声道数的指令 1是单声道,2是双声道立体声 默认是使用原音频的声# 道数
# 最后是输出音频的文件名和格式
ffmpeg -i example.flac -acodec libmp3lame -ar 44100 -ab 320k -ac 2 out.mp3

Video encapsulation, encoding and decoding formats of major video websites

3. Audio and video options

1. Video options

Video options:
-vframes number     set the number of video frames to output
-r rate             set frame rate (Hz value, fraction or abbreviation)
# 用来缩放视频尺寸
-s size             set frame size (WxH or abbreviation)
-aspect aspect      set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-bits_per_raw_sample number  set the number of bits per raw sample
-vn                 disable video
-vcodec codec       force video codec ('copy' to copy stream)
-timecode hh:mm:ss[:;.]ff  set initial TimeCode value.
-pass n             select the pass number (1 to 3)
-vf filter_graph    set video filters
-ab bitrate         audio bitrate (please use -b:a)
-b bitrate          video bitrate (please use -b:v)
-dn                 disable data

2. Audio options

Audio options:
-aframes number     set the number of audio frames to output
-aq quality         set audio quality (codec-specific)
-ar rate            set audio sampling rate (in Hz)
-ac channels        set number of audio channels
-an                 disable audio
-acodec codec       force audio codec ('copy' to copy stream)
-vol volume         change audio volume (256=normal)
-af filter_graph    set audio filters

4. Video suppression

  • Compress 4k in.webm to 1080p MP4
# -pix_fmt : 此处采用yuv420p,网络视频基本上采用这个格式
# -vcodec : 用来设置视频流的编码器指令,一般使用libx264
# -preset 编码器预设 默认是medium
# -profile:v 用来指定编码器的配置,主要和压缩比有关    实时通信领域一般采用 baseline, 
# 流媒体采用 main,制作超清视频使用 high
# -level:v 对编码器的具体规范和限制,压缩比和画质就像鱼和熊掌,一般情况下,1080p的视频就用4.1
# -crf  constant rate factor 恒定速率因子模式 适用于对画质有要求,大小无关紧要的情形。
# 范围为0- 51,默认值就是23,数值越小,质量越高,0就是无损的画质 
# -r 是30帧
# -b:a 用来设置音频比特率,ffmpeg在这里推荐使用这一种书写方式,目前大多数网站都要求为128k/s或
# 192k/s
ffmpeg -i in.webm -s 1920*1080 -pix_fmt yuv420p -vcodec libx264 -preset medium 
-profile:v high -level:v 4.1 -crf 23 -acodec acc -ar 44100 -ac 2 -b:a 128k out.mp4

# YUV、NV、RGB
ffmpeg -pix_fmts

-preset encoder preset options generally use veryslow when compressing video

video compression simple code

# -i 输入
# -r 帧率,即每秒多少帧
# -b:a 32k 用来设置音频比特率,目前大多数网站都要求为128k/s
# 最后为输出
 ffmpeg -i "output.avi" -r 10 -b:a 32k 1.mp4

Five, ffmpeg rate control mode

We all know that the better the picture quality, the higher the bit rate required, and the larger the file size. The so-called bit rate control is to determine how many bits are allocated to each frame, which is a trade-off between file size and picture quality. In fact, ffmpeg supports a total of three rate control modes: -qp, -crf, -b

-qp (constant quantizer): Constant quantizer mode, using a special algorithm to convert the quantizer into a number, so that the picture quality of each frame can be judged by these parameters, these quantization parameters can be manually tested, It is not recommended, just let the software set it automatically. The image quality is divided into 0-51 levels, 0 means lossless image quality. Compress video with qp, and each frame of the picture can reach the quality of this level. The image quality is The best, the largest volume, too big to imagine, generally do not use this rate control mode

# 无损压缩的例子(快速编码)
ffmpeg -i input -vcodec libx264 -preset ultrafast -qp 0 output.mkv

# 无损压缩的例子(高压缩比)
ffmpeg -i input -vcodec libx264 -preset veryslow -qp 0 output.mkv

-crf (constant rate factor) The constant rate factor mode is actually a floating qp mode, and the code rate is adjusted according to the human eye.

Use a part of the bit rate in the visually sensitive picture, the overall bit rate is reduced, and the file size is also smaller

-b (bitrate) Fixed target bit rate mode, encoder view, let the final bit rate of the encoder be equal to our given bit rate, video bit rate and file size are determined

They are all single-pass encoded

-b bitrate uses vbr (variable bit rate dynamic bit rate) to encode

# VBR的例子
ffmpeg -i input -vcodec libx264 -preset veryslow output
  • Today's video sites generally use ABR (average bit rate/average bit rate) encoding
  • CBR (constant bit rate) constant bit rate no one uses

6. Merge and extract audio and video

1. Extract audio and video

# -vcodec copy:保持原编码不变 
# -an 静音,实际上是将音频剔除
# 纯粹视频获取
ffmpeg -i in.mp4 -vcodec copy -an v.mp4

# 存粹音频获取 
ffmpeg -i in.mp4 -vn -acodec copy a.m4a

Some videos have multiple audio streams, so they can only be extracted in a targeted manner, so you need to add -map 0:3 to the extracted audio

2. Synthesize audio and video

# 将音频和视频合在一起  -c copy 用来维持编码器不变
ffmpeg -i a,m4a -i v.mp4 -c copy out.mp4

6. Intercept and connect audio and video

1. Intercept

# 记得音视频区别-acodec -vcodec
# 截取音频 -ss,-to要截取的音视频的起始时间和终止时间
ffmpeg -i in.mp3 -ss 00:01:00 -to 00:01:10 -acodec copy out.mp3
# 截取音频 -ss要截取的音视频的起始时间,-t 代表截取时长,此处为10s
ffmpeg -i in.mp3 -ss 00:01:00 -t 10 -acodec copy out.mp3
# 将-ss放在-i之前能够使用关键帧技术,但是这样截取出来的视频起始时间和终止时间不一定准确
ffmpeg -ss 00:01:00 -i in.mp3 -to 00:01:10 -acodec copy out.mp3
# 解决上述问题 -copys 保留时间戳
ffmpeg -ss 00:01:00 -i in.mp3 -to 00:01:10 -acodec copy -copys out.mp3

2. Connection

# concat:concatenate 串联 串联视频
ffmpeg -i "concat:01.mp4|02.mp4|03.mp4" -c copy out.mp4

7. Extract keyframes

ffmpeg -i video_name.mp4 -vf select='eq(pict_type\,I)' -vsync 2 -s 1920*1080 -f image2 core-%02d.jpeg
# 各个参数解释: 
# -i :输入文件,这里的话其实就是视频, 
# -vf:是一个命令行,表示过滤图形的描述, 选择过滤器select会选择帧进行输出:包括过滤器常量 
# pict_type和对应的类型:PICT_TYPE_I 表示是I帧,即关键帧。 
# -vsync 2:阻止每个关键帧产生多余的拷贝 
# -f image2 name_%02d.jpeg:将视频帧写入到图片中,样式的格式一般是: 
# “%d” 或者 “%0Nd” 
# -s:分辨率,1920*1080

Relevant information

Python - FFmpeg  - This article describes the structural concepts of ffmpeg in detail, but not much about the application of ffmpeg in python.
[Python] ffmpeg module query video and audio information

Original  ffmpeg tutorial - teach you how to handle audio and video screens simply and quickly - Miracleo_'s Blog - CSDN Blog

★The business card at the end of the article can receive audio and video development learning materials for free, including (FFmpeg, webRTC, rtmp, hls, rtsp, ffplay, srs) and audio and video learning roadmaps, etc.

see below!

 

Guess you like

Origin blog.csdn.net/yinshipin007/article/details/131011007
Recommended