FFmpeg command line practice two

1 FFmpeg command parameters

1.1 Main parameters

  • -i set the input stream
  • -f set the output format (format)
  • -ss start time
  • -t time length
#修改音视频容器格式为flv,拷贝编码格式,复制0-20s数据
ffmpeg -i test.mp4 -ss 0 -t 20 -codec copy -f flv out.flv
#-f参数可以省略,自动识别输出格式
ffmpeg -i test.mp4 -ss 0 -t 20 -codec copy out.flv

1.2 Video parameters

  • -vframes set the number of video frames to output
  • -b set the video bit rate
  • -b:v video bit rate
  • -r set the frame rate
  • -s set the width and height of the screen
  • -vn do not process video
  • -aspect aspect set aspect ratio 4:3 16:9 or 1.3333 1.7777
  • -vcodec Set the video codec, if copy means the original codec data must be copied.
  • -vf video filter
#查看h265编码格式
ffmpeg -encoders | grep h265
--enable-libx265
#修改视频码率为64k,帧率30帧,分辨率:640x480,纵横比4:3,编码格式为h265
ffmpeg -i test.mp4 -vframes 5 -b 64k -r 30 -s 640x480 -aspect 4:3 -vcodec libx265 test-video-265.mp4
#提取视频数据
ffmpeg -i test.mp4 -vcodec copy -an test-no-audio.mp4

before fixing:insert image description here

After modification: insert image description here
Note: After modification, you can see that the bit rate of the video does not take effect, so the parameters affect each other.

1.3 Audio parameters

  • -aframes set the number of audio frames to output
  • -b:a audio bitrate
  • -ar set the sampling rate
  • -ac Set the number of channels for the sound
  • -acodec Set the sound codec, if copy means the original codec data must be copied.
  • -an do not process audio
  • -af audio filter
#查看mp3编码格式
ffmpeg -encoders | grep mp3
A..... libmp3lame           libmp3lame MP3 (MPEG audio layer 3) (codec mp3)
#修改音频音频码率为64k,采样率为16000,双通道,编码格式为mp3,输出100帧大小
ffmpeg -i test.mp4 -aframes 100 -b:a 64k -ar 16000 -ac 2 -acodec libmp3lame test-audio.mp4 
#提取音频数据
ffmpeg -i test.mp4 -acodec copy -vn test-no-video.mp4

Before modification: insert image description here
After modification:insert image description here

2 Extract audio and video data

2.1 Keep the original data format

First check the audio and video format with mediainfo, and then directly extract the corresponding file

#提取文件中aac音频数据
ffmpeg -i test.mp4 -acodec copy -vn test-no-video.aac
#提取文件中h264视频码流
ffmpeg -i test.mp4 -vcodec copy -an test-no-audio.h264

2.2 Specify the encoding format to extract data

Sometimes it is necessary to convert the audio and video format to the required format, and you can specify a specific encoding format for encoding and extract related data.

#将视频编码格式转换为h265
ffmpeg -i test.mp4 -vcodec libx265 -an -vframes 5 test.h265 
#将音频转换为mp3
ffmpeg -i test.mp4 -acodec libmp3lame -vn test.mp3

2.3 Extract video pixel format

The video pixel format in the video file can be extracted, and the two common ones are yuv and rgb. It should be noted here that the pixel format has no video resolution information. After the conversion is completed, the video resolution must be attached to the name to ensure that the video can be played normally.

#查看yuv格式
ffmpeg -pix_fmts | grep yuv

#提取视频像素格式为yuv420p,提取5s
ffmpeg -i test.mp4 -t 5 -pix_fmt yuv420p -s 320x240 test-yuv420p-320x240.yuv
#提取视频像素格式为rgb24
ffmpeg -i test.mp4 -t 5 -pix_fmt rgb24 -s 320x240 test-rgb24-320x240.rgb
#yuv420p转rgb24,需要指定输入的分辨率320x240
ffmpeg -s 320x240 -i test-yuv420p.yuv -pix_fmt rgb24 test-rgb24_320x240_1.rgb

2.4 Play video pixel format files

To play the video pixel format, you need to specify the basic pixel format and resolution

#播放yuv420p格式文件,需要指定像素格式为yuv420p,视频分辨率320x240
ffplay -pixel_format yuv420p -video_size 320x240 -i test-yuv420p-320x240.yuv

2.5 Extract audio pcm format

Extract pcm data and specify the sampling rate, number of channels and package format

#查询采样格式
ffmpeg -formats | grep PCM
DE s32le           PCM signed 32-bit little-endian
#提取pcm数据
ffmpeg -i test.mp3 -ar 16000 -ac 2 -f s16le 16000_2_s16le.pcm
#利用sample_fmt获取pcm数据
ffmpeg -sample_fmts | grep PCM
ffmpeg -i test.mp3 -ar 16000 -ac 2 -sample_fmt s16 16000_2_s16.wav
//利用encodec获取
ffmpeg -encoders | grep PCM
ffmpeg -i test.mp3 -ar 16000 -ac 2 -codec:a pcm_s16le 16000_2_s16le.wav

2.6 Play audio pixel format

To play pcm data, you need to specify the sampling rate, number of channels and encapsulation format.

ffplay -ar 16000 -ac 2 -f s16le 16000_2_s16le.pcm

3 ffmpeg to package format

3.2 Preserve encoding format

Keep the copy of the encapsulation format. It should be noted here that retaining the encapsulation format -codec copy is equivalent to -vcodec copy plus -acodec copy plus -s copy, which includes audio encoding, video encoding and subtitles.

#保留编码格式,转换封装格式为ts,
ffmpeg -i test.mp4 -codec copy -t 5 test.flv

3.2 Specify the encoding format

The specified encoding format mainly modifies two parameters, one is vcodec and the other is acodec. It should be noted that how to find the format name you want, generally use -encoders

#将MP4中原格式h264转为h265;音频aac转成mp3
ffmpeg -i test.mp4 -vcodec libx265 -acodec libmp3lame -t 5 test-h265-mp3.ts

3.2 Modify video parameters

Modify video frame rate, bit rate and resolution

#修改视频码率为500k
ffmpeg -i test.mp4 -b:v 500k -t 5 test-bv500.ts
#修改视频帧率为10
ffmpeg -i test.mp4 -r:v 10 -t 5 test-rv10.ts
#修改视频分辨率为
ffmpeg -i test.mp4 -s:v 800x480 -t 5 test-sv10.ts
#修改音视频帧率
ffmpeg -i test.mp4 -r:v 10 -r:a 100 -t 5 test-rv10av10.ts

Modify the frame rate to 10 insert image description here
and modify the code rate to 500kinsert image description here

3.3 Modify audio parameters

Modify audio bit rate, sampling rate and sampling precision

#修改音频码率为500k
ffmpeg -i test.mp4 -b:a 500k -vcodec copy -t 5 test-av500.ts
#修改音频采样率16000
ffmpeg -i test.mp4 -r:a 16000 -vcodec copy -t 5 test-ar10.ts
#修改音频采样精度位16bit
ffmpeg -i test.mp4 -q:a 16 -vcodec copy -t 5 test-aq16.ts
#修改音视频帧率
ffmpeg -i test.mp4 -r:v 10 -r:a 100 -vcodec copy -t 5 test-rv10av10.ts

4 Audio and video cropping

4.1 Audio and video file cropping

Video trimming needs to be composed of two parts: the starting position and the trimming duration. Here, the trimming starting position can be represented by the number of seconds or formatted as 00:00:00. In addition, when converting the video format, you need to pay attention to the conversion to add a bsf filter for video compatibility to ensure that the video can be played normally after conversion.

#裁剪视频从第5秒开始裁剪10s长度视频
ffmpeg -i test.mp4 -ss 00:00:05 -t 10 -codec copy test-ss10.mp4
#裁剪视频从第5秒开始裁剪10s长度视频,加vbsf为了提高转换的兼容性
ffmpeg -bsfs | grep h264
h264_mp4toannexb
ffmpeg -i test.mp4 -ss 00:00:05 -t 10  -codec copy -vbsf h264_mp4toannexb test-ss10vbsf.ts

4.2 Audio and video file splicing

#Video splicing method, concat command for splicing

ffmpeg -f concat -i mp4list.txt -codec copy test-concat2.mp4
vi mp4list.txt
file 'test.mp4'
file 'test-ss10.mp4'

5 Video and picture conversion

When converting video to
picture, when converting multiple pictures, you need to use the formatting method to convert to multiple pictures.

#将视频转图片,%02d转换多张图片,转换一张图片可以用-vframes
ffmpeg -i test.mp4 -t 5 -s 640x360 -r 15 frame%03d.jpg

picture to video

#将图片转为25帧率的mp4视频
ffmpeg -f image2 -i frame%02d.jpg -r 25 jpgtomp4.mp4

Guess you like

Origin blog.csdn.net/qq_38731735/article/details/124953904