Skip to content

A Python script to encode iMovie .dv files to MP4 using HandBrake

December 29, 2008

I recently captured some old 8mm video tape of the family to iMovie. 
My strategy for archiving is to copy the material to a new medium every
few years. We will see how that works out.

In any event, I want to throw all the videos on a hard drive and store
it offsite in our safety deposit box. However, the raw .DV footage is
too large and won’t fit (and I am too cheap to buy more hard drives or
a larger safety deposit box). 

Necessity being the mother of invention, I needed a script to encode and compress
all my .DV footage. MP4 seems to be a good choice for a format that is widely supported and offers good compression quality.  I wanted to preserve the folder event structure in
iMovie – so a simple find command was not going to cut it.

I started off with bash – but quickly became frustrated with the quirky
syntax. One of the problems with shell scripts is that file and path
names are strings – not objects. You often run into escaping issues
when presented with long file names and special characters.  You can
work around these issues – but it is much more painful than it ought to

I elected to give python a go and see if it was any easier. This is my
first python script – and I don’t claim this is a particullarily good
example. However – if you are facing the same issue – please feel free
to use this as a starting point.

You will need a copy of the Handbrake CLI  to perform the mp4 encoding. Handbrake uses the x264 encoder.

Without further ado, here is the script:

 1 #!/usr/bin/env python
 2 # Script to encode DV files to MP4 using HandBrakeCLI
 3 # Preserves the folder structure of the iMove events
 4 __author__="warrenstrange"
 5 __date__ ="$Dec 28, 2008 6:28:09 PM$"
 7 import os
 8 import subprocess
 9 import os.path
11 # Source for .dv files
12 root = "/Volumes/Velocity1/iMovie Events.localized"
13 # Destination for encoded MP4 files
14 dest = "/Volumes/WSTCD/mp4.bak"
15 #dest = "/var/tmp/xx"
17 def encode(path, dest):
18     for root, dirs, files in os.walk(path, topdown=False):
19         for name in files:
20             # Get the extension
21             (base, ext)=os.path.splitext(name)
22             # If it is a .dv file
23             if ext.lower() == ".dv":
24                 # Get the directory folder where the .dv file is stored
25                 (h,tail) = os.path.split(root)
26                 # Form the destination directory folder name
27                 destdir = os.path.join(dest,tail)
28                 # Create the destination directory if required
29                 if not os.path.exists(destdir):
30                     os.mkdir(destdir)
31                 # Form the output file name (.MP4 extenstion)
32                 newfile = os.path.join(destdir, base + ".MP4")
33                 infile = os.path.join(root,name)
34                 # If it already exists don't overwrite it - skip it
35                 if os.path.exists(newfile):
36                     print "skipping " + infile
37                 else:
38            ["HandBrakeCLI", "--preset=Universal", "-i" ,infile, "-o", newfile])
41 if __name__ == "__main__":
42     encode(root, dest)

Comments are closed.

%d bloggers like this: