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().

Troubleshooting

  • 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.

References




10 comments:

  1. Amazing.
    Is there any way that something like this would work for python?

    I really want to be able to save ogv files from python.

    ReplyDelete
    Replies
    1. I have not tried python OpenCV. I would like to think that that python API is wrapper that uses the same OpenCV ffmpeg library as the C++ interface. If that's the case, then my suggestion above should work.

      Delete
  2. Hello,
    I want to use Theora for my project but was unable to complete your procedure. Can you provide the ffmpeg dll that you built with theora support?
    Thanks

    ReplyDelete
  3. Hi,

    I have uploaded the DLLs I used for OpenCV 2.2 and 2.4.1. You could get them here: http://www.ge.tt/#!/6BeYnja

    The link will expire in a week or two. And that's all I could help. Good luck!

    Frankie

    ReplyDelete
  4. I have a question for you. If VideoWriter is set, can I just output Mat without converting it into YUV for ogv? Thanks.

    JP

    ReplyDelete
    Replies
    1. I did not do anything special about the colorspace regarding to ogv.

      Delete
  5. Thank you.

    but I was unable to complete your procedure. me too,

    Can you provide the ffmpeg dll.. plz..

    jeonikbeom@daum.net

    ReplyDelete