forked from AMDmi3/gopro-rename
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopro-rename
executable file
·85 lines (68 loc) · 3.56 KB
/
gopro-rename
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
#
# Copyright (c) 2016-2018,2022 Dmitry Marakasov <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import argparse
import os
import re
import sys
def get_transformed_filename(path: str, args: argparse.Namespace) -> str:
dirname, basename = os.path.split(path)
match = re.match(r'GOPR(\d{4})(\..*)', basename)
if match:
return os.path.join(dirname, f'{args.prefix}{int(match[1]):04}_00{match[2]}')
match = re.match(r'G[PHXL](\d{2})(\d{4})(\..*)', basename)
if match:
return os.path.join(dirname, f'{args.prefix}{int(match[2]):04}_{int(match[1]):02}{match[3]}')
return path
def try_rename_file(path: str, args: argparse.Namespace) -> None:
target_path = get_transformed_filename(path, args)
if target_path != path:
if args.dry_run or args.verbose:
print(f'{path} -> {target_path}', file=sys.stderr)
if os.path.exists(target_path) and not args.force:
raise RuntimeError(f'target path {target_path} exists')
if not args.dry_run:
os.rename(path, target_path)
def process_path(path: str, args: argparse.Namespace) -> None:
if os.path.isfile(path):
try_rename_file(path, args)
elif os.path.isdir(path):
for entry in os.listdir(path):
if entry == '.' or entry == '..':
continue
elif os.path.isfile(os.path.join(path, entry)):
try_rename_file(os.path.join(path, entry), args)
elif args.recursive and os.path.isdir(os.path.join(path, entry)):
process_path(os.path.join(path, entry), args)
def main() -> int:
parser = argparse.ArgumentParser(description='Goes through files or directories specified on command line and renames files produced by GoPro cameras in a more usable and sortable way')
parser.add_argument('-n', '--dry-run', action='store_true', help='do not actually rename anything')
parser.add_argument('-f', '--force', action='store_true', help='rename even if target file exists')
parser.add_argument('-v', '--verbose', action='store_true', help='print actions to stderr')
parser.add_argument('-r', '-R', '--recursive', action='store_true', help='process subdirectories recursively')
parser.add_argument('-p', '--prefix', nargs='?', default='GoPro_', help='prefix for renamed files')
parser.add_argument('paths', metavar='path', nargs='+', help='file or directory to process')
args = parser.parse_args()
for path in args.paths:
process_path(path, args)
return 0
if __name__ == '__main__':
sys.exit(main())