I am looking to automate way开发者_开发技巧s to encoding my videos for HTML5 playback. Miro converter does great job but I am looking to automate the process.
What are some great paid/free APIs which can be used to achieve encoding?
While I am unaware of any service that encodes everything you need, you can make your own fairly simply. For full support on the web of all html5 browsers, you'll need to encode in (at least) 2 different formats.
h264 is supported by IE9, Safari, Mobile Safari (iPhone) and Android. (I think Chrome may support this, but may not in the future. Politics.)
Theora is supported be Opera, Mozilla (Firefox) and Chrome.
You can use ffmpeg to encode to both these formats, so it is entirely possible to create a batch converter on your own.
Here's a few commands to get you started:
ffmpeg -i input -pass 1 -vcodec libx264 -preset fast -b 512k -threads 0 -f mp4 -an -y /dev/null && ffmpeg -i [INPUT FILE] -pass 2 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -preset fast -b 512k -threads 0 output.mp4
That will do a 2-pass encode to mp4/aac h264. Play with the -b option for a higher and lower bitrate (and filesize). The -ab option is your audio, 128k should be plenty, but if you notice it sounding bad you can try using 256k.
Theora is usally trickier to set up, but there's a good tool called ffmpeg2theora that will make the conversion process a little easier. It can be found here : http://v2v.cc/~j/ffmpeg2theora/
usage is a little simpler than ffmpeg, something like this: ffmpeg2theora -v 7 -a 3 [INPUT FILE]
, where -v is video quality (of 10) and -a is audio quality (of 10).
in both examples, replace [INPUT FILE]
with your input, for instance myvideo.avi
.
I recommend playing around with both tools to get a feel for them, they're both powerful and once you get the hang of it pretty easy to use.
Once you want to make a batch processing file for it, one of the simplest ways to do that is to write a quick shell script. Here's an example:
#!/bin/bash
# super simple batch video script
ffmpeg -i input -pass 1 -vcodec libx264 -preset fast -b 512k -threads 0 -f mp4 -an -y /dev/null && ffmpeg -i $1 -pass 2 -acodec libfaac -ab 128k -ac 2 -vcodec libx264 -preset fast -b 512k -threads 0 output.mp4 && ffmpeg2theora -v 7 -a 3 $1
save it as, say encode.sh
.
then, make it executable: chmod 755 encode.sh
and run it with an argument of a path to a video: sh encode.sh "/path/to/video.avi"
The only things in that script that may be unfamiliar are:
#!/bin/bash
- this tells sh to run this script in the bash shell.
$1
- this will be the first argument passed to the bash shell. In the above case, the video file path.
&&
- this tells bash to wait until the last command is done, then continue. You can chain many commands together this way.
Hope that helps.
FFMpeg is the command-line utility on which Miro is based, so you might want to start there.
Zencoder is a cloud encoding API that can do all the HTML5 formats.
http://www.bitcodin.com can generate MPEG-DASH and HLS content, which can be played natively in HTML5 by different browsers. There is a comprehensive tutorial on the content generation: http://www.bitcodin.com/blog/2015/02/create-mpeg-dash-hls-content-for-amazon-s3-and-cloudfront/
Playback in HTML5 can be done using HTML5-based JavaScript players such as http://www.dash-player.com. There is also a tutorial how to generate MPEG-DASH content using x264 + MP4Box: http://www.dash-player.com/blog/2014/11/mpeg-dash-content-generation-using-mp4box-and-x264/
精彩评论