Command line video processing

I’m gonna jump right into the technical stuff and see if anybody pays attention to these kinds of posts. As my buddy Jason once said:

“Yeah that command line <bleep> is real <bleeping> interesting”.

He was right, but wasn’t real interested in the really nerdy part of linux like I was (and am).

While compiled languages are almost ALWAYS going to be inherently faster at high cpu and memory usage tasks, we love using our old friend PHP as a wrapper for video processing. There is an incredible number of things you can do with tools like ffmpeg. For example, I had about 1,500 .MP4 video files I needed to transcode from 1080p down to 540p for web delivery. The application I had built needed a specific format, mostly due to real time tagging and cues, etc. Anyway…Since the metadata was already in the database, I was able to do a simple query:

SELECT * from footage where vwidth >= '1280' order by updated

Inside of my while loop I had a call that looked like this:

$cmd = "/usr/bin/ffmpeg -i '/var/www/html/clips/$mypath' -c:v libx264 -crf 28 -preset medium -c:a aac -strict -2 -vbr 4 -movflags +faststart -vf scale=720:-2,format=yuv420p '/var/www/html/clips/$mypath.mp4' &";

Then we shell out the command with:

exec($cmd);

Not to get too far in the weeds, but this does need a bit of explanation…
the $cmd variable is really just being used as a string. Contained in the string is (in order) the path to ffmpeg, the input file, output video codec, quality setting, output audio codec, bitrate, scaling, container format and output file name. One could (BELIEVE ME) go nuts trying to understand all of the command-line params for ffmpeg, let alone which ones have precedence and why order is important.

Anyway, after about a day of processing about 72 hours worth of video, I had my transcoded video all ready to go. Naturally there are always better / different / etc ways to do stuff like this but it’s simple and incredibly effective.

Next up was creating a thumbnail that would turn into a .webm on mouseover. Easy enough with the simple, but fantastic ffmpegthumbnailer. Just tell it what file, frame and the output file name and you’re good. (Naturally you can go nuts with your command line options as well) . I combined it with the GD library to stamp stuff like date and time on the resulting image.

For the .webm “preview”, I used a little algorithm I wrote. It calculates the length of the video, divides it up into 10 parts and creates a 1 second video for each of those 10 start times (minus 1 second) and then puts it together as a 10 second preview. As you mouse over the thumbnail, it swaps the image for the webm so you can see the preview action before clicking to view the whole file. Youtube sorta does this, but if you’re spotting sports videos it helps a ton by being able to see the different uniforms, etc. Plus, it’s cool 😎

As you mouse over those stills, the 10 second preview plays.

About the Author: