DeShaking Video

Shaky camcorder video can look nauseating on big screens. I knew there was software to remove the shake but I thought it involved cropping the edges of the image off to provide a “shake buffer”. Then I found DeShaker, a VirtualDub plugin. It can deshake video without losing the edges, by using previous and future frames to provide the missing edges.

DeShaker uses a two pass approach. In the first pass it estimates motion vectors for each frame in the clip. The second pass then uses the vectors to translate/rotate the video, correcting the motion. There are lots of parameters for adjusting how smooth it makes the video.

A good tutorial on setting it up is here. Its now at version 2.2 and the release notes describe additional capabilities.

There are times when you want to bypass the deshaker filter, eg: when camera generated titles are present or complex motion confuses it. In these instances I use Premiere to cut between the deshaked and original video. I deshake an entire clip and put it into Premiere’s Video 1A track. I dont bother processing audio for this clip. I then line it up with the original, in Video 1B. This still has the audio locked with it. Then all I have to do is cut out the sections of the deshaked video (in 1A) that I dont want and Premiere will cut to the corresponding video in 1B. The sound continues uninterrupted.

After initial tests I discovered that a lot of black garbage was being “dragged” into the image as it was de-shaked. I found that this was due to the source video not extending to the borders of the 720×576 frame. This meant I needed to crop whatever I fed to DeShaker. Yet, I needed the project to remain in the standard 720×576 format. This was solved by adding cropping and padding filter stages in VirtualDub.

Here is the VirtualDub filter setup:

Filter 1: DeShaker. (see the tutorial link for a guide on settings).

VirtualDub’s Filter Input Cropping:

LEFT(X1) 5
TOP(Y1) 2
RIGHT(X2) 11
BOTTOM(Y2) 8

Notice its not centred. 8 rows from the bottom removes head switching noise. Horizontally, I found the “active” image area was offset so more is removed from the right. The top line is black so 2 rows are cropped to preserve the interlacing order.

This results in DeShaker gettting a clean 704×566 image to work with, with no nasty black borders to bleed into the visible area when it does its stuff. The next step is to restore the image to 720×576 size. This is done by translating the 704×566 image into a 720×576 frame. Unfortunately VirtualDub doesn’t come stock with a single filter to achieve this, but it can be done as follows:

Filter 2: Resize

New Width 704
New Height 566
Filter Mode Nearest Neighbour
Interlaced [X]
Expand frame and letterbox image [X]
Frame Width XXX (tempting to use 720)
Frame Height YYY (tempting to use 576)

This adds borders around the processed iamge, without resizing the content. Thats why Nearest Neighbour is OK, its a pixel for pixel blit. The problem is that Resize filter’s letterboxing will centre the content in the frame, but originally it wasn’t centred. This would lead to lost vertical content and the image jumping a little when transitioning to/from a deshaked region.The answer is to add enough extra “padding” around the image and then trim off the extra on the sides that dont need it. 11 pixels are needed on the right and 8 pixels at the bottom. The required size is hence:

XXX = 704 + 11 * 2 = 726
YYY = 566 + 8 * 2 = 582

Now a final crop can trim the extra padding from the top and left. No other filtering is necessary for this stage so the “null transform” is used.

Filter 3: Null Transform

VirtualDub’s Filter Input Cropping:

LEFT(X1) 6
TOP(Y1) 6
RIGHT(X2) 0
BOTTOM(Y2) 0

This outputs a 720×576 frame (what we started with) with the image nominally in the same position it was at in the source file. DeShaker will shift it about as it does its job, so some care is needed to ensure a “cut” isn’t made in Premiere at a point where the image was heavily offset by DeShaker.

 Hi8 Video

I found that footage taken with a Hi8 camcorder has 8 black rows at the top. Here are cropping and scaling values for it:

Filter 1: (DeShaker)

VirtualDub’s Filter Input Cropping:

LEFT(X1) 5
TOP(Y1) 8
RIGHT(X2) 11
BOTTOM(Y2) 8

(feeds 704×560 video into DeShaker).

Filter 2: Resize

New Width 704
New Height 560
Filter Mode Nearest Neighbour
Interlaced [X]
Expand frame and letterbox image [X]
Frame Width 726
Frame Height 576

Filter 3: Null Transform

VirtualDub’s Filter Input Cropping:

LEFT(X1) 6
TOP(Y1) 0
RIGHT(X2) 0
BOTTOM(Y2) 0

 DV Video

Here are cropping settings for DV recordings. Video for these extends the full 720 pixels horizontally so no cropping is needed there. There is no switching noise at the bottom but a couple of blank lines at the top. I’ve settled with cropping 4 lines from the top and bottom, making it symmetrical. This means the oversize/crop trick isn’t necessary and the 3rd filter stage is not required.

Filter 1: (DeShaker)

VirtualDub’s Filter Input Cropping:

LEFT(X1) 0
TOP(Y1) 4
RIGHT(X2) 0
BOTTOM(Y2) 4

(feeds 720×568 video into DeShaker).

Filter 2: Resize

New Width 720
New Height 568
Filter Mode Nearest Neighbour
Interlaced [X]
Expand frame and letterbox image [X]
Frame Width 720
Frame Height 576

The results of all this messing about … is smooth video without all the bumps and knocks from the use of a handycam.