Archive for May, 2010

How to layer/remix videos with free command line tools

May 18, 2010

This tutorial assumes you know a little bit about script files. All files have some editing that is required before executing and knowing a little bit about a couple parameters really expands what you can do with them. That said, it’s really not too hard and we’re happy to answer basic questions.

Video Overview: (which you can definitely skip)

INSTALL SOFTWARE:
FFmpeg (OS X howto), video utility, format conversions, etc.
Imagemagick:all kinds of image processing goodness. Like a command line version of GIMP or photoshop.
Sox: Audio processing.

We’ve used all of these programs on both Linux and Mac. Linux installs are pretty easy with package managers (apt-get, yum, etc). Mac is little more work for ffmpeg and imagemagick but not too tough.

[Optional] VideoDownloadHelper: Firefox plugin that allows you to download easily from lots of video sharing sites in mp4 and flv format at a variety of resolutions. There are Linux command line downloaders as well but we haven’t tried that yet…

GET SOME MOVIES Maybe you take them with your own camera, maybe you download from online. You’ve probably got a bunch floating around your hard drive already. Start with short ones. Feel free to grab some from http://youtube.com/brooklynmobile, or any other broadcaster, all are public domain.

EXPORT MOVIES TO FRAMES
mkdir frames
ffmpeg -i yourvideoA.avi frames/A%4d.jpg
ffmpeg -i yourvideoB.avi frames/B%4d.jpg

This will export individual frames of the video to A0001.jpg, A0002.jpg, etc
For longer videos you might need to use A%5d.jpg or A%6d.jpg but start with small videos first.

DO SOMETHING TO THE FRAMES and RECOMPILE MOVIE
This is where ImageMagick comes in handy. ImageMagick can do all kinds of image manipulations. We will give you a few examples.

EXAMPLES:
Blending two movies on top of one another. Copy the following code into a file called blurmachine.sh and make it executable[type chmod 755 blurmachine.sh at the command line].

#!/bin/bash
# boranj!
counter=10000 # initialize counter
for f in A*.jpg; do # step through frames
let "counter+=1" # increment counter
# apply some Imagemagick effects to the 2 images and write a new image
composite -blend 50 A${counter:1}.jpg B${counter:1}.jpg -matte ./mixedFrame${counter:1}.jpg
done
#Use FFMPEG to make frames into a movie again
ffmpeg -r 30 -sameq -i mixedFrame%4d.jpg ../outputmovie$DATETIME.mp4

Now you will have a movie with no sound that will look something like this:

ADD AUDIO BACK TO THE MOVIES
After using the above script file, you will have a movie with no sound. You can add the soundtrack of your choice, or add the mixed audio from the original movies.
To extract audio from the original movies and sum it back together:

# AUDIO EXTRACTION
ffmpeg -i movA.mp4 movAsound.wav
ffmpeg -i movB.mp4 movBsound.wav
# AUDIO MIX
sox -m movAsound.wav movBsound.wav mixed.mp3

Add audio back to your movie:
ffmpeg -i movNoSound.mp4 -sameq -i mixed.mp3 -ab 192k movSound.mp4

OTHER IMAGE MANIPULATIONS

Wavy overlay: Script file.

and similar example with more slices.
For this example, you need to create a mix image first. That is done with this line.
convert -size 640x480 gradient: -evaluate sin 20 wave_gradient.png

Place it in an appropriate location and make sure it’s correctly reference in your script file.
This type of combination has all kinds of options. You can take this a lot of other places if you wish to dig deeper. See this forum post and this usage examples page.

Four movie grid montage: Script File.

Six panel version of same effect.

Lots of other possibilities if you poke around Fred’s Imagemagick scripts as well.

Let us know what you come up with.