FFMPEG简单例程

最近在处理HDR10格式的视频,在视频领域ffmpeg和开源解码器可以帮助我们解决大部分视频编解码和格式封装的问题。

ffmpeg -h之后我们会看到用户手册,提示我们明林需要遵照如下格式

usage: ffmpeg [options] [[infile options] -i infile]… {[outfile options] outfile}…

我们查看帮助,可以在 -h 后增加选项,来查看特定的帮助:

-h      -- print basic options
-h long -- print more options
-h full -- print all options (including all format and codec specific options, very long)
-h type=name -- print all options for the named decoder/encoder/demuxer/muxer/filter/bsf/protocol

编码

#图像序列转化为视频
ffmpeg -threads 2 -f image2 -i F:\\IdeaProjects\\ImageEnhance\\markerTestResult\\%03d.png  -vcodec h264 -r 30 -t 10 -b 16000000 output.mp4
#将HDR的YUV视频转化为MP4格式
ffmpeg -s 1920X1080 -r 60 -pix_fmt yuv420p10le -i Morocco_HDR_1920X1080_10bit.yuv -r 60 -crf 1 -c:v libx265 -x265-params "colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display=""G(13250,34500)B(7500,3000)R(34000,16000)WP(15635,16450)L(10000000,1)"":max-cll=""1000,400"":min-luma=0.001:max-luma=4000" -y Morocco_HDR.mp4

ffmpeg -s 1920X1080 -r 60 -pix_fmt yuv420p10le -i Morocco_HDR_1920X1080_10bit.yuv -r 60 -crf 1 -c:v libx265 -x265-params "colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc:master-display=""G(12250,35150)B(6899,2600)R(33899,16099)WP(15634,16449)L(10000000,0)"":max-cll=""1100,180"":min-luma=0.0000:max-luma=1000" -y Morocco_HDR.mp4

#HDR源转化为SDR
ffmpeg.exe -i 4k30P_HDR.mp4 -vf zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p -c:v libx265 -crf 18 -preset slower output.mkv

解码

 #普通封装格式的转换
ffmpeg -i input_video.avi output_video.mp4
# yuv之间不同格式互转,如果出现花屏现象,注意查看尺寸是否设置正确。
ffmpeg -s 1280*720 -pix_fmt yuv444p -i input.yuv -pix_fmt yuv420p output.yuv
# MP4转换成yuv 
ffmpeg -i input.MP4 -pix_fmt yuv420p output.yuv
# yuv转MP4
ffmpeg -s 1280*720 -pix_fmt yuv444p -i input.yuv output_video.mp4

转封装

保持编码格式:
ffmpeg -i test.mp4 -vcodec copy -acodec copy test_copy.ts
ffmpeg -i test.mp4 -codec copy test_copy2.ts

改变编码格式:
ffmpeg -i test.mp4 -vcodec libx265 -acodec libmp3lame out_h265_mp3.mkv

修改帧率:
ffmpeg -i test.mp4 -r 15 -codec copy output.mp4 (错误命令)
ffmpeg -i test.mp4 -r 15 output2.mp4

修改视频码率:
ffmpeg -i test.mp4 -b 400k output_b.mkv (此时音频也被重新编码)

修改视频码率:

ffmpeg -i test.mp4 -b:v 400k output_bv.mkv

修改音频码率:
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 480×270 output_480x270.mp4

修改音频采样率 :
ffmpeg -i test.mp4 -ar 44100 output_44100hz.mp4

图片和视频转换

截取一张图片
ffmpeg -i test.mp4 -y f image2 -ss 00:00:02 -vframes 1 -s 640×360 test.jpg
ffmpeg -i test.mp4 -y f image2 -ss 00:00:02 -vframes 1 -s 640×360 test.bmp
-i 输入
-y 覆盖
-f 格式
image2 一种格式
-ss 起始值
-vframes 帧 如果大于 1 那么 输出加 %03d test%03d.jpg
-s 格式大小 size

转换视频为图片(每帧一张图)
ffmpeg -i test.mp4 -t 5 -s 640×360 -r 15 frame%03d.jpg

图片转换为视频
ffmpeg -f image2 -i frame%03d.jpg -r 25 video.mp4

这里记得windows shell 是两个百分号

从视频中生成 GIF 图片
ffmpeg -i test.mp4 -t 5 -r 1 image1.gif
ffmpeg -i test.mp4 -t 5 -r 25 -s 640×360 image2.gif

将 GIF 转化为 视频
ffmpeg -f gif -i image2.gif image2.mp4

发表评论