software: video fixing is possible, but not easy

In your phone’s Google search bar, search for certain animal names, then tap View in 3D, then tap View in your Space. You can even take a video as you move around (and it occasionally lashes its tail). VR is so passé, AR (Augmented Reality) is kewl.

Then spend 10 minutes trying unsuccessfully to turn off the permissions you had to give Google Search to access your camera and microphone (“Let’s film you while searching and analyze your facial expression to see how frustrated you are with Google”, what’s the harm?), and remember to curse Google for discontinuing gems like Chromecast Audio and Google Play Music while screwing around with stuff like this.

Then try to clean up and share the video, and the real fun starts…

Video processing by random walk (ultra-nerd alert!)

My camera was confused filming downward, so the original video had the wrong orientation. You can realign all the pixels to the correct orientation, but it’s even simpler: just change the video’s metadata to indicate that it should be displayed rotated. Linux media processing tools such as VLC and ffmpeg have accrued literally hundreds of options to modify video and audio streams, and I found an incantation to change the metadata:

% ffmpeg -i original_video.mp4 \
        -c copy -metadata:s:v:0 rotate=90 \
    alligator_and_dogs.mp4

Next problem: Android told my phone’s camera to take a 1920×1080 video. Most phones do not have a sensor with exactly this 16:9 ration, so normally when told to capture at a particular size they sample a part of what their sensor captures. Somehow my phone + Google’s software did this wrong, and the video wound up with black bars on the top and bottom. Ffmpeg has a video filter, cropdetect, that detects black bars and outputs a cropping rectangle, but the transition from video to black left a single line of glitched pixels at the bottom of each video frame. I could have probably fiddled with cropdetect‘s parameters to get the right output; instead I took a snapshot in VLC (press [Shift+S]), zoomed into it in a paint program, and found the top bar is 22 pixels tall and the bottom 39 pixels.

Ffmpeg has a crop filter that lets you specify how to crop the input video. But figuring out the format for it was hard. All the guides I read gave a series of ever more outlandish cropping recipes, e.g. crop=in_w/2:in_h/2:in_w/2:in_h/2 ; none of them explained that this specifies an output width and output height then a starting position in the original frame. Once I knew that I worked out that I needed to crop to the input video’s width (in_w), 61 pixels less than the input height, starting 0 pixels over, and 22 pixels down: crop=in_w:in_h-61:0:22. Clear as mud!

Facebook wouldn’t let me upload this MP4 video, because it was too brief. No problem, convert it into a GIF. I also wanted to reduce the file size. VLC’s Tools > Media information > Code said the original MP4 video’s frame rate was 48.408636, so reduce the frame rate to 1/3 of this, 16 fps. Also halve the video resolution with ffmpeg’s scale video filter to (1080 – 22 – 39)/2 = 510 tall (and -1 wide as a magic value to preserve the aspect ratio).

Put it all together and the command to make a cleaned-up small animated GIF out of the video is:

% ffmpeg -i alligator_and_dogs.mp4 \
        -r 16 \
        -vf "crop=in_w:in_h-61:0:22,scale=510:-1" \
     alligator_and_dogs.gif

I didn’t actually check if this made the right adjustments but it looked OK, so ship it. I should fiddle around with ffmpeg’s palettegen options to improve the GIF quality, but this took so much time the alligator ate my dog! 🐊🍴🐕

This entry was posted in software. Bookmark the permalink.

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.