Skip to content

Instantly share code, notes, and snippets.

@dmerrick
Last active July 8, 2024 17:05
Show Gist options
  • Save dmerrick/5f4f89611dd2135beaa681007a5e1856 to your computer and use it in GitHub Desktop.
Save dmerrick/5f4f89611dd2135beaa681007a5e1856 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
from datetime import datetime
import click
from rich.pretty import pprint
DEFAULT_DIR = "/Volumes/a7c/Tallman/A/DCIM"
DEFAULT_FILENAME = "timelapse"
SMALL_VID_FLAG = "-vf 'scale=w=1920: h=1280, setpts=.25*PTS'"
@click.command()
@click.option(
"--dir",
type=click.Path(exists=True, file_okay=False,
dir_okay=True, readable=True),
help="Directory path",
default=DEFAULT_DIR,
)
@click.option("--name", type=str, default=DEFAULT_FILENAME, help="Name of output video")
@click.option("--fps", type=int, default=30, help="Frames per second")
@click.option("--small", type=bool, default=False, help="Create a small test video")
@click.option(
"--moveback", type=bool, default=False, help="Move the final video to dir"
)
@click.option("--open", type=bool, default=False, help="Open the video after creating")
def main(dir, name, fps, small, moveback, open):
sorted_directories = list_directories_by_date_created(dir)
for index, pair in enumerate(sorted_directories):
subdir = pair[0]
command = build_command(dir, name, fps, small,
moveback, open, index, subdir)
print(command)
def build_command(dir, name, fps, small, moveback, open, index, subdir):
# set up the filename
filename = f"{name}{index}.mp4"
if small:
filename = f"{name}{index}-small.mp4"
# Start building the command string
command = [f"cd {dir}"]
command.append("&& ffmpeg -hide_banner -y")
command.append(f"-pattern_type glob -i '{subdir}/*.JPG'")
# Add the small flag if small is True
if small:
command.append(SMALL_VID_FLAG)
command.append(f"-r {fps}")
command.append("-c:v libx264 -crf 20 -pix_fmt yuv420p")
command.append(f"/tmp/{filename}")
if moveback:
command.append(f"&& mv /tmp/{filename} {dir}")
if open:
command.append(f"&& open /tmp/{filename}")
return " ".join(command)
def list_directories_by_date_created(path):
# Get all entries in the directory
entries = os.listdir(path)
# Filter out non-directories
directories = [
entry for entry in entries if os.path.isdir(os.path.join(path, entry))
]
# Get creation times
directories_with_dates = []
for directory in directories:
full_path = os.path.join(path, directory)
creation_time = os.path.getctime(full_path)
creation_date = datetime.fromtimestamp(creation_time)
directories_with_dates.append((directory, creation_date))
# Sort directories by creation date
directories_with_dates.sort(key=lambda x: x[1])
# Return the sorted directories
return directories_with_dates
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment