Re-wrapping trail cam .avi files so they can be imported into Lightroom (Python script; ffmpeg)
ArticlesWe have a well-used animal path in our backyard here in San Mateo, California; bobcats, deer, coyotes, raccoons, and skunks make their way past our house virtually every day. The bobcats are our favorites–each spring, they have kittens, and there is nothing cuter than a bobcat family making its way along the railing. A couple times a year, I get a mountain lion on camera.
I use a Bushnell Trophy Cam on a GorillaPod, which I love because it just works. 8 x AA batteries last for months; I pull the card out of the camera every 3 months or so and change out the batteries.
Once upon a time, I was able to import .avi files into Adobe Lightroom (Classic), but after an update one day a couple years ago, .avi import stopped working. The Adobe folks claim that .avi files have never been supported, but if drag 100 files in, usually, one or two still ingest successfully (!). Still, it’s clear that .avi and Lightroom do not get along, so I’ve been rewrapping all of the .avi files into .mp4 containers using ffmpeg, and these have been importing into Lightroom successfully.
While there are probably apps that can rewrap a single .avi into a .mp4 without re-encoding, it’s been easier to script ffmpeg to rewrap every .avi it finds in a directory. Here’s the script. The normal disclaimers apply–I am not responsible for what this does to your files, so please back things up before you play. Enjoy!
"""
This python script rewraps all files that have the extension avi to mp4 in the current
working directory.
Sources:
http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide
"""
import subprocess, os
#-------------------------------------------------------------------------------
# CONFIGURABLE SETTINGS
#-------------------------------------------------------------------------------
# path to ffmpeg bin
FFMPEG_PATH = '/usr/bin/ffmpeg'
#-------------------------------------------------------------------------------
# encoding script
#-------------------------------------------------------------------------------
def process():
cwd = os.getcwd()
# get a list of files that have the extension mkv
filelist = filter(lambda f: f.split('.')[-1] == 'avi' or f.split('.')[-1] == 'AVI', os.listdir(cwd))
filelist = sorted(filelist)
# encode each file
for file in filelist:
print file
encode(file)
def encode(file):
name = ''.join(file.split('.')[:-1])
output = 'rewrapped/' + '{}.mp4'.format(name)
try:
command = [
FFMPEG_PATH, '-y', '-i', file,
'-c', 'copy'
]
newdircommand = ['mkdir', 'rewrapped']
subprocess.call(newdircommand)
command += [output] # add output
subprocess.call(command) # encode the video!
finally:
# always cleanup even if there are errors
pass
if __name__ == "__main__":
process()