A script for rotating digital camera videos!

A while ago, after flickr launched the ability to upload “long photos”, I shot a clip of waves crashing on the beach, and wanted to share it. But there was a catch: being taken on a compact camera, it felt perfectly natural to turn it upright, and shoot the video in a “portrait” orientation. So, like a lot of my photos, it needed turning 90° clockwise – but unlike a photo, there was no obvious way to do this.

I hunted around at the time, and found that while plenty of players can transform video during playback (my copy of VLC seems to have got stuck, and now rotates everything!), few video editing tools could save a rotated video. I finally managed it with a power-utility which required me to import the video, tweak a large number of output options I didn’t understand, and hope the result wasn’t too mangled. Hardly ideal.

But while reading up on it, I discovered that most digital cameras – including my Casio Ezilim EX-Z1050 – shoot videos in a format called “Motion JPEG” or “MJPEG”, which is basically a bunch of JPEG pictures stuck together. So if I could extract the images from the video, they should be easy to rotate, and all I’d need to do was stick them together again…

Well, yesterday I was playing with lossless image rotation using jpegtran, and then managed to find MJPEG tools – a bunch of commandline tools for manipulating MJPEG data, actually designed for video capture utilities. And guess what? It works! I can split the AVI file from my camera into a bunch of JPEGs, rotate them, and stitch them back together, with minimal loss of quality!

Oddly, the biggest sticking point was sound – the mjpegtools utilities can’t see the sound stream in my source files. In fact, the only utility I could was FFmpeg, which at over 10 times the size of all the other tools put together is like the proverbial sledgehammer to the sound streams nut.

So, with my meagre Windows batch-scripting skills, I’ve put together something which might, just about, be useful, and called it VidRotate. Go forth, download! But beware – I make absolutely no guarantee that it won’t blow your computer up at this stage…

2 Comments

  1. Hroth McMcHammer

    Hi Ron,

    You do realize that Squeak has full support for hacking M-JPEGS?

    So you wouldn’t even have to write anything if you were being lazy. It also shows you how to interface between the JPEGs underneath and how to get at them, it shows that there is a header that describes the motion-ness of the set of JPEGs to come (= a pseudo folder).

    See:

    “A JPEG movie file consists of a header followed by a sequence of JPEG-compressed images followed by one or more sound tracks. It responds to a subset of the protocol for MPEGFile so that a JPEG movie can be played by MPEGDisplayMorph as if it were an MPEG movie.

    cf:

    Browser::

    Movies-Kernel :: JPEGMovieFile

  2. Rowan

    Well, if by “not writing anything” you mean “writing something in Squeak” – unless the JPEGMovieFile class already has a “rotate video by 90 degrees” method, which seems unlikely.

    Could be an interesting avenue to explore though, I’ll have to see if it can read my files, particularly that tricky audio track…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.