]> git.treefish.org Git - photosort.git/blob - src/misc.py
fixing db creation
[photosort.git] / src / misc.py
1 import datetime
2 import logging
3 import mimetypes
4 import os
5 import PIL.Image
6 import re
7 import shutil
8 import subprocess
9
10 def walk_media_files(dir_path):
11     for root, dirs, files in os.walk(dir_path):
12         for f in files:
13             file_path = os.path.join(root, f)
14             if is_media_file(file_path):
15                 yield (f, file_path)
16
17 def extract_meta_time(file_path):
18     if is_media_file(file_path, types=['image']):
19         return _extract_image_timestamp(file_path)
20     elif is_media_file(file_path, types=['video']):
21         return _extract_video_timestamp(file_path)
22
23 def find_file(base_dir, name, size, meta_time, exclude_dir):
24     for root, dirs, files in os.walk(base_dir):
25         if root.startswith(exclude_dir):
26             continue
27         for other_name in files:
28             if other_name == name:
29                 full_path = os.path.join(root, other_name)
30                 if os.path.getsize(full_path) == size:
31                     other_meta_time = extract_meta_time(full_path)
32                     if meta_time == other_meta_time:
33                         return root
34     return None
35
36 def import_file(src_file_path, dst_file_path):
37     shutil.copyfile(src_file_path, dst_file_path)
38     src_stat = os.stat(src_file_path)
39     dst_stat = os.stat(dst_file_path)
40     os.utime( dst_file_path, ns=(dst_stat.st_atime_ns, src_stat.st_mtime_ns) )
41
42 def is_media_file(file_path, types=['image', 'video']):
43     if not os.path.isfile(file_path):
44         return False
45     mime_type = mimetypes.guess_type(file_path)[0]
46     if not mime_type:
47         return False
48     if not mime_type.split('/')[0] in types:
49         return False
50     return True
51
52 def _extract_image_timestamp(file_path):
53     time = None
54     try:
55         with PIL.Image.open(file_path) as image:
56             exif = image._getexif()
57             if exif and 36867 in exif:
58                 time = datetime.datetime\
59                                .strptime(exif[36867], '%Y:%m:%d %H:%M:%S')\
60                                .timestamp()
61     except Exception as e:
62         logging.warn("Error extracting exif for %s: %s", file_path, str(e))
63     return time
64
65 def _extract_video_timestamp(file_path):
66     p = subprocess.run(['ffmpeg', '-i', file_path],
67                        capture_output=True, encoding='UTF-8')
68     for line in p.stderr.splitlines():
69         m = re.search('^.*creation_time.*: ([^ ]+)$', line)
70         if m:
71             return datetime.datetime\
72                            .strptime(m.group(1), '%Y-%m-%dT%H:%M:%S.%fZ')\
73                            .timestamp()
74     return None