Initial Commit of working files

This commit is contained in:
Jaisen Mathai 2015-10-02 00:20:27 -07:00
parent a8d08a4be6
commit f276d37e7f
7 changed files with 175 additions and 1 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
**/.DS_Store
**/*.pyc

View File

@ -1 +1,2 @@
# hyde
# elodie
pip install exifread

0
elodie/__init__.py Normal file
View File

29
elodie/filesystem.py Normal file
View File

@ -0,0 +1,29 @@
import glob
import os
import time
class FileSystem:
def get_current_directory(self):
return os.getcwd()
def get_file_name_for_video(self, video):
if(not video.is_valid()):
return None
metadata = video.get_metadata()
if(metadata == None):
return None
file_name = '%s-%s-%s.%s' % (time.strftime('%d-%H-%M', metadata['date_taken']), metadata['base_name'], metadata['length'].replace(':', '-'), metadata['extension'])
return file_name
def get_folder_name_by_date(self, time_obj):
return time.strftime('%Y-%m', time_obj)
def create_directory(self, directory_path):
if not os.path.exists(directory_path):
os.makedirs(dest_directory)

0
elodie/media/__init__.py Normal file
View File

102
elodie/media/video.py Normal file
View File

@ -0,0 +1,102 @@
#!/usr/bin/env python
from sys import argv
import mimetypes
import os
import re
import subprocess
import time
"""
Video package that handles all video operations
"""
"""
Video class for general video operations
"""
class Video(object):
# Constructor
def __init__(self, source=None):
self.__valid_extensions = ['avi','m4v','mov','mp4']
self.source = source
def get_metadata(self):
if(not self.is_valid()):
return None
source = self.source
metadata = {
"date_taken": self.__get_date_taken(),
"length": self.__get_duration(),
"mime_type": self.__get_mimetype(),
"base_name": os.path.splitext(os.path.basename(source))[0],
"extension": self.__get_extension()
}
return metadata
#directory_name = time.strftime('%Y-%m', date_taken)
#file_base_name = path_regex.group(1)
#file_extension = path_regex.group(2)
#file_name = '%s-%s-%s.%s' % (time.strftime('%d-%H-%M', date_taken), file_base_name, video_length, file_extension)
def is_valid(self):
source = self.source
# we can't use self.__get_extension else we'll endlessly recurse
return os.path.splitext(source)[1][1:].lower() in self.__valid_extensions
#
# Private methods
#
# get the min() of mtime and ctime
# returns a time object
def __get_date_taken(self):
if(not self.is_valid()):
return None
source = self.source
seconds_since_epoch = min(os.path.getmtime(source), os.path.getctime(source))
if(seconds_since_epoch == 0):
return None
return time.gmtime(seconds_since_epoch)
# get the duration of a video in seconds
# uses ffmpeg
def __get_duration(self):
if(not self.is_valid()):
return None
source = self.source
result = subprocess.Popen(['/usr/local/bin/ffprobe', source],
stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
for key in result.stdout.readlines():
if 'Duration' in key:
return re.search('(\d{2}:\d{2}.\d{2})', key).group(1).replace('.', ':')
# returns file extension
def __get_extension(self):
if(not self.is_valid()):
return None
source = self.source
return os.path.splitext(source)[1][1:].lower()
# returns the mime type
def __get_mimetype(self):
if(not self.is_valid()):
return None
source = self.source
mimetype = mimetypes.guess_type(source)
if(mimetype == None):
return None
return mimetype[0]
class Transcode(object):
# Constructor takes a video object as it's parameter
def __init__(self, video=None):
self.video = video

40
store_video.py Executable file
View File

@ -0,0 +1,40 @@
#!/usr/bin/env python
import os
import shutil
import sys
from elodie.media.video import Video
from elodie.filesystem import FileSystem
print 'Running with arguments %r' % sys.argv
destination = '%s/Dropbox/Videos' % os.path.expanduser('~')
if __name__ == '__main__':
if(len(sys.argv) < 2):
print "No arguments passed"
sys.exit(0)
file_path = sys.argv[1]
filesystem = FileSystem()
video = Video(file_path)
# check if the file is valid else exit
if(not video.is_valid()):
print "File is not valid"
sys.exit(0)
metadata = video.get_metadata()
directory_name = filesystem.get_folder_name_by_date(metadata['date_taken'])
dest_directory = '%s/%s' % (destination, directory_name)
file_name = filesystem.get_file_name_for_video(video)
dest = '%s/%s' % (dest_directory, file_name)
if not os.path.exists(dest_directory):
os.makedirs(dest_directory)
shutil.copy2(file_path, dest)