Fast and reliable way to encode Theora Ogg videos using ffmpeg, libtheora, and liboggz

by bram

if this doesn’t work for you:
ffmpeg2theora in.mov -o out.ogv
or
ffmpeg2theora in.mov -v 8 -o out.ogv
(To solve the video artefacts)

Try the following:

from tracey jaquith at archive.org

use ffmpeg to turn any video to “rawvideo”.
pipe its output to *another* ffmpeg to turn the video to “yuv4mpegpipe”.
pipe its output to the libtheora tool.
for videos with audio, ffmpeg create a vorbis audio .ogg file.
add tasty metadata (with liboggz utils).
combine the video and audio ogg files to an .ogv output!

Detailed example:

Video:
ffmpeg -an -deinterlace -s 400×300 -r 20.00 -i videofile.avi -vcodec rawvideo -pix_fmt yuv420p -f rawvideo – |  ffmpeg -an -f rawvideo -s 400×300 -r 20.00 -i – -f yuv4mpegpipe – |  libtheora-1.0/lt-encoder_example –video-rate-target 512k – -o tmp_video.ogv

Why the double pipe above? Some videos could not go directly to yuv4mpegpipe format such that libtheora (or ffmpeg2theora) would work all the time.

We convert to yuv420p in the rawvideo step because ffmpeg2theora has (i think) some known issues of not handling all yuv422 video inputs (i found at least a few videos that did this).

Audio:
ffmpeg -y -i videofile.avi -vn -acodec libvorbis -ac 2 -ab 128k -ar 44100 tmp_audio.ogg

We do the vorbis audio outside of libtheora (or ffmpeg2theora) to avoid any issues with Audio/Video sync.
Annotate:
oggz-comment tmp_audio.ogg -o tmp_audio_annotated.ogg TITLE=”a title” ARTIST=”an artist” LICENSE=”http://creativecommons.org/licenses/publicdomain/” DATE=”2010″ ORGANIZATION=”an organisation name” LOCATION=http://someurl

We add the metadata to the audio vorbis ogg because adding it to the video ogv file wound up making the first video frame not a keyframe (!)
Merge:
oggzmerge tmp_video.ogv tmp_audio_annotated.ogg -o out_videofile.ogv

rm tmp_video.ogv tmp_audio.ogg tmp_audio_annotated.ogg
This will end up working in Firefox 3.1 and greater — the new HTML “video” tag:

<video controls=”true” autoplay=”true” src=”http://somevideo.ogv”> </video>