Last active
June 6, 2020 10:11
-
-
Save Blizzke/d6e068ae25aadc85ce3af302e38e0e3e to your computer and use it in GitHub Desktop.
Correct create and modify times of the images/videos based on the filename on my new android device
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
from subprocess import PIPE | |
if __name__ == '__main__': | |
source = '/storage/self/primary/DCIM/Camera' | |
files = subprocess.run(["/usr/local/bin/adb", "shell", f"ls -1 {source}"], check=False, | |
stdout=PIPE).stdout.splitlines() | |
print(f'{len(files)} files found.') | |
import re | |
dt = re.compile(r'.*(\d{4})(\d{2})(\d{2})[-_](\d{2})(\d{2})(\d{2}).*') | |
processed = 0 | |
skipped = 0 | |
for file in files: | |
processed += 1 | |
file = file.decode() | |
(name, extension) = file.rsplit('.', 1) | |
if extension.lower() not in ['jpg', 'mp4']: | |
skipped += 1 | |
continue | |
result = dt.match(name) | |
if result: | |
tm = '-'.join(result.groups()[0:3]) + 'T' + ':'.join(result.groups()[3:6]) | |
output = subprocess.run(["/usr/local/bin/adb", "shell", f"touch -a -m -c -d {tm} '{source}/{file}'"], | |
check=False, stdout=PIPE) | |
else: | |
skipped += 1 | |
if not (processed % 100): | |
print(f'Progress: {processed} processed / {skipped} skipped') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Recently bought a new cellphone and "android file transfer"-ed all my images and videos to it. Unfortunately it messed up the creation/modification times of all of the files, resulting in a big unsorted mess. This little script uses adb to fetch a list from the camera folder and then touch all files