Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
此篇文章只列举常见命令,不进行详细解释。
ffmpeg -ss 00:00:00 -t 00:00:30 -i test.mp4 -vcodec copy -acodec copy output.mp4
ffmpeg -i big.mov -vf scale=360:-1 small.mov
,
隔开并用 ""
包起来ffmpeg -i input.mp4 -acodec copy -vcodec libx264 -b 800k -vf transpose=1 output.mp4
1 | ffmpeg -i input.mp4 -vf “scale=640:360,pad=640:1138:0:389:black” -acodec copy -vcodec libx264 -s 640*1138 -b 2000k output.mp4 |
pad=w:h:x:y:color
1 | ffmpeg -i input.mp4 -vf crop=500:500:100:100 -acodec copy -vcodec libx264 -f mp4 output.mp4 |
crop=w:h:x:y
从 (x, y) 的位置开始取 w×h 大小的画面1 | ffmpeg -i input.mov -filter:v “setpts=0.5*PTS” output.mov |
1 | ffmpeg -i input.mov -r 16 output.mov |
1 | ffmpeg -re -i INPUT.mp4 -c copy -f flv rtmp://…… |
-re
表示以原生帧率读取数据。一般情况下 ffmpeg 会以尽可能快的速度读取数据,所以推流文件的话要加上此参数,如果是转推直播流的话,不用加此参数-c copy
表示音视频保持原有编码-f flv
表示输出的封装格式为 flv1 | ffmpeg -i rtmp://…… output.mp4 |
1 | ffmpeg -f avfoundation -list_devices true -i “” |
shell
1 2 3 4 5 | [AVFoundation indev @ 0x12f604290] AVFoundation video devices: [AVFoundation indev @ 0x12f604290] [0] FaceTime HD Camera [AVFoundation indev @ 0x12f604290] [1] Capture screen 0 [AVFoundation indev @ 0x12f604290] AVFoundation audio devices: [AVFoundation indev @ 0x12f604290] [0] MacBook Pro麦克风 |
可以看到我的电脑上视频设备有两个,0 是我电脑的摄像头,1 是我电脑的屏幕;音频设备有一个,是我电脑的麦克风
shell
1 | ffmpeg -f avfoundation -i 1 -r 30 output.mp4 |
-f avfoundation
表示从 avfoundation 采集数据-i 1
表示从设备 1 取数据,1 在我的电脑上代表屏幕-r 30
标识编码的帧率为 30shell
1 | ffmpeg -f avfoundation -i 1:0 -r 30 -c:v libx264 -crf 0 -c:a libfdk_aac -profile:a aac_he_v2 -b:a 32k out.flv |
同时录制电脑屏幕和麦克风
-c:v libx264
表示视频编码器选择 libx264,c 是 codec 的缩写,v 是 video 的缩写-crf 0
该选项是 x264 的参数, 0 表示无损压缩-c:a libfdk_aac
表示音频编码器选择 libfdk_aac-profile:a aac_he_v2
是 fdk_aac 的参数。 aac_he_v2 表式使用 AAC_HE v2 压缩数据-b:a
指定音频码率,b 是 bitrate 的缩写,,a 是 audio 的缩写shell
1 | ffmpeg -ss 00:00:16 -i input.mp4 -s 320×240 -frames:v 1 -f image2 output.jpeg |
-frames:v
表示选择视频轨道-f image2
表示输出的封装格式,image2 是 ffmpeg 中对图片的封装格式shell
1 | ffmpeg -loop 1 -i input.png -vcodec libx264 -b 500k -s 480*640 -r 15 -t 10 ouput.mp4 |
-loop
表示循环输入流,只工作于图像流-vcodec libx264
表示选择 libx264 编码器-b 500k
表示设置编码视频的比特率为 500k-s 480*640
表示设置编码视频的尺寸为 480×640-r 15
表示编码的帧率为 15-t 10
表示编码十秒的视频shell
1 | ffmpeg -i input.mp4 -vcodec copy -acodec copy output.flv |
-vcodec copy
-acodec copy
代表不重新编码音频和视频shell
1 | ffmpeg -i input.h264 -i input.aac -vcodec copy -acodec copy output.mp4 |
首先要建一个拼接文件 input.txt,文件内容如下:
plaintext
1 2 3 | file ‘1.mp4’ file ‘2.mp4’ file ‘3.mp4’ |
然后执行下面的命令:
shell
1 | ffmpeg -f concat -i inputs.txt -c copy output.mp4 |
shell
1 | ffmpeg -y -t 60 -i input.mp4 -i logo1.png -i logo2.png -filter_complex “overlay=x=if(lt(mod(t\,20)\,10)\,10\,NAN ):y=10,overlay=x=if(gt(mod(t\,20)\,10)\,W-w-10\,NAN ) :y=10” output.mp4 |
shell
1 | ffmpeg -i input1 -i input2 -vcodec libx264 -an -filter_complex “overlay=x=’if(gte(t,2), -w+(t-2)*20, NAN)’:y=0” -s 320×240 output.mp4 |
shell
1 | ffmpeg -i input.mp4 -vf “movie=logo.png[logo];[in][logo]overlay=x=’if(gte(t\,2)\,((t-2)*80)-w\,NAN)'” output.mp4 |
shell
1 | ffmpeg -i video.mpg -ignore_loop 0 -i loop.gif -filter_complex “[0:v][1:v]overlay=10:10:shortest=1” -vcodec mpeg2video out.mpg |
shell
1 | ffmpeg -i input -vf drawtext=fontfile=arial.ttf:text=welcome output |
shell
1 | ffmpeg -i input.mp4 -vf “drawtext=fontfile=font.ttf:text=’testtest’:x=w-100:y=100:enable=lt(mod(t\,3)\,1):fontsize=24:fontcolor=yellow@0.5:shadowy=2” output.mp4 |
shell
1 | ffmpeg -i 1.mp4 -vf “delogo=x=50:y=640:w=180:h=60:show=0” -c:a copy 2.mp4 |
shell
1 | ffmpeg -i input_file -i watermark_file -filter_complex ‘overlay=x=x坐标:y=y坐标’ output_file |
shell
1 | ffmpeg -i small.mp4 small.gif |
默认转化是中等质量模式,若要转化出高质量的 gif,可以修改比特率
shell
1 | ffmpeg -i small.mp4 -r 5 -b 2000k small.gif |
shell
1 | ffmpeg -ss 00:00:5 -t 7 -i input.mp4 -s 320×240 -f gif -r 15 output.gif |
从视频中第五秒开始,截取时长为 7 秒的片段转化为大小 320×240 帧率 15 的 gif
shell
1 | ffmpeg -f gif -i animation.gif animation.mp4 |
也可以将 gif 转为其他视频格式
shell
1 2 | ffmpeg -f gif -i animation.gif animation.mpeg ffmpeg -f gif -i animation.gif animation.webm |
安装 ImageMagick
shell
1 | brew install imagemagick |
提取第一帧
shell
1 | convert ‘animation.gif[0]’ animation-first-frame.gif |
shell
1 | ffmpeg -i input.mov -an mute-output.mov |
shell
1 | ffmpeg -i input.mp4 -acodec copy -vn out.mp3 |
-acodec copy
指定音频编码器,copy 指明只拷贝,不做编解码-vn
v 代表视频,n 代表 no,也就是无视频