headerphoto

Raw RGB/RGBA input for MEncoder

Sometimes it's useful to get a video output directly from your program. If you don't want to mess around with Video Compression APIs directly you'd write a series of images out to disk, and then load these in with something like:

./myprogram -size 640 480 -fps 30 -outputformat png

mencoder mf://*.png -mf w=640:h=480:fps=30:type=png \
-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1000000 -o video.mp4

But to be honest, PNG compression is slow, and it'll just eat your disk space up if you use TGA.

However, you can actually pipe your video data directly into MEncoder! (at least under Linux)

./myprogram -size 640 480 -fps 30 | \
mencoder /dev/stdin -demuxer rawvideo \
-rawvideo w=640:h=480:fps=30:format=rgba \
-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1000000 -o video.mp4

You might also want to use format:rgb8 or bgr8 here, but I found these tended to cause my mencoder to crash so it was easier to use rgba and jush push in 255 for the alpha channel.

Literally all your program then has to do is write uncompressed RGBA data for each frame out to stdout, one after the other, and then exit. And it's *fast*. I'm able to create AND compress full 1080p videos at around 15fps on my computer, as one core gets used for rendering, and another for compression.

If you just want to write to a file (or you have an application which writes to a file), you can do:

./myprogram -size 640 480 -fps 30 -o /dev/fd/3 3>&1 >/dev/null | \
mencoder /dev/stdin -demuxer rawvideo \
-rawvideo w=640:h=480:fps=30:format=rgba \
-ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1000000 -o video.mp4