使用 ffmpeg 转换视频格式 mp4 webm


ffmpeg 是 *nix 系统下最流行的音视频处理库,功能强大,并且提供了丰富的终端命令,实是日常视频处理的一大利器!

实例

flac 格式转 mp3

音频格式转换非常简单:。

Python
<span class="wp_keywordlink_affiliate"><a href="https://www.168seo.cn/tag/ffmpeg" title="View all posts in ffmpeg" target="_blank">ffmpeg</a></span> -i input.flac -acodec libmp3lame output.mp3
1
2
ffmpeg - i input . flac - acodec libmp3lame output . mp3
 

ffmpeg 将会使用 libmp3lame 解码器将 input.flac 文件转换为 mp3 格式的 output.mp3 文件。

批量格式转换

想要批量转换 flac 文件也是很常见的需求,我们可以结合 bash 命令来完成:

Python
find . -name "*.flac" -exec bash -c 'ffmpeg -i "{}" -y "${0/.flac}.wav"' {} \;
1
2
find . - name "*.flac" - exec bash - c 'ffmpeg -i "{}" -y "${0/.flac}.wav"' { } \ ;
 

webm 转 gif 格式

webm 是 Google 提出的多媒体文件格式,包含了 VP8 影片轨和 Ogg Vorbis 音轨,按照 BSD 格式开源。webm 能够提供更高质量的在线视频,但是其支持程度并不如存在已久的 mp4 和动态图片格式 gif,因此有时候会有将其转换为 gif 格式的需求。我们用到的命令是:

Python
ffmpeg -i input.webm -vf "scale=400:-1,fps=10" output.gif
1
2
ffmpeg - i input . webm - vf "scale=400:-1,fps=10" output . gif
 

其作用是将 input.webm 格式的视频转换为横向宽度为 400px,纵向宽度保持比例的每秒 10 帧的 output.gif 图片。

当然,也可以将 gif 转换为 webm 格式:

Python
ffmpeg -i output.gif -c:v libvpx -auto-alt-ref 0 input.webm
1
2
ffmpeg - i output . gif - c : v libvpx - auto - alt - ref 0 input . webm
 

如果出现“Unknown encoder libvpx”的问题,需要重新在重新编译安装 ffmpeg 以支持 libvpx 编码器:brew reinstall ffmpeg --with-libvpx。

转换为 mp4 格式:

Python
ffmpeg -i input.webm -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output.mp4
1
2
ffmpeg - i input . webm - vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" output . mp4
 

这里 vf 参数的意义是,将视频的长宽强制转换为偶数,否则可能遇到“width not divisible by 2”的问题。

生成视频缩略图
生成封面
使用视频的第一帧作为封面:

Python
ffmpeg -i your-video.mp4 -ss 00:00:00 -vframes 1 thumb.png
1
2
ffmpeg - i your - video . mp4 - ss 00 : 00 : 00 - vframes 1 thumb . png
 

每 xx 秒生成一个缩略图
每分钟一张:

Python
ffmpeg -i your-video.mp4 -vf fps=1/60 your-video-%03d.png
1
2
ffmpeg - i your - video . mp4 - vf fps = 1 / 60 your - video - % 03d.png
 

再将其合并:

Python
ffmpeg -i your-video-%03d.png -filter_complex scale=-1:-1,tile=99x1:margin=10:padding=4 output.png
1
2
ffmpeg - i your - video - % 03d.png - filter_complex scale = - 1 : - 1 , tile = 99x1 : margin = 10 : padding = 4 output . png
 

这里的 tile 长度设定为 99,应当改为缩略图的总数。

一步到位:

Python
ffmpeg -ss 00:00:00 -i your-video.mp4 -vf 'select=not(mod(n\,24)),scale=-1:-1,tile=99*1' out.png
1
2
ffmpeg - ss 00 : 00 : 00 - i your - video . mp4 - vf 'select=not(mod(n\,24)),scale=-1:-1,tile=99*1' out . png
 

意思是从视频的 00:00:00 处开始,每 24 帧(一般动换都是 24 帧的)取一张图片,长宽不进行压缩,最后合成在 99*1 的方格中。




  • zeropython 微信公众号 5868037 QQ号 5868037@qq.com QQ邮箱
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值