ffmpeg 是一个非常流行且好用的图像、音视频处理工具。可以实现视频分割,截图,转码,合并,水印处理等常见媒体操作。它的作者也是一个非常厉害的人,感兴趣的同学可以查查。
【视频时长剪切】
# 如果编码格式采用的copy 最好加上
ffmpeg -ss 00:00:00.000 -to 00:10:00.000 -accurate_seek -i input.mp4 -c:v copy -c:a copy -avoid_negative_ts 1 -y result.mp4
【视频尺寸裁剪】
# width:宽度
# height:高度
# x:横向x的坐标点开始
# y:纵向y的坐标点开始
ffmpeg -i input.mp4 -vf crop=width:height:x:y -y result.mp4
【添加图片水印】
# 可以同时添加很多张图片
ffmpeg -i input.mp4 "-i image01.jpg -i image02.jpg" -filter_complex "overlay=x=50:y=50,overlay=x=100:y=100" -c:a copy -y result.mp4
【视频去水印】
# 可以同时去除多个范围
# x:坐标
# y:坐标
# width:宽度
# height:高度
ffmpeg -i input.mp4 -vf "delogo=x:y:width:height,delogo=x:y:width:height" -c:a copy -y result.mp4
【视频提取音频】
# 这里提取成mp3为例
ffmpeg -i input.mp4 -acodec libmp3lame -vn -y result.mp3
基于mobile-ffmpeg对于格式的支持度,是否支持主要看是否编译了对应的编码到ffmpeg库中
格式 | 编码器 | 说明 |
---|---|---|
mp3 | libmp3lame | 支持 |
wav | pcm_s16le | 支持 |
flac | flac | 支持 |
ac3 | ac3_fixed | 支持 |
aiff | pcm_s16be | 支持 |
m4a | libfdk_aac | 不支持 |
aac | libfdk_aac | 不支持 |
ra | ac3 | 支持 |
wma | wmav2 | 不支持 |
ogg | libvorbis | 支持 |
其它 | - | 未测试 |
【视频格式转换】
# 注意:假如目标格式是3gp格式,必须加上参数 -ac 1 -ar 8000
#preset:ultrafast、superfast、veryfast、faster、fast、medium、slow、slower、veryslow、placebo
ffmpeg -i input.mp4 -codec:v libx264 -preset superfast -b:v 1280K -s 宽x高 -r 帧率 -y result.mkv
【提取m3u8视频为mp4】
ffmpeg -allowed_extensions ALL -i https://xxx.xxx.com/aaa.m3u8 -codec:v libx264 -codec:a acc -async 2 -ar 44100 -c copy -threads 2 -y result.mp4
【视频转GIF】
# -r:指定帧率
# -s:指定分辨率
# -b:v:指定码率1000K
ffmpeg -i input.mp4 -r 15 -s 270x480 -y result.gif
【参看视频信息】
以JSON形式输出
# -v:调试信息级别(quiet、panic、fatal、error、warning、info、verbose、debug)
ffprobe -i input.mp4 -v quiet -print_format json -show_format -show_streams
# 样例
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "High",
"codec_type": "video",
"codec_time_base": "659/39360",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 592,
"height": 1280,
"coded_width": 592,
"coded_height": 1280,
"closed_captions": 0,
"has_b_frames": 1,
"pix_fmt": "yuv420p",
"level": 31,
"color_range": "tv",
"color_space": "bt709",
"color_transfer": "bt709",
"color_primaries": "bt709",
"chroma_location": "left",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"r_frame_rate": "30/1",
"avg_frame_rate": "19680/659",
"time_base": "1/600",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 26360,
"duration": "43.933333",
"bit_rate": "734637",
"bits_per_raw_sample": "8",
"nb_frames": "1312",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2022-04-19T08:03:25.000000Z",
"language": "und",
"handler_name": "Core Media Video"
}
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "LC",
"codec_type": "audio",
"codec_time_base": "1/44100",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "44100",
"channels": 1,
"channel_layout": "mono",
"bits_per_sample": 0,
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/44100",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 1936505,
"duration": "43.911678",
"bit_rate": "46494",
"max_bit_rate": "48000",
"nb_frames": "1894",
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0
},
"tags": {
"creation_time": "2022-04-19T08:03:25.000000Z",
"language": "und",
"handler_name": "Core Media Audio"
}
}
],
"format": {
"filename": "input.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "43.933333",
"size": "4317459",
"bit_rate": "786183",
"probe_score": 100,
"tags": {
"major_brand": "mp42",
"minor_version": "1",
"compatible_brands": "isommp41mp42",
"creation_time": "2022-04-19T08:03:25.000000Z",
"copyright": "",
"copyright-eng": ""
}
}
}
评论区