Skip to content

Instantly share code, notes, and snippets.

@stefan-girlich
Last active August 29, 2015 14:20
Show Gist options
  • Save stefan-girlich/48425d0379e3401da86e to your computer and use it in GitHub Desktop.
Save stefan-girlich/48425d0379e3401da86e to your computer and use it in GitHub Desktop.
Provides a simple CLI wizard for copying images and videos from a mounted camera and sorting them into timestamped folders.
#!/usr/bin/env python3
import os.path
import time
import datetime
import sys
import shutil
from os import listdir, mkdir
from os.path import isfile, join, basename
CONFIG = {
'fileExtensionsImage': ['jpg', 'jpeg', 'png'], # TODO RAW?
'fileExtensionsVideo': ['mov', 'avi', 'mpg'],
'sourcePath': '/Volumes/NO NAME/DCIM/100OLYMP/',
# 'destinationPathImage': '/Volumes/documents/Bilder/',
# 'destinationPathVideo': '/Volumes/documents/Videos/privat/'
'destinationPathImage': '/Users/steve/tools_own/tmp/image',
'destinationPathVideo': '/Users/steve/tools_own/tmp/video'
}
def buildMediaIndex(srcDir):
mediaIndex = {}
files = []
for f in listdir(srcDir):
absPath = join(srcDir, f)
if isfile(absPath):
fileExt = os.path.splitext(absPath)[1][1:].lower()
if fileExt in CONFIG['fileExtensionsImage']:
fileType = 'image'
elif fileExt in CONFIG['fileExtensionsVideo']:
fileType = 'video'
else:
continue
modTime = str(getModificationTimeUnixTime(absPath))
if modTime not in mediaIndex.keys():
mediaIndex[modTime] = {}
if fileType not in mediaIndex[modTime].keys():
mediaIndex[modTime][fileType] = []
mediaIndex[modTime][fileType].append(absPath)
return mediaIndex
def getModificationTimeUnixTime(filePath):
fileModTime = datetime.datetime.fromtimestamp(os.path.getmtime(filePath)).date()
fileModTime = int(time.mktime(fileModTime.timetuple()))
return fileModTime
def printMediaIndex(mediaIndex):
print('found media files:')
for modTime in mediaIndex.keys():
date = datetime.datetime.fromtimestamp(int(modTime)).strftime('%Y-%m-%d')
if 'image' in mediaIndex[modTime]:
imageCount = len(mediaIndex[modTime]['image'])
else:
imageCount = 0
if 'video' in mediaIndex[modTime]:
videoCount = len(mediaIndex[modTime]['video'])
else:
videoCount = 0
print(date + '\t' + str(imageCount) + ' images \t' + str(videoCount) + ' videos')
print('\n')
def copyFiles(albumDestPath, albumName, files):
albumDestPath = join(albumDestPath, albumName)
mkdir(albumDestPath)
for fileSrcPath in files:
fileBasename = basename(fileSrcPath)
fileDestPath = join(albumDestPath, fileBasename)
print('Copying ' + fileBasename + ' to ' + albumDestPath)
shutil.copy2(fileSrcPath, fileDestPath)
# ============================================
srcPath = CONFIG['sourcePath']
imgDestPath = CONFIG['destinationPathImage']
vidDestPath = CONFIG['destinationPathVideo']
errors = [];
if not os.access(srcPath, os.W_OK):
errors.append('Source is not readable: ' + srcPath)
if not os.access(imgDestPath, os.W_OK):
errors.append('Image destination not writable: ' + imgDestPath)
if not os.access(vidDestPath, os.W_OK):
errors.append('Video destination not writable: ' + vidDestPath)
for err in errors:
print(err)
if len(errors) > 0:
print('Aborting.')
sys.exit()
mediaIndex = buildMediaIndex(CONFIG['sourcePath'])
printMediaIndex(mediaIndex)
print('source:\t' + CONFIG['sourcePath'])
print('image destination:\t' + CONFIG['destinationPathImage'])
print('video destination:\t' + CONFIG['destinationPathVideo'])
print('\n')
userInput = input('Hit Enter to start.\n')
if len(userInput) > 0:
print('Aborting.')
sys.exit()
print('Enter the album names:')
for modTime in mediaIndex.keys():
timestamp = datetime.datetime.fromtimestamp(int(modTime)).strftime('%Y%m%d')
albumName = timestamp + ' ' + input('Enter album directory name: ' + timestamp + ' ')
if 'image' in mediaIndex[modTime]:
copyFiles(CONFIG['destinationPathImage'], albumName, mediaIndex[modTime]['image'])
if 'video' in mediaIndex[modTime]:
copyFiles(CONFIG['destinationPathVideo'], albumName, mediaIndex[modTime]['video'])
print('\n')
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment