-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename-mpv-images
executable file
·47 lines (36 loc) · 1.19 KB
/
rename-mpv-images
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
"""
Rename images from mpv screenshots, retaining the timestamp in the name, but removing the original filename
\b
My mpv config has the following line:
screenshot-template="%f-%wH.%wM.%wS.%wT"
\b
That creates images with names like:
some-long-filename-here-12.34.56.789.jpg
\b
This script renames them to:
name-12.34.56.789.jpg
"""
from pathlib import Path
from typing import Sequence
import click
@click.command(help=__doc__)
@click.option("--name", prompt=True, type=str, help="prefix for images")
@click.argument(
"IMAGES", type=click.Path(exists=True, path_type=Path), required=True, nargs=-1
)
def main(name: str, images: Sequence[Path]) -> None:
if not name.strip():
raise click.BadParameter("name cannot be empty")
for image in images:
if "-" not in image.stem:
click.echo(f"Skipping {image}, no '-' specifying timestamp")
continue
timestamp = image.stem.rsplit("-", maxsplit=1)[1]
if not timestamp.strip():
click.echo(f"Skipping {image}, no timestamp")
continue
new_name = f"{name}-{timestamp}{image.suffix}"
image.rename(new_name)
if __name__ == "__main__":
main()