Tuesday, August 13, 2013

Converting video for web with FFmpeg

The plight of the HTML5 video format is bad and it looks like that will soon not change.


Formats overview:
  • WebM ( video codec: VP8, audio codec: Vorbis )
  • OGG ( video codec: Theora, audio codec: Vorbis )
  • MP4 ( video codec: H.264, audio codec: ACC/ MP3 )
  • Flash video ( video codec: H.264, audio codec: ACC/ MP3 )




MediaInfo
MediaInfo is a tool for reading video files metadata. Using it is very simple:


FFmpeg
FFmpeg is tool for converting audio and video files. It uses libavcodec library. Converting video files with FFmpeg to required formats ist simple:


WebM
ffmpeg -i INPUT_FILE -acodec libvorbis -vcodec libvpx -b 400k OUTPUT_FILE.webm


OGG
ffmpeg -i INPUT_FILE -acodec libvorbis -vcodec libtheora -b 400k OUTPUT_FILE.ogg


MP4
ffmpeg -i INPUT_FILE -acodec libfaac -vcodec libx264 -b 400k OUTPUT_FILE.mp4


Parameters:
  • -acodec: audio codec, or -c:a in new release of FFmpeg
  • -vcodec: video codec, or -c:v in new release of FFmpeg


There are a lots of others interesting parameters.


libfaac
For the libfaac codec you need the FFmpeg compiled with --enable-libfaac. For more details read FFmpeg Wiki. Or you can use libvo_aacenc codec:
-acodec libvo_aacenc


Bash script for converting video
For painless conversions you can use a prepared bash script.


Use:
  • ./web_video_converter.sh MY_VIDEO_FILE.avi
  • ./web_video_converter.sh FOLDER_WITH_VIDEOS