Converting HTML5 Video in Bulk
Posted on : 27-12-2011 | By : Arash Hemmat | In : Tutorials for myself
0
Recently i’ve been working on a multimedia magazine project and i wanted to use HTML5 for playing video, i’ve decided to use VideoJs while there were also good tools like MediaElementJs and SublimeVideo as you can see in below chart (borrowed from MediaElementJs website!) i needed to have both Webm and H.264 format in order to cover most of the browsers.
The first problem was the lack of good converters for Webm format, i could find few but most of them were lacking either advanced options or multiple file conversion, so i started using ffmpeg for Webm conversion and it was working pretty good until i realised that ffmpeg has dropped support for H.264 because of the some patent problems, i could build ffmpeg with H.264 support but Handbrake was out there with many more features and i decided to use ffmpeg for Webm conversion and Handbrake for H.264 conversion.
I needed to create both Linux and Windows converters for my own use and the other stuff who were using windows.
Windows Converter
First i downloaded the Windows builds of Handbrake and ffmpeg:
Handbrake: http://handbrake.fr/downloads.php
ffmpeg: http://ffmpeg.zeranoe.com/builds/
Then i created a batch file which was reading all files in “input” folder convert them and put in “ouput” folder, all the user needs to do is to put video files in “input” folder and click on “convert.bat” file. Here is the “convert.bat”:
@echo off
SET count=1
FOR /f “tokens=*” %%U IN (‘dir /b input’) DO (
echo %%~nU
ffmpeg.exe -i input\%%U -vcodec libvpx -s 640×360 -aspect 16:9 -b 256k -qmin 4 -qmax 63 -deinterlace -acodec libvorbis -ab 96k -aq 4 -ar 44100 -threads 8 output\%%~nU_normal.webmffmpeg -i input\%%U -vcodec libvpx -s 854×480 -aspect 16:9 -b 640k -qmin 4 -qmax 63 -deinterlace -acodec libvorbis -ab 128k -aq 4 -ar 44100 -threads 8 output\%%~nU_high.webm
handbrake -i input\%%U -t 1 -c 1 -o output\%%~nU_normal.mp4 -O -f mp4 -e x264 -w 640 -l 360 –custom-anamorphic -b 256 -a 1,1 -E faac,ac3 -6 stereo,auto -R 44.1,Auto -B 96,auto -D 0.0,0.0 -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subq=6:weightb=0 -v 1
handbrake.exe -i input\%%U -t 1 -c 1 -o output\%%~nU_high.mp4 -O -f mp4 -e x264 -w 854 -l 480 –custom-anamorphic -b 640 -a 1,1 -E faac,ac3 -6 stereo,auto -R 44.1,Auto -B 128,auto -D 0.0,0.0 -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subq=6:weightb=0 -v 1
set /a count+=1 )
pause
This bat file covnerts video files in “input” folder into two different quality and size which i needed for my project and you can customise them as you wish.
Linux Converter
The linux approach is almost the same but to make it work you need to install ffmpeg and Handbrake on your linux box first. Here is the convert.sh batch file:
for FILE in `ls input/`
do
NAME=${FILE%\.*}
ffmpeg -i input/$FILE -vcodec libvpx -s 640×360 -aspect 16:9 -b 256k -qmin 4 -qmax 63 -deinterlace -acodec libvorbis -ab 96k -aq 4 -ar 44100 -threads 8 output/”$NAME”_normal.webm
ffmpeg -i input/$FILE -vcodec libvpx -s 854×480 -aspect 16:9 -b 640k -qmin 4 -qmax 63 -deinterlace -acodec libvorbis -ab 128k -aq 4 -ar 44100 -threads 8 output/”$NAME”_high.webm
HandBrakeCLI -i input/$FILE -t 1 -c 1 -o output/”$NAME”_normal.mp4 -O -f mp4 -e x264 -w 640 -l 360 –custom-anamorphic -b 256 -a 1,1 -E faac,ac3 -6 stereo,auto -R 44.1,Auto -B 96,auto -D 0.0,0.0 -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subq=6:weightb=0 -v 1
HandBrakeCLI -i input/$FILE -t 1 -c 1 -o output/”$NAME”_high.mp4 -O -f mp4 -e x264 -w 854 -l 480 –custom-anamorphic -b 640 -a 1,1 -E faac,ac3 -6 stereo,auto -R 44.1,Auto -B 128,auto -D 0.0,0.0 -x cabac=0:ref=2:me=umh:bframes=0:8x8dct=0:trellis=0:subq=6:weightb=0 -v 1
done
Customising Video Quality and size
I needed two different quality and size for this project, the normal quality and the high quality and the options used in converter is based on these parameters:
Normal Quality
Video bitrate: 256k
Video Size: 640×360
Video Aspect: 16:9
Audio bitrate: 96k
High Quality
Video bitrate: 640k
Video Size: 854×480
Video Aspect: 16:9
Audio bitrate: 128k
The most important thing here is to make a video with a bitrate that suits your user’s bandwidth, the normal quality needs at least 352 kbps (256k video +96k audio) bandwidth for playing without buffering and the high quality needs at least 768kbps bandwidth for a smooth play, knowing these you should be able to customise the batch files based on your own needs.
Note 1: this approach won’t guarantee the bitrate and if you need a guaranteed bitrate you must use a two pass conversion method which is much more complicated.
Note 2: if you wonder what each of options i used here means you can take a look at ffmpeg and Handbrake cli guides:
Handbrake CLI Guide: https://trac.handbrake.fr/wiki/CLIGuide
ffmpeg documentation: http://ffmpeg.org/ffmpeg.html
Download the Converter Here

