1.命令相关参数
cpp
主要参数:
-i 设定输入流
-f 设定输出格式
-ss 开始时间
-t 时间长度
音频参数:
-aframes 设置要输出的音频帧数
-b:a 音频码率
-ar 设定采样率
-ac 设定声音的Channel数
-acodec 设定声音编解码器
-an 不处理音频数据
-af 音频过滤器
视频参数:
-vframes 设置要输出的视频的帧数
-b 设定视频码率
-b:v 设定视频码率
-r 设定帧速率
-s 设定画面的宽和高
-vn 不处理视频
-aspect 设置纵横比
-vcodec 设定视频编码器
-vf 视频过滤器
2.命令
(1)数据提取
cpp
提取音视频数据:
//保留封装格式
ffmepg -i test.mp4 -acodec copy -vn audio.mp4
ffmepg -i test.mp4 -vcodec copy -an video.mp4
//提取视频
ffmpeg -i test.mp4 -vcodec copy -an test_copy.h264 //保留编码格式
ffmpeg -i test.mp4 -vcodec libx264 -an test.h264 //强制格式
//提取音频
ffmepg -i test.mp4 -acodec copy -vn test_copy.aac //保留编码格式
ffmepg -i test.mp4 -acodec libmp3lame -vn test.mp3 //强制格式
提取像素格式
//提取YUV
ffmepg -i test.mp4 -t 3 -pix_fmt yuv420p yuv420p_orig.yuv
ffmepg -i test.mp4 -t 3 -pix_fmt yuv420p -s 320x240 yuv420p_320x240.yuv
//提取RGB
ffmepg -i test.mp4 -t 3 -pix_fmt rgb24 -s 320x240 rgb24_320x240.rgb
//RGB与YUV之间的转化
ffmpeg -pix_fmt yuv420p -s 320x240 -i yuv420p_320x240.yuv -pix_fmt rgb24 rgb24_320x240_2.rgb
提取PCM数据
ffmepg -i test.mp3 -ar 48000 -ac 2 -f s16le 48000_2_s16le
ffmepg -i test.mp3 -ar 48000 -ac 2 -sample_fmt s16 out_s16.wav
ffmepg -i test.mp3 -ar 48000 -ac 2 -codec:a pcm_s16le out2_s16le.wav
ffmepg -i test.mp3 -ar 48000 -ac 2 -f s32le 48000_2_s32le
(2)转封装
cpp
转封装:
//保持编码格式
ffmepg -i test.mp4 -vcodec copy -acodec copy test_copy.ts
ffmepg -i test.mp4 -codec copy test_copy2.ts
//改变编码格式
ffmepg -i test.mp4 -vcodec libx265 -acodec libmp3lame out_265_mp3.mkv
//修改帧率
ffmepg -i test.mp4 -r 15 output.mp4
//修改视频码率
ffmepg -i test.mp4 -b 400k output_b.mkv
ffmepg -i test.mp4 -b:v 400k output_bv.flv
//修改音频码率
ffmepg -i test.mp4 -b:a 192k output_ba.mp4
ffmepg -i test.mp4 -b:v 400k -b:a 192k output_bva.mp4
//修改视频分辨率
ffmepg -i test.mp4 -s 480x270 output_480x270.mp4
//修改音频采样率
ffmepg -i test.mp4 -ar 44100 output_44100.mp4
(3)剪裁和合并
cpp
生成测试文件:
//找到3个不同的视频每个视频截取10s内容
ffmepg -i test1.mp4 -ss 00:01:00 -t 10 -codec copy 1.mp4
ffmepg -i test2.mp4 -ss 00:01:00 -t 10 -codec copy 2.mp4
ffmepg -i test3.mp4 -ss 00:01:00 -t 10 -codec copy 3.mp4
//将上述的mp4格式转化为ts格式
ffmpeg -i 1.mp4 -codec copy -vbsf h264_mp4toannexb 1.ts
ffmpeg -i 2.mp4 -codec copy -vbsf h264_mp4toannexb 2.ts
ffmpeg -i 3.mp4 -codec copy -vbsf h264_mp4toannexb 3.ts
//转成flv格式
ffmepg -i 1.mp4 -codec copy 1.flv
ffmepg -i 2.mp4 -codec copy 2.flv
ffmepg -i 3.mp4 -codec copy 3.flv
拼接文件:
//以MP4格式拼接
ffmepg -f concat -i mp4list.txt -codec copy out_mp4.mp4
//以ts格式拼接
ffmepg -i "concat:1.ts|2.ts|3.ts" -codec copy out_ts.mp4
ffmepg -f concat -i tslist.txt -codec copy out_ts.mp4
//以flv格式进行拼接
ffmepg -f concat -i flvlist.txt -codec copy out_flv.mp4
(4)图片视频互转
cpp
图片与视频转化:
//截取图片
ffmepg -i test.mp4 -y -f image2 -ss 00:00:02 -vframes 1 -s 640x360 test.jpg
ffmepg -i test.mp4 -y -f image2 -ss 00:00:02 -vframes 1 -s 640x360 test.bmp
//转化视频为图片(每帧一张图)
ffmepg -i test.mp4 -t 5 -s 640x360 -r 15 frame%03d.jpg
//图片转化为视频
ffmepg -f image2 -i frame%03d.jpg -r 25 video.mp4
//从视频中生成gif图片
ffmepg -i test.mp4 -t 5 -r 1 image1.gif
ffmepg -i test.mp4 -t 5 -r 25 -s 640x360 image2.gif
//将gif转化成视频
ffmepg -f gif -i image2.gif video.mp4
(5)直播
cpp
直播拉流:
ffplay rtmp://server/live/streamName
ffmpeg -i rtmp://server/live/streamName -c copy dump.flv
直播推流:
ffmepg -re -i out.mp4 -c copy flv rtmp://server/live/streamName