«

»

Print this Post

Using ffmpeg to convert videos to HTML5 formats

After installing and compiling ffmpeg on my CentOS 6+ dedicated server, I set off to implement a video conversion  utility using ffmpeg. Once I determined what file types I needed to output it was a fairly straight forward process of compiling ffmpeg with the proper codec libraries, then write a custom script specific to our application.

The 3 formats we will output are .mp4, .webm, and .ogg at our original resolution of 640x480SD and a bit rate of 768kbps. Our input is .mp4 3000kbps de interlaced video ripped from DVD, for more information on converting DVDs see this article.

Here are the 3 ffmpeg command lines in php that are used to create the output files.

  1. $mp4cmd = “ffmpeg -y -i $raw_video_path -ac 2 -ab 96k -ar 44100 -vcodec libx264 -b:v 768k $new_mp4”;
  2. $ogvcmd = “ffmpeg -y -i $raw_video_path -acodec libvorbis -ac 2 -ab 96k -ar 44100 -vcodec libtheora -b:v 768k $new_ogg”;
  3. $webmcmd = “ffmpeg -y -i $raw_video_path -acodec libvorbis -ac 2 -ab 96k -ar 44100 -f webm -vcodec libvpx -b:v 768k $new_webm”;

In the first conversion we are creating an mp4 that is suitable for streaming, all of our videos are instructional there is no music so we keep the audio bit rate at 96kbps, while the sample rate is at 44khz, mp4 video is converted using the h264 codec from the libx264 library and we set the video bit rate at 768k.

In the second and third lines we convert using the same bit rate but different codecs for each of the output files. The .ogg file uses the theora codec and the webm uses the vpx codec. The audio for each is encoded using the libvorbis codec.

Here is a sample of what ffmpeg returns when converting an mp4 to webm:

 

/usr/local/bin/ffmpeg -y -i /movie.mp4 -acodec libvorbis -ac 2 -ab 96k -ar 44100 -b:v 768k /movie.webm
ffmpeg version 1.2 Copyright (c) 2000-2013 the FFmpeg developers
built on Jul 18 2013 21:12:08 with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-3)
configuration: –enable-version3 –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libvpx –enable-libfaac –enable-libmp3lame –enable-libtheora –enable-libvorbis –enable-libx264 –enable-libvo-aacenc –enable-libxvid –disable-ffplay –enable-shared –enable-gpl –enable-postproc –enable-nonfree –enable-avfilter –enable-pthreads –extra-cflags=-fPIC
libavutil      52. 18.100 / 52. 18.100
libavcodec     54. 92.100 / 54. 92.100
libavformat    54. 63.104 / 54. 63.104
libavdevice    54.  3.103 / 54.  3.103
libavfilter     3. 42.103 /  3. 42.103
libswscale      2.  2.100 /  2.  2.100
libswresample   0. 17.102 /  0. 17.102
libpostproc    52.  2.100 / 52.  2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from ‘movie.mp4’:
Metadata:
major_brand     : isom
minor_version   : 512
compatible_brands: mp41
creation_time   : 2013-07-05 00:56:34
Duration: 00:00:58.09, start: 0.000000, bitrate: 3750 kb/s
Stream #0:0(eng): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 1024×600 [SAR 1:1 DAR 128:75], 3616 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 58 tbc
Metadata:
creation_time   : 2013-07-05 00:56:34
handler_name    : VideoHandler
Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s
Metadata:
creation_time   : 2013-07-05 00:56:34
handler_name    : SoundHandler
[libvpx @ 0x16a8b80] v1.2.0-3618-g73ff56b
Output #0, webm, to ‘/movie.webm’:

major_brand     : isom
minor_version   : 512
compatible_brands: mp41
encoder         : Lavf54.63.104
Stream #0:0(eng): Video: vp8, yuv420p, 1024×600 [SAR 1:1 DAR 128:75], q=-1–1, 345 kb/s, 1k tbn, 29.97 tbc
Metadata:
creation_time   : 2013-07-05 00:56:34
handler_name    : VideoHandler
Stream #0:1(eng): Audio: vorbis, 44100 Hz, stereo, fltp, 96 kb/s
Metadata:
creation_time   : 2013-07-05 00:56:34
handler_name    : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (h264 -> libvpx)
Stream #0:1 -> #0:1 (aac -> libvorbis)

 

 

 

 

Permanent link to this article: http://www.palmbeachdigitaldesign.com/dev/using-ffmpeg-to-convert-videos-to-html5-formats/