ffmpeg笔记(三)ffmpeg指令总结

参数汇总:

-i 设定输入流
-f 设定输出格式(format)
-ss 开始时间
-t 时间长度
-aframes 设置要输出的音频帧数
-b:a 音频码率
-ar 设定采样率
-ac 设定声音的Channel数
-acodec 设定声音编解码器,如果用copy表示原始编解码数据必须被拷贝。
-an 不处理音频
-af 音频过滤器
-vframes 设置要输出的视频帧数
-b 设定视频码率
-b:v 视频码率
-r 设定帧速率
-s 设定画面的宽与高
-vn 不处理视频
-aspect aspect 设置横纵比 4:3 16:91.3333 1.7777
-vcodec 设定视频编解码器,如果用copy表示原始编解码数据必须被拷贝。
-vf 视频过滤器

播放本地文件

ffplay -window_title "sunx test_time" -ss 2 -t 10 -autoexit test.mp4(-ss表示起始秒)

强制解码器

mpeg4解码器:ffplay  -vcodec mpeg4 test.mp4(使用mpeg4转码)
h264解码器:ffplay -vcodec h264 test.mp4  (使用h264转码)

禁用音频或视频

禁用音频:ffplay test.mp4 -an (audio)
禁用视频:ffplay test.mp4 -vn (video)

播放YUV数据

ffplay -pixel_format yuv420p -video_size 320x240 -framerate 5  yuv420p_320x240.yuv

播放RGB数据

ffplay -pixel_format rgb24 -video_size 320x240 -i rgb24_320x240.rgb
ffplay -pixel_format rgb24 -video_size 320x240 -framerate 5  -i rgb24_320x240.rgb

播放PCM数据

ffplay -ar 48000 -ac 2 -f f32le 48000_2_f32le.pcm
 -ar        set audio sampling rate (in Hz) (from 0 to INT_MAX) (default 0)
 -ac      set number of audio channels (from 0 to INT_MAX) (default 0)

yuv测试:

1、获取YUV:ffmpeg -i test.mp4  -pix_fmt yuv420p yuv420p_orig.yuv(test分辨率为1280*720)
2、播放YUV:  ffplay -pix_fmt yuv420p -video_size 1280*720 -framerate 5 yuv420p_orig.yuv

保留封装格式,只要音频:

ffmpeg -i test.mp4 -acodec copy -vn audio.mp4

保留封装格式,只要视频:

ffmpeg -i test.mp4 -vcodec copy -an video.mp4

h264测试(仅提取视频):

1.1、ffmpeg -i test.mp4 -vcodec copy -an test_copy.h264(保留编码格式)
1.2、ffplay copy.h264
2.1、ffmpeg -i test.mp4 -vcodec libx264 -an test.h264(强制格式)
2.2、ffplay test.h264

acc测试(仅提取音频):

1.1、ffmpeg -i test.mp4 -acodec copy -vn test.aac(保留编码格式)
1.2、ffplay test.aac
2.1、ffmpeg -i test.mp4 -acodec libmp3lame -vn test.mp3(强制格式)
2.2、ffplay test.mp3

音频测试:

1、获取音频: ffmpeg -i test.mp4 -vn -ac 2 -f f32le 48000_2_f32le_2.pcm
2、播放音频:ffplay -ar 48000 -ac 2 -f f32le  48000_2_f32le_2.pcm

视频向右旋转90度

ffplay -i test.mp4 -vf transpose=1

视频左右翻转

ffplay test.mp4 -vf hflip

视频上下颠倒

ffplay test.mp4 -vf vflip

视频旋转和反转

ffplay test.mp4 -vf hflip,transpose=1

音频2倍速播放

ffplay -i test.mp4 -af atempo=2

视频2倍速播放

ffplay -i test.mp4 -vf setpts=PTS/2

音视频同时2倍速

ffplay -i test.mp4 -vf setpts=PTS/2 -af atempo=2

ts测试(保持编码格式)

ffmpeg -i test.mp4 -vcodec copy -acodec copy test_copy.ts
fflpay test_copy.ts
ffmpeg -i test.mp4 -codec copy test_copy2.ts
ffplay test_copy2.ts

ts测试(改变编码格式)

ffmpeg -i test.mp4 -vcodec libx265 -acodec libmp3lame out_h265_mp3.ts
ffplay out_h265_mp3.ts

提取ES流

ffmpeg -i 1.ts -vcodec copy -an -f rawvideo es.raw

若输入文件为MP4 + H264,需要加-vbsf h264_mp4toannexb选项(H265为 -vbsf hevc_mp4toannexb )将AVCC(HVCC)格式转为Annex B

ffmpeg -i 1.mp4 -vcodec copy -an -f rawvideo -vbsf h264_mp4toannexb es.raw

修改帧率:

ffmpeg -i test.mp4 -r 15 -codec copy output.mp4 (错误命令)
ffmpeg -i test.mp4 -r 15 output2.mp4

修改音频码率

ffmpeg -i test.mp4 -b:a 192k output_ba.mp4

如果不想重新编码video,需要加上-vcodec copy

修改音视频码率

ffmpeg -i test.mp4 -b:v 400k -b:a 192k output_bva.mp4

修改视频分辨率

ffmpeg -i test.mp4 -s 480x270 output_480x270.mp4

修改音频采样率:

ffmpeg -i test.mp4 -ar 44100 output_44100hz.mp4

以MP4格式进行拼接

方法2:ffmpeg -fconcat -i mp4list.txt -codec copy out_mp42.mp4

以TS格式进行拼接

方法1:ffmpeg -i"concat:1.ts|2.ts|3.ts" -codec copy out_ts.mp4   
方法2:ffmpeg -fconcat -itslist.txt -codec copy out_ts2.mp4

以FLV格式进行拼接

ffmpeg -fconcat -iflvlist.txt -codec copy out_flv2.mp4

修改音频编码

ffmpeg -i 2.mp4 -vcodec copy -acodec ac3 -vbsf h264_mp4toannexb 2.ts

修改音频采样率

ffmpeg -i 2.mp4 -vcodec copy -acodec aac -ar 96000 -vbsf h264_mp4toannexb 2.ts

修改视频编码格式

ffmpeg -i 1.mp4 -acodec copy -vcodec libx265 1.ts

修改视频分辨率

ffmpeg -i 1.mp4 -acodec copy -vcodec libx264 -s 800x472 -vbsfh264_mp4toannexb 1.ts

截取一张图片

ffmpeg -i test.mp4 -y -f image2 -ss 00:00:02 -vframes 1 -s 640x360 test.jpg
ffmpeg -i test.mp4 -y -f image2 -ss 00:00:02 -vframes 1 -s 640x360 test.bmp

参数说明:
-i 输入
-y 覆盖
-f 格式 image2 一种格式
-ss 起始值
-vframes 帧 如果大于1 那么 输出加%03d test%03d.jpg
-s 格式大小size
-vf 视频过滤器
-af 音频过滤器

视频转换为图片(-r表示帧率,可修改):

ffmpeg -i test.mp4 -t 5 -s 640x360 -r 15 frame%03d.jpg
ffmpeg -i test.mp4 -t 5 -s 640x360 -r 15 image2.gif

图片转换为视频:

ffmpeg -f image2 -i frame%03d.jpg-r 25 video.mp4
ffmpeg -f gif -i image2.gif  image2.mp4

将文字的水印加在视频的左上角:

ffplay -i test.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':x=20:y=20"

将字体的颜色设置为绿色:

ffplay -i test.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':fontcolor=green"

如果想调整文字水印显示的位置,调整x与y参数的数值即可。

ffplay -i test.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':fontcolor=green:x=400:y=200"

修改透明度

ffplay -i test.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':fontcolor=green:x=400:y=200:alpha=0.5"

文字水印还可以增加一个框,然后给框加上背景颜色:

ffplay -i test.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='hello world':fontcolor=green:box=1:boxcolor=yellow"

每3秒钟显示一次文字水印:

ffplay -i test.mp4 -vf "drawtext=fontsize=60:fontfile=FreeSerif.ttf:text='test':fontcolor=green:box=1:boxcolor=yellow:enable=lt(mod(t\,3)\,1)"

文字水印跑马灯效果:

ffplay -i test.mp4 -vf "drawtext=fontsize=100:fontfile=FreeSerif.ttf:text='helloworld':x=mod(100*t\,w):y=abs(sin(t))*h*0.7"

修改字体透明度,修改字体颜色

ffplay -i test.mp4 -vf "drawtext=fontsize=40:fontfile=FreeSerif.ttf:text='liaoqingfu':x=mod(50*t\,w):y=abs(sin(t))*h*0.7:alpha=0.5:fontcolor=white:enable=lt(mod(t\,3)\,1)"

添加图片水印:

ffmpeg -i test.mp4 -vf "movie=test.jpg[watermark];[in][watermark]overlay=x=10:y=10[out]" output.mp4

Ø 原始视频文件路径:input.mp4
Ø 水印图片路径:logo.png
Ø 水印位置:(x,y)=(10,10)<=(left,top)距离左侧、顶部各10像素;
Ø 输出文件路径:output.mp4

在FFmpeg中加入图片水印有两种方式,一种是通过movie指定水印文件路径,另外一种方式是通过filter读取输入文件的流并指定为水印

图片logo.png将会打入到input.mp4视频中,显示在x坐标50、y坐标20的位置

ffplay -i test.mp4 -vf "movie=test.jpg[logo];[in][logo]overlay=50:10[out]"

左上水印:

ffplay -i test.mp4 -vf "movie=test.jpg[watermark];[in][watermark]overlay=10:10[out]"

右上水印:

ffplay -i test.mp4 -vf "movie=test.jpg[watermark];[in][watermark]overlay=main_w-overlay_w-10:10[out]"

左下水印:

ffplay -i test.mp4 -vf "movie=test.jpg[watermark];[in][watermark]overlay=10:main_h-overlay_h-10[out]"

右下水印:

ffplay -i test.mp4 -vf "movie=test.jpg[watermark];[in][watermark]overlay=main_w-overlay_w-10:main_h-overlay_h-10[out]"

图片水印跑马灯效果

ffplay -i test.mp4 -vf "movie=test.jpg[watermark];[in][watermark]overlay=x=mod(50*t\,main_w):y=abs(sin(t))*h*0.7[out]"

显示画中画效果

ffplay -i test.mp4 -vf "movie=sub_test.mp4[sub];[in][sub]overlay=x=20:y=20[out]"

遇到eof表示时的处理方式,默认为重复

Ø  repeat(值为0):重复前一帧
Ø  endcall(值为1):停止所有的流
Ø  pass(值为2):保留主图层
ffplay -i test.mp4 -vf "movie=sub_test.mp4[sub];[in][sub]overlay=x=20:y=20:eof_action=1[out]"
shortest: 终止最短的视频时全部终止(默认false)
ffplay -i test.mp4 -vf "movie=sub_test.mp4[sub];[in][sub]overlay=x=20:y=20:shortest =1[out]"

视频画中画跑马灯:

ffplay -i test.mp4 -vf "movie=sub_test.mp4[test];[in][test]overlay= x=mod(50*t\,main_w):y=abs(sin(t))*main_h*0.7[out]"

多宫格视频文件:

ffmpeg -i test1.mp4 -i test2.mp4 -i  test3.mp4 -i  test4.mp4 -filter_complex "nullsrc=size=640x480[base];[0:v] setpts=PTS-STARTPTS,scale=320x240[upperleft];[1:v]setpts=PTS-STARTPTS,scale=320x240[upperright];[2:v]setpts=PTS-STARTPTS, scale=320x240[lowerleft];[3:v]setpts=PTS-STARTPTS,scale=320x240[lowerright];[base][upperleft]overlay=shortest=1[tmp1];[tmp1][upperright]overlay=shortest=1:x=320[tmp2];[tmp2][lowerleft]overlay=shortest=1:y=240[tmp3];[tmp3][lowerright]overlay=shortest=1:x=320:y=240" out.mp4

只叠加左上和右上,左下和右下不叠加文件:

ffmpeg -i test1.mp4 -i test2.mp4 -i  test3.mp4 -i  test4.mp4 -filter_complex "nullsrc=size=640x480[base];[0:v]setpts=PTS-STARTPTS,scale=320x240[upperleft];[1:v]setpts=PTS-STARTPTS,scale=320x240[upperright];[base][upperleft]overlay=shortest=1[tmp1];[tmp1][upperright]overlay=shortest=1:x=320" out2.mp4

使用[0:v][1:v][2:v][3:v]将输入的4个视频流去除,分别进行缩放处理,然后基于nullsrc生成的画布进行视频平铺,命令中自定义upperleft,upperright,lowerleft,lowerright进行不同位置平铺。

ffmpeg直播推流:

ffmpeg -re i test.mp4 -c copy flv rtmp://server/live/streamName

ffmpeg直播拉流

ffplay rtmp://server/live/streamName
ffmpeg -i rtmp://server/live/streamName -c copy dump.flv

找三个不同的视频每个视频截取10秒内容

ffmpeg -i test1.mp4 -ss 00:05:00 -t 10 -codec copy 1.mp4
ffmpeg -i test2.mp4 -ss 00:05:00 -t 10 -codec copy 2.mp4
ffmpeg -i test3.mp4 -ss 00:05:00 -t 10 -codec copy 3.mp4

如果音视频格式不统一则强制统一为 -vcodec libx264 -acodec aac

将上述1.mp4/2.mp4/3.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格式

ffmpeg -i 1.mp4 -codec copy 1.flv
ffmpeg -i 2.mp4 -codec copy 2.flv
ffmpeg -i 3.mp4 -codec copy 3.flv
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值