音视频】5.ffmpeg命令分类与使用

GT
其实平时也有一些处理音视频的个人或者亲人需求,熟练使用ffmpeg之后也不要借助图示化软件,一个命令基本可以搞定

G: 熟练使用ffmpeg命令!
T :不要死记硬背,看一遍,自己找下规律,敲一遍,用的多了,自然而然就记住了。就算忘了,我也可以查一查_

ffmpeg提供的命令行工具
ffprobe:用作信息查询,查看音视频文件中的信息
ffplay:是一个使用了FFmpeg和SDL库的媒体播放器,ijkplayer 就是基于ffplay改造二次开发的,多做了一些硬件解码和兼容性的工作。
ffmpeg:最重要,最核心。包含各种功能,学习也主要是学习这玩意。
ffserver:简单的流媒体服务器,能够响应客户端的流媒体请求和把数据流发送给客户端。
ffprobe
ffprobe [fileName] :直接输出文件信息,很明显,可以看出代表的含义

# 时长、开始、比特率
Duration: 00:01:39.96, start: 0.000000, bitrate: 1035 kb/s

# video流,编码格式h264 (封装格式avc1),视频帧格式为yuv420p,分辨率1000x562,比特率、帧率,default代表默认的视频流,可能有多路视频流。
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 1000x562 [SAR 1:1 DAR 500:281], 935 kb/s, 24 fps, 24 tbr, 12288 tbn, 48 tbc (default)

# 音频流,编码格式aac(封装格式MP4A),采用Profile是LC,采样率44.1kHZ,stereo立体声,数据表示格式浮点型,比特率
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 94 kb/s (default)
1
2
3
4
5
6
7
8
ffprobe -show_format [fileName]

ffprobe -show_packets [fileName]

ffprobe -show_frames [fileName]

ffprobe -print_format json -show_streams [fileName]

属性 含义
media_type 帧的类型(a,v,字幕等)
pict_type 帧类型(IPB)
pic_fmt 帧的图像色彩格式(yuv420p)
Key_frame 是否是关键帧
ffplay
播放: ffplay [fileName]
直接播放文件,播放同时会打印音视频流信息,与上面通过ffprobe查看到的一致
1.seek:播放窗口点击
2.快进快退:左右键10s,上下键1min
3.音视频切换:W
4.frame-step:S

循环播放:ffplay [fileName] -loop [times]

切换音视频流:ffplay [fileName] ast/vst [index]

播放音视频裸数据PCM/YUV:
ffplay .pcm -f s161e -channels 2 -ar 44100
ffplay -f rawvideo -pixel_format yuv420p/rgb24 -s 1920*1080 [fileName]

音视频同步,以音/视频/外部时钟为参考
ffplay [fileName] -sync audio/video/ext

ffmpeg:最美味的鱼,也是刺最多的。
ffmpeg -help:可以考到如下几类参数
# 1.Global options 全局生效,而不是只针对个别文件
# 2.Per-file main options:操作每个文件单独生效
-f fmt 指定音视频格式
-c codec codec name
-codec codec codec name
-pre preset preset name
-map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile
-t duration 指定时长
-to time_stop record or transcode stop time
-fs limit_size 指定文件大小的上限
-ss time_off 指定开始时间
-sseof time_off set the start time offset relative to EOF
-seek_timestamp enable/disable seeking by timestamp with -ss
-timestamp time set the recording timestamp ('now' to set the current time)
-metadata string=string add metadata
-program title=string:st=number... add program with specified streams
-target type specify target file type ("vcd", "svcd", "dvd", "dv" or "dv50" with optional prefixes "pal-", "ntsc-" or "film-")
-apad audio pad
-frames number set the number of frames to output
-filter filter_graph set stream filtergraph
-filter_script filename read stream filtergraph description from a file
-reinit_filter reinit filtergraph on input parameter changes
-discard discard
-disposition disposition

# 3. Video options: 视频参数
-vframes number set the number of video frames to output
-r rate 帧率fps
-s size 分辨率
-aspect aspect 指定视频长宽比(4:3,16:9或1.3333,1.7777)
-bits_per_raw_sample number set the number of bits per raw sample
-vn 取消视频的输出
-vcodec codec 强制使用codec编解码方式(‘copy’ 代表不进行重新编码)
-timecode hh:mm:ss[:;.]ff set initial TimeCode value.
-pass n select the pass number (1 to 3)
-vf filter_graph set video filters
-ab bitrate audio bitrate (please use -b:a)
-b bitrate video bitrate (please use -b:v)
-dn disable data

# 4. Audio options: 音频参数
-aframes number set the number of audio frames to output
-aq quality 设置音频质量 (指定编码)
-ar rate 设置音频采样率 (单位Hz)
-ac channels 设置声道数 1单声道 2立体声
-an 取消音频输出
-acodec codec 指定音频编码 ('copy' 指不做音频转码,直接复制)
-vol volume 设置录制音量大小 (256=normal)
-af filter_graph set audio filters

# 5. Subtitle options
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
查询类命令
-version show version
-buildconf show build configuration
-formats show available formats
-muxers show available muxers
-demuxers show available demuxers
-devices show available devices
-codecs show available codecs
-decoders show available decoders
-encoders show available encoders
-bsfs show available bit stream filters
-protocols show available protocols
-filters show available filters
-pix_fmts show available pixel formats
-layouts show standard channel layouts
-sample_fmts show available audio sample formats
-colors show available color names
-sources device list sources of the input device
-sinks device list sinks of the output device
-hwaccels show available HW acceleration methods

命令格式
ffmpeg [global_options] {[input_file_options] -i input_url} {[output_file_options] output_url}

复用解复用命令
抽取音频流
ffmpeg -i input.mp4 -acodec copy -vn out.aac

抽取视频流
ffmpeg -i input.mp4 -vcodec copy -an out.h264

格式转换
ffmpeg -i input.mp4 -vcodec copy -acodec copy out.flv

音视频合并
ffmpeg -i out.h264 -i out.aac -vcodec copy -acodec copy out.mp4

处理裸数据
#提取YUV数据
ffmpeg -i input.mp4 -an -c:v rawvideo -pixel_format yuv420p out.yuv

#提取PCM数据
ffmpeg -i input.mp5 -vn -ar 44100 -ac 2 -f s16le out.pcm

#yuv转h264
ffmpeg -f rawvideo -pix_fmt yuv420p -s 320x240 -r 30 -i out.yuv -c:v libx264 -f rawvideo out.h264

#pcm转wav
ffmpeg -s16le -ar 8000 -ac 2 -acodec pcm_s16le -i input.raw output.wav

滤镜
#添加水印:-vf中的 movie 指定logo位置。scale 指定 logo 大小。overlay 指定 logo 摆放的位置
ffmpeg -i 1.mp4 -vf “movie=2.png,scale=30:30[watermask];[in][watermask] overlay=30:10[out]” water.mp4

#删除水印
1.找到水印
ffplay -i water.mp4 -vf delogo=x=30:y=10:w=32:h=31:show=1
2.删除水印
ffmpeg -i water.mp4 -vf delogo=x=30:y=10:w=32:h=31output.mp4

#视频缩小1倍:-vf scale 指定使用简单过滤器 scale,iw/2:-1 中的 iw 指定按整型取视频的宽度。 -1 表示高度随宽度一起变化。
ffmpeg -i 1.mp4 -vf scale=iw/2:-1 scale.mp4

视频裁剪
crop 格式:crop=out_w:out_h: x :y
out_w: 输出的宽度。可以使用 in_w 表式输入视频的宽度。
out_h: 输出的高度。可以使用 in_h 表式输入视频的高度。
x : X坐标
y : Y坐标
ffmpeg -i 1.mp4 -vf crop=in_w-200:in_h-200 -c:v libx264 -c:a copy -video_size 1280x720 out.mp4

倍速播放
-filter_complex 复杂滤镜,[0:v]表示第一个(文件索引号是0)文件的视频作为输入。setpts=0.5PTS表示每帧视频的pts时间戳都乘0.5 ,也就是差少一半。[v]表示输出的别名。音频同理就不详述了。
-map 可用于处理复杂输出,如可以将指定的多路流输出到一个输出文件,也可以指定输出到多个文件。"[v]" 复杂滤镜输出的别名作为输出文件的一路流。上面 map的用法是将复杂滤镜输出的视频和音频输出到指定文件中。
ffmpeg -i 1.mp4 -filter_complex "[0:v]setpts=0.5PTS[v];[0:a]atempo=2.0[a]" -map “[v]” -map “[a]” speed.mp4

对称视频(水平翻转)
ffmpeg -i 1.mp4 -filter_complex “[0:v]pad=w=2*iw[a];[0:v]hflip[b];[a][b]overlay=x=w” duicheng.mp4

对称视频(垂直翻转)
ffmpeg -i 1.mp4 -filter_complex “[0:v]pad=h=2*ih[a];[0:v]vflip[b];[a][b]overlay=y=h” duicheng.mp4

画中画
ffmpeg -i 1.mp4 -i 2.mp4 -filter_complex “[1:v]scale=w=176:h=144:force_original_aspect_ratio=decrease[ckout];[0:v][ckout]overlay=x=W-w-10:y=0[out]” -map “[out]” -movflags faststart new.mp4

音量效果
ffmpeg -i 1.mp4 -af ‘volume=0.5’ out.mp4

音视频的拼接与裁剪
裁剪
ffmpeg -i 1.mp4 -ss 00:00:00 -t 10 out1.mp4

拼接
text文件:
file ‘out1.flv’
file ‘out2.flv’
file ‘out3.flv’
ffmpeg -f concat -i text -c copy pinjie.mp4

hls切片
-strict -2 指明音频使用AAC。
-f hls 转成 m3u8 格式。
ffmpeg -i 1.mp4 -c:v libx264 -c:a libfdk_aac -strict -2 -f hls out.m3u8

视频图片互转
视频转JPEG
-r 1:1s一张
ffmpeg -i 1.mp4 -r 1 -f image2 image-%3d.jpeg

图片转gif
ffmpeg -i image-%3d.jpeg -r 5 out.gif

视频转gif图
ffmpeg -i 1.mp4 -ss 00:00:00 -t 10 out.gif

图片转视频
ffmpeg -f image2 -i image-%3d.jpeg images.mp4

直播相关
推流
ffmpeg -re -i out.mp4 -c copy -f flv rtmp://server/live/streamName

拉流保存
ffmpeg -i rtmp://server/live/streamName -c copy dump.flv

转流
ffmpeg -i rtmp://server/live/originalStream -c:a copy -c:v copy -f flv rtmp://server/live/h264Stream

水平有限,能力一般,且处于学习阶段,博客大概为自己学习的记录,难免有理解偏差,欢迎留言指正!
生命不止,学习不息!
--------------------- 

猜你喜欢

转载自www.cnblogs.com/hyhy904/p/10983107.html