WebM Web Video Encoding Tutorial With FFMpeg

本文提供了一种使用FFmpeg和libvpx库进行WebM(VP8/vorbis)视频编码的方法。首先,需要获取最新版本的FFmpeg和libvpx。然后选择分辨率和比特率,一般情况下比H.264视频比特率增加10-15%。最后进行编码,注意参数的顺序和设置,如-i指定输入文件,-codec:v选择libvpx VP8编码器,-quality和-cpu-used调整编码速度和质量,-b:v设定视频比特率等。文章还提供了多条样例命令行供参考。
摘要由CSDN通过智能技术生成


转自:https://www.virag.si/2012/01/webm-web-video-encoding-tutorial-with-ffmpeg-0-9/

Previously I wrote about encoding H.264 video for web. Since video support in browsers is kind of a mess, you’ll be forced to re-encode videos to WebM (VP8/vorbis) format sooner or later if you want HTML5 playback. FFMpeg is able to use the official libvpx library, which makes it pretty easy. However, the support is still slightly buggy and the parameter mapping is non-obvious if you’re used to encoding to other formats.

So, let’s start:

Step 1: Get new FFmpeg and libvpx versions

libvpx is a rather new addition to FFmpeg and for quality reasons you really want to use the newest stable version of FFmpeg and libvpx. How to get those was described in my previous article. Just don’t forget to compile in libvpx and libvorbis support.

Step 2: Choose resolution and bitrate

You’ll have to choose resolution and bitrate into which to encode. For general guidelines check my previous articleand add about 10-15% to video bitrate. libfaac and libvorbis output quality is mostly comparable, so you can just keep the same settings.

Step 3: Encode!

All the choices being made, now it’s time to encode your video:

ffmpeg -i input_file.avi -codec:v libvpx -quality good -cpu-used 0 -b:v 500k -qmin 10 -qmax 42 -maxrate 500k -bufsize 1000k -threads 4 -vf scale=-1:480 -codec:a libvorbis -b:a 128k output.webm
(I’m using new-style parameters introduced in latest FFmpegs, old-style parameters are being slowly deprecated because of ambiguity)

Two-pass encoding is of course also possible:

1st pass
1
ffmpeg -i input_file.avi -codec:v libvpx -quality good -cpu-used 0 -b:v 500k -qmin 10 -qmax 42 -maxrate 500k -bufsize 1000k -threads 4 -vf scale=-1:480 -an -pass 1 -f webm /dev/null`
2nd pass
1
ffmpeg -i input_file.avi -codec:v libvpx -quality good -cpu-used 0 -b:v 500k -qmin 10 -qmax 42 -maxrate 500k -bufsize 1000k -threads 4 -vf scale=-1:480 -codec:a libvorbis -b:a 128k -pass 2 -f webm output.webm

As mentioned in the previous article, FFmpeg parameters are position sensitive, so be sure to maintain the order of parameters.

Now, let’s break down the parameters for the encode:

-i [input file] - Specifies the name and path to the input file

-codec:v - Specifies the video encoder to be used, in our case that is libvpx VP8 library
-quality good - Sets encoding speed for the VP8 encoder. This works in concert with cpu-used parameter. Available values are bestgood and realtime. The official libvpx documentation strongly advises against usingbest quality parameter, because good with cpu-used 0 offers almost identical quality for half the encoding time.
-cpu-used [0-5] - Sets “speed” of encoding “ lower value uses more CPU for processing and produces better quality. Larger values trade quality for faster encoding, with 4 and 5 enabling “rate distortion optimization”, which significantly speeds-up encoding with price of big quality hit.
-b:v [bitrate] - Sets desired output video bitrate
-maxrate/-bufsize - Set upper limits for stream bitrate, maxrate specifying maximum bitrate and bufsize specifying device buffer size. Buffer size tells the encoder how much it can overshoot the maximum bitrate when required. Good default is twice the maxrate for approx. 2sec of buffer.
-qmin 10 -qmax 42 - This sets minimum and maximum quantization values. Since as of 0.9, FFmpeg sets those values wrong by default, adding these is required for a a decent video quality. Omitting these will produce blocky broken video.
-threads [num] - Sets number of encoding threads to use. Set to the number of your CPU cores available. -vf scale=[width:height] - Rescales video to a chosen resolution. Value of “-1″ means “size to keep aspect ratio”, e.g. setting this to “-1:720″ will produce a 720p output with same aspect ratio as input.

-codec:a libvorbis - Sets output audio encoder to libvorbis to produce vorbis output. This is required for a valid WebM file. -b:a [bitrate] - Sets bitrate for encoded audio. -an - disables audio, audio processing has no effect on first pass so itís best to disable it to not waste CPU.

-f webm - tells FFmpeg the output file format (required only if it cannot be inferred from output file extension). -pass [1|2] - tells FFmpeg to process video in multiple passes and sets the current pass.

That’s mostly all there is to it “ other FFmpeg mappings for libvpx can be found in the documentation. It’s also worth noting, that libvpx supports the “profile” parameter, which sets complexity of output stream, much like H.264 has. However, since devices with WebM support are very scarce, there currently isn’t much documentation on which devices supports which profile.

Some sample command lines:

These are analogous to the command lines in the H.264 article. Replace the thread count in -threads parameter with number of your CPU cores for optimal encoding speed.

Standardî web video (480p at 600kbit/s)
1
ffmpeg -i input_file.avi -codec:v libvpx -quality good -cpu-used 0 -b:v 600k -maxrate 600k -bufsize 1200k -qmin 10 -qmax 42 -vf scale=-1:480 -threads 4 -codec:a vorbis -b:a 128k output_file.webm
High-quality SD video for archive/storage (PAL at 1.2Mbit/s):
1
ffmpeg -i input_file.avi -codec:v libvpx -quality good -cpu-used 0 -b:v 1200k -maxrate 1200k -bufsize 2400k -qmin 10 -qmax 42 -vf scale=-1:480 -threads 4 -codec:a vorbis -b:a 128k output_file.webm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值