Skip to content

Instantly share code, notes, and snippets.

@gvangool
Last active December 31, 2018 14:21
Show Gist options
  • Save gvangool/2150609899edb72f6b4d340ec6b2b695 to your computer and use it in GitHub Desktop.
Save gvangool/2150609899edb72f6b4d340ec6b2b695 to your computer and use it in GitHub Desktop.
Photo booth file renamer
"""Tasks to fixup the output of the Area3001 photobooth.
This requires python3.6+ and invoke>=1.2
"""
from datetime import datetime, timedelta
from invoke import task
@task(help={"diff": "Difference in seconds for correction of the timings",
"dry-run": "Don't modify the files, just print what you would do"})
def correct_time(c, diff=0, dry_run=True):
"""Corrects the name of the movies and photos to represent the correct
time."""
if diff == 0:
print("No correction needed")
correct_time_movies(c, diff=diff, dry_run=dry_run)
correct_time_photos(c, diff=diff, dry_run=dry_run)
@task(help={"diff": "Difference in seconds for correction of the timings",
"dry-run": "Don't modify the files, just print what you would do"})
def correct_time_movies(c, diff=0, dry_run=True):
"""Corrects the name of the movies to represent the correct time"""
if diff == 0:
print("No correction needed")
result = c.run("ls *.ts", hide=True)
if result.stdout:
fmt = "%Y-%m-%d_%H-%M-%S.ts" # format of the movie filenames
diff = timedelta(seconds=diff)
for line in result.stdout.split("\n"):
if not line:
continue
original = datetime.strptime(line, fmt)
corrected = original + diff
new_name = corrected.strftime(fmt)
if dry_run:
print(f"Renaming {line} to {new_name}")
else:
c.run("mv {line} {new_name}")
@task(help={"diff": "Difference in seconds for correction of the timings",
"dry-run": "Don't modify the files, just print what you would do"})
def correct_time_photos(c, diff=0, dry_run=True):
if diff == 0:
print("No correction needed")
"""Corrects the name of the photos to represent the correct time.
The datetime format of the pictures is different and these are directories
instead of just files (since 3 picture create a collage)"""
result = c.run("ls | grep -E '^photo'", hide=True)
if result.stdout:
fmt = "photo%Y%m%d%H%M%S"
diff = timedelta(seconds=diff)
for line in result.stdout.split("\n"):
if not line:
continue
original = datetime.strptime(line, fmt)
corrected = original + diff
new_name = corrected.strftime(fmt)
if dry_run:
print(f"Renaming {line} to {new_name}")
else:
c.run("mv {line} {new_name}")
@task(help={"dry-run": "Only print what you would do, but don't modify"})
def rename_photos(c, dry_run=True):
"""Renames photos to give them unique names (to be able to remove them from
the directories)."""
result = c.run("ls | grep -E '^photo'", hide=True)
if result.stdout:
for line in result.stdout.split("\n"):
if not line:
continue
with c.cd(line):
c.run(f"for f in *.jpg; do {'echo' if dry_run else 'mv'} ${{f}} {line}_${{f}}; done")
c.run(f"{'echo' if dry_run else 'mv'} strip.png {line}.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment