Tuesday, May 18, 2010

Extract Ogg (ogv) from Matroska (mkv)

The Story

I don't do much with video. I finally wrote a script which compresses all the files in a given folder using the handbrake cli...just for archiving my home videos that had been digitized; on import they were some slight compression avi. The cli lets me automate converting my videos to a patent free (afaik) format -- Ogg. I use both Theora and Vorbis in a Matroska container...I was going Ogg (Ogv) container, but handbrake didn't support that, and I hear Matroska is better...or far more capable. Even though Ogv would have been fine for my purposes. Oh well.

Anyway, I decide that I want to make a webpage on my development server (read: home computer) that allows my home videos to be seen by my family without the hassle of uploading them to youtube...and carefully optimizing them first or watchmaking Youtube butcher the already mediocre quality. I decided to embrace the future and use the upcoming html5's 'video' tag.

Ah, man...I JUST encoded/archived all my videos to Matroska...even though they were encoded with Theora/Vorbis, the Matroska container doesn't work. I need to extract (I don't want to transcode them and lose all that quality. are you kidding?) the streams from the Matroska format and put them in an Ogg (ogv) container...somehow. Can't be hard, right?

I tried loads of things. MKVToolnix, AVIMux GUI, VirtualDubMod...and I read loads of forums, mostly about people wanting to convert MKV (h.264) to AVI (xvid). I finally read something about ffmpeg...so I thought I could at least (since I encode to the original size AND to a half-size) transcode from the large .mkv to .ogv with that.

20 minutes into it, my Ogg (.ogv) files were looking horrible and coming out at half the size of the (same resolution) input, I did a few searches for optimal settings .ogv or something and came across a post that mentioned something I hadn't seen in the documentation (although I'd imagine it's there)...the ability to 'copy' the audio and video stream. Someone was just using ffmpeg to extract a 1 minute segment from a video file without re-encoding. That's what I want to do!!!

Long story short, this code did it for me:

The Code

ffmpeg -i mysourcevideo.mkv -vcodec copy -acodec copy myoutvideo.ogv

A straight 'Direct Stream Copy' (i used to use VirtualDub -- pardon me), essentially...with no loss in quality and a container change. Now I can make a quick script...I'm not sure if I'll do it for all my videos or just the half-sizes...as I don't really wanna stream the large ones, which I make available for download.

Be End.




Now I'm just wondering if Handbrake CLI has any advantage (for my use, anyway) over ffmpeg?