Tuesday, April 3, 2012

Adding Ogg Video to VideoWriter

Did the following such that OpenCV could write Theora encoded Ogg Video (.ogv) in Win32 environment. Useful as it is a way to embed HTML5 video

Details of FFMPEG build and OpenCV integration is the same as previous post. Not repeating here.
  1. Setup MinGW-32 environment.
  2. Download Ogg, Vorbis and Theora from Xiph.org.
  3. Configure and Build Ogg, Vorbis and Theora from source - in this order.
  4. Configure and Build FFMPEG-0.8.2 with --enable-libvorbis and --enable-libtheora enabled.
  5. Copy the ogg, vorbis and theora archive (.a) to OpenCV, in addition to the updated ffmpeg core libraries. Rebuild opencv_ffmpeg with the new OpenCV library.
  6. Specify codec with fourcc ('t', 'h', 'e', 'o') and output file extension .ogv in call to VideoWriter::open().


  • Fourcc for theora used in OpenCV 2.3 is 'theo' not 'ther'
  • If theora build ended with error in compiling examples, re-configure with --disable-examples flag. This avoids the error and allows the 'make install' to complete without error, thus copying pkg-config files.
  • Use --extra-cflags and --extra-ldflags to specify compile options necessary for ffmpeg to build against the vorbis and theora libraries. The flags could be looked up with pkg-config. (Use PKG_CONFIG_PATH if installed in /usr/local or other non-standard locations).
  • FFMPEG 0.8.2 build failed with error in ffplay if --disable-avfilter is specified. Patched with '0001-fix-compilation-error-when-disable-avfilters.patch' (see References)
  • The order of ogg, vorbis and theora library specified in opencv_ffmpeg build is important. Will failed with missing symbol link-time error if the common libraries are not specified last (like ogg). It is static build. This is what I used: "-lvorbisenc -lvorbisfile -ltheoraenc -ltheoradec -ltheora -lvorbis -logg"
  • Encountered ffmpeg error "Application provided invalid, non monotonically increasing dts to muxer in stream 0". This only happen when I set the output frame rate to certain values such as 5-fps, 15-fps. (Other values might fail, but didn't check). Cross-check with the ffmpeg executable built from the same source. There is no error and the following transcoding succeeded.
    $ ./ffmpeg.exe -i -an -vcodec libtheora -f ogg -b 500k -r 15 15fps.ogv
    Work-around: Noticed that there is a condition check on AVCodecContext->coded_frame->pts before calling av_rescale_q(). (ffmpeg.c line 1312.) Put the same condition to cap_ffmpeg_impl.h. Problem goes away.

Embed HTML Video

  • Apache Configuration: 'AddType' directive to make sure the .ogv is sent to the browser with the intended mime-type.
  • Uses <video> tag on HTML to embed inline video.