10 def walk_media_files(dir_path):
 
  11     for root, dirs, files in os.walk(dir_path):
 
  13             file_path = os.path.join(root, f)
 
  14             if is_media_file(file_path):
 
  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)
 
  23 def find_file(dir_path, file_name, file_size, exclude_dir):
 
  24     for root, dirs, files in os.walk(dir_path):
 
  25         if root.startswith(exclude_dir):
 
  29                 full_path = os.path.join(root, f)
 
  30                 if os.path.getsize(full_path) == file_size:
 
  34 def import_file(src_file_path, dst_file_path):
 
  35     shutil.copyfile(src_file_path, dst_file_path)
 
  36     src_stat = os.stat(src_file_path)
 
  37     dst_stat = os.stat(dst_file_path)
 
  38     os.utime( dst_file_path, ns=(dst_stat.st_atime_ns, src_stat.st_mtime_ns) )
 
  40 def is_media_file(file_path, types=['image', 'video']):
 
  41     if not os.path.isfile(file_path):
 
  43     mime_type = mimetypes.guess_type(file_path)[0]
 
  46     if not mime_type.split('/')[0] in types:
 
  50 def _extract_image_timestamp(file_path):
 
  53         with PIL.Image.open(file_path) as image:
 
  54             exif = image._getexif()
 
  55             if exif and 36867 in exif:
 
  56                 time = datetime.datetime\
 
  57                                .strptime(exif[36867], '%Y:%m:%d %H:%M:%S')\
 
  59     except Exception as e:
 
  60         logging.warn("Error extracting exif for %s: %s", file_path, str(e))
 
  63 def _extract_video_timestamp(file_path):
 
  64     p = subprocess.run(['ffmpeg', '-i', file_path],
 
  65                        capture_output=True, encoding='UTF-8')
 
  66     for line in p.stderr.splitlines():
 
  67         m = re.search('^.*creation_time.*: ([^ ]+)$', line)
 
  69             return datetime.datetime\
 
  70                            .strptime(m.group(1), '%Y-%m-%dT%H:%M:%S.%fZ')\