Stabilizing Cute Baby Goats

Featured

Stabilizing WHAT ?!?

Baby goats. Cute baby goats. Yes, you’re reading it right. Look:

Yeah, I know, this is a naive video that belongs to /r/aww. But any excuse is good to me when its all about playing with video.

These kids (as baby goats are called) decided to come to this world the night I was around. In a hurry I grabbed my Canon 7D and filmed these newborns with a Sigma 30mm f/1.4 EX DC HSM to leverage its large aperture. And as expected, in the night, with no artificial light and no extra stabilization device, the resulting footage are extremely shaky.

I tried to stabilize the shots but was really disappointed by the results. The final version of the video you watched above only feature the original footage, without any stabilization. But for posterity, here are some notes about the tools I played with.

vid.stab

The first tool I tried was vid.stab, a Transcode plugin that is now part of Transcode itself. But the 1.1.5 version that is bundled with the current Ubuntu 11.10 is quite old.

I wanted to compile it from its sources. But the binary distribution available on the project website works out of the box. To save some effort, let’s install the latter:

$ wget http://public.hronopik.de/vid.stab/files/vid.stab-0.93-transcode-1.1-binary-x86_64.tgz
$ tar xvzf ./vid.stab-0.93-transcode-1.1-binary-x86_64.tgz
$ sudo mv ./vid.stab-0.93-transcode-1.1-binary-x84_64/filter_*.so /usr/lib/transcode/
$ rm -rf ./vid.stab-0.93-transcode-1.1-binary-x8*

Now, as explained in the documentation, you have to let transcode analyze the video:

$ transcode -J stabilize -i ./MVI_1714.MOV -y null,null -o dummy

Only after this first pass you can apply the stabilizing transformations:

$ transcode -J transform -i ./MVI_1714.MOV -y ffmpeg -F huffyuv -o ./MVI_1714-stabilized.MOV

If your not satisfied with the result, you can increase the area of tracking points with the shakiness parameter:

$ transcode -J stabilize=shakiness=8:show=1,preview -i ./MVI_1714.MOV -y null,null -o dummy

In the command line above we added the show=1,preview parameters, which have the nice effect of displaying a preview of the work done behind the scene:

And if you want to see the transformations applied in the final video, just deactivate the cropping and zooming mechanism:

$ transcode -J transform=crop=1:optzoom=0 -i ./MVI_1714.MOV -y ffmpeg -F huffyuv -o ./MVI_1714-stabilized.MOV

Finally, here are some command helpers to automate the stabilization process for a massive amount of video:

$ find ./ -name "*.MOV" -exec transcode -J stabilize -i "{}" -y null,null -o dummy \;
$ find ./ -name "*.MOV" -exec transcode -J transform -i "{}" -y ffmpeg -F huffyuv -o "{}.stabilized.avi" \;

Alternative tools

I told you I was disappointed by the results. For example, in the first shot of the video above, vid.stab will stabilize based on the movements of the head of the goat, not based on the background. All of this because tracking points are generated on hight-contrast area. Unfortunately in this first scene, the only high contrast area is the kid’s head.

Even in shots where the contrast is in our favor, software stabilization don’t always produce nice output. If by chance the tracking points are set on the right objects (those that should be considered motionless), the results may not be pleasing, as it may expose inappropriate skewed perspective, shifting motion-blur and spacial deformation.

While a little bit smarter, YouTube’s embedded stabilization effect still suffer from these same short-comings. If it tries to smooth out consecutive transformations better than vid.stab, it still fails to produce nice output devoid of unattractive artifacts.

Another tool worth trying is VirtualDub, which you can run under Wine and leverage its deshaker plugin. But I didn’t tested it.

Last but not least, Blender can be used to stabilize videos. Again I haven’t tried this yet, but I think it’s the best investment of you time, since the new tracking features make Blender a powerful tool for 3D integration.

Conclusion (tl;dr)

There is no silver bullet: don’t expect software stabilizer to save your shaky shots in post-production. If you want steady shots, plan them beforehand and use proper gear on site, be it a monopod, a tripod, a slider, a dolly, a crane or a steadicam. That’s the only way to eliminate the pain and deception when you hit the editing room.

Video commands

  • Here are some commands to get informations about the nature of a video:
    mplayer -frames 0 -identify ./video.avi
    tcprobe -i ./video.avi
    ffmpeg -i ./video.avi
    file ./video.avi
    

Mplayer / Mencoder

  • Change the aspect ratio of a film for the playback. Standard aspect ratio are : 1.33 (4:3), 1.66 (1.66:1), 1.77 (16:9) and 2.35 (2.35:1):
    mplayer -aspect 2:1 ./video.avi
    
  • Play the video with subtitles:
    mplayer -sub ./subtitle_file.txt ./video.avi
    
  • This will extract audio track no. 128, downmix the AC3 sound to PCM and write the results to file.wav:
    mplayer -vo null -hardframedrop -aid 128 -ao pcm -aofile file.wav dvd://1
    
  • This will extract the audio, convert it to PCM and write the resulting wave file to audio.wav:
    mplayer -vo null -hardframedrop -ao pcm:file=audio.wav myvideo.avi
    
  • Show all subtitles streams:
    mplayer -vo null -ao null -frames 0 -v 2 dvd://1 >&1 | grep sid
    
  • Create a rotated copy of the file.avi video (rotate=1 : clockwise ; rotate=2 : anti-clockwise):
    mencoder -vop rotate=2 -oac pcm -ovc lavc ./source.avi -o ./dest.avi
    
  • Preview a video composed of all jpeg files from the current folder at 15fps (mplayer only support jpeg, png, tga and sgi formats):
    mplayer "mf://*.jpg" -mf fps=15
    
  • Create a 15fps video from all jpeg files of the current folder:
    mencoder "mf://*.jpg" -mf fps=15 -ovc lavc -o ./dest.avi
    
  • Encode a SVCD to AVI file:
    mencoder -oac lavc -ovc lavc vcd://1 -o ./svcd.avi
    
  • Transcode video to raw format (be carefull: usually the output video got annoying audio delay):
    mencoder -oac pcm -ovc raw -ofps 25 -noskip ./video.wmv -o ./video.avi
    
  • Encode a video using the default mpeg4 codec at 400 kbps for video and mp3 codec at constant 32 kbps bitrate for audio:
    mencoder -oac mp3lame -lameopts cbr:preset=32 -ovc lavc -lavcopts vbitrate=400 in.avi -o out.avi
    
  • Enhance the sharpness of the video:
    mplayer video.avi -vf smartblur=.6:-.5:0,unsharp=l5x5:.8:c5x5:.4
    
  • Cut a video to keep the first 5.4 seconds:
    mencoder big-file.avi -ss 0 -endpos 5.4 -ovc copy -oac copy -o cutted.avi
    
  • Cut a video to keep everything exept the first 5.4 seconds:
    mencoder big-file.avi -ss 5.4 -ovc copy -oac copy -o cutted.avi
    
  • Show all mplayer filter list:
    mplayer -vf help
    
  • Get help of a particular filter (eq2 in this example):
    mplayer -vf eq2=help
    
  • Here is the filter I use to light up a video taken in the dark with my cheap camera. Of course it add noise but thanks to this we can distinguish shapes in the dark.
    mencoder -vf eq2=1.61:1.95:0.54:2.43 -oac pcm -ovc lavc video.avi -o bright-vid.avi
    
  • And this is the command to preview the result of the filter used above:
    mplayer video.avi -vf eq2=1.61:1.95:0.54:2.43
    
  • This is how I convert raw videos taken with my digital camera into ISO standard MPEG-4 (DivX 5, XVID compatible) videos [to encode in grayscale, add :gray option to -lavcopts]:
    mencoder source.avi -ovc lavc -oac lavc -ffourcc DX50 -lavcopts vcodec=mpeg4:vbitrate=400:v4mv:mbd=2:trell:autoaspect:dia=2:acodec=mp3:abitrate=32:vpass=1 -vf hqdn3d -o output.avi
    mencoder source.avi -ovc lavc -oac lavc -ffourcc DX50 -lavcopts vcodec=mpeg4:vbitrate=400:v4mv:mbd=2:trell:autoaspect:dia=2:acodec=mp3:abitrate=32:vpass=2 -vf hqdn3d -o output.avi
    
  • Play all videos of the current folder fullscreen at 4x speed with 50% more brightness:
    mplayer -speed 4 -brightness 50 -fs ./*.avi
    
  • Extract audio stream from a video:
    mplayer -dumpaudio -dumpfile audio.ac3 video_source.mpg
    

FFmpeg

  • Concatenate a series of videos and transcode the audio output to a flac file. This only works with certain multimedia container (MPEG-1, MPEG-2 PS, DV):
    cat M2U01802.MPG M2U01803.MPG M2U01804.MPG | ffmpeg -i - -acodec flac output.flac
    
  • Remove the first 16 seconds of video and change container to Matroska:
    ffmpeg -ss 16 -i ./MVI_8763.MOV -vcodec copy -acodec copy ./MVI_8763.mkv
    
  • Extract the first frame of a video (great to generate image preview):
    ffmpeg -i video.mov -r 1  -t 00:00:01 -f image2 images%05d.png
    
  • Transcode the video stream to the lossless HuffYUV codec:
    ffmpeg -i ./MVI_1714.MOV -vcodec huffyuv -sameq ./MVI_1714-lossless.avi
    

Transcode

  • Merge multiple video into one:
    avimerge -i part1.avi part2.avi -o big-file.avi
    
  • Extract the raw subtitle stream. The -a 0x21 option correspond to the subtitle stream’s hexadecimal number (= 0×20 + id of the stream):
    tccat -i /space/st-tng/dic1/ -T 1 -L | tcextract -x ps1 -t vob -a 0x22 > subs-en
    
  • List export video codecs:
    transcode -i . -y ffmpeg -F list
    

VLC

  • Transcode the first video stream found in a m3u playlist to a 384 kbps MPEG-2 video and 48 kHz Vorbis audio, and serve the resulting stream ina Ogg container to http://localhost:8888. To save bandwisth we reduce by two the size of the video:
    vlc -vvv http://mafreebox.freebox.fr/freeboxtv/playlist.m3u --sout '#transcode{vcodec=mp2v,vb=384,scale=0.5,acodec=vorbis,ab=48,channels=1}:standard{access=http,mux=ogg,url=:8888}' -I ncurses 2> /dev/null
    

Others

  • List MLT audio codecs:
    melt -query "audio_codecs"
    
  • Extract to chapter.txt the chapter file of the track n°1 of the DVD:
    dvdxchap -t 1 /mnt/cdrom > chapter.txt
    
  • Test XV video driver output via gstreamer v0.10:
    gst-launch-0.10 videotestsrc ! xvimagesink
    

Extract All Frames of a Video as Jpeg Images

You need to extract all frames of a video and save them as .jpg images files ? Transcode can do the job for you thanks to this command line:

transcode -i video.avi -y im

Of course with some parameters tweaking you will be able to save images in any format.

Do this on little video only, else the number of generated files will be so huge that all your hard drive space will be eaten in a couple of minutes. Be carefull !