-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenameVideos.py
62 lines (50 loc) · 1.69 KB
/
renameVideos.py
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
import sys
import os
import re
import datetime
from pymediainfo import MediaInfo
def transcodeDate(date):
# # Extract time
# dateRegex = re.compile(r'(\d\d)-(\d\d)-(\d\d)')
# mo = re.search(dateRegex, date)
# # Adapt time to timezone
# dateT = datetime.time(
# int(mo.group(1)),
# int(mo.group(2)),
# int(mo.group(3)))
# print(dateT)
# TODO?: change time based on location
# Remove characters which do now work on Windows
date = date.replace(":", "-")
return date
def renameFile(root, file):
filepath = os.path.join(root, file)
# Gather information
meta = MediaInfo.parse(filepath)
encoded_date = None
for track in meta.tracks:
if track.track_type == 'Video':
encoded_date = track.encoded_date
if track.track_type == 'General':
file_extension = track.file_extension
# Rename files
if encoded_date != None:
encoded_date = transcodeDate(encoded_date)
newfilepath = os.path.join(root, encoded_date + "." + file_extension)
if filepath != newfilepath:
# TODO: If file already exists, create file with suffix
os.rename(filepath, newfilepath)
print("Renamed from: " + filepath + " to: " + newfilepath)
else:
print("File has the same filename: " + filepath)
else:
print("File has no encoded date or is not compatible: " + filepath)
# Stop if no input files
if len(sys.argv) == 1:
print("Script needs one or more paths.")
sys.exit()
# Walk through files in directory
for i in sys.argv[1:]:
for root, dirs, files in os.walk(i):
for file in files:
renameFile(root, file)