-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixFileExtensions.py
More file actions
75 lines (68 loc) · 3.1 KB
/
Copy pathfixFileExtensions.py
File metadata and controls
75 lines (68 loc) · 3.1 KB
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
import os
import subprocess
import json
import sys
# Uses FFProbe to identify contained file type and renames the file with correct extension.
def get_format_details(file_path):
try:
result = subprocess.run(
["ffprobe", "-v", "error", "-show_format", "-show_streams", "-of", "json", file_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
result_json = json.loads(result.stdout)
format_name = result_json['format']['format_name']
format_long_name = result_json['format'].get('format_long_name', 'N/A')
codec_long_name = 'N/A'
for stream in result_json.get('streams', []):
if stream['codec_type'] == 'audio':
codec_long_name = stream.get('codec_long_name', 'N/A')
break
return format_name, format_long_name, codec_long_name
except Exception as e:
return f"Error: {e}", "N/A", "N/A"
def get_extension_for_format(format_name, codec_long_name):
# Lookup for specific formats
if "webm" in format_name:
return "webm"
elif "mov" in format_name:
return "mov"
elif "ogg" in format_name:
return "ogg"
elif "wav" in format_name:
return "wav"
# Default None if format is not recognized
return None
def rename_files_based_on_format(folder_path, log_file):
deleted_files_count = 0
with open(log_file, 'w') as log:
for root, dirs, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
format_name, format_long_name, codec_long_name = get_format_details(file_path)
if "Error" not in format_name:
new_extension = get_extension_for_format(format_name, codec_long_name)
if new_extension:
new_file_name = f"{os.path.splitext(file)[0]}.{new_extension}"
new_file_path = os.path.join(root, new_file_name)
os.rename(file_path, new_file_path)
log.write(f"Renamed: {file} to {new_file_name} | Format: {format_name} ({format_long_name}), Codec: {codec_long_name}\n")
else:
os.remove(file_path)
deleted_files_count += 1
log.write(f"Deleted: {file} | Unrecognized format: {format_name} ({format_long_name}), Codec: {codec_long_name}\n")
else:
os.remove(file_path)
deleted_files_count += 1
log.write(f"Deleted: {file} | Reason: {format_name}\n")
print(f"Total files deleted: {deleted_files_count}")
log.write(f"\nTotal files deleted: {deleted_files_count}\n")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python fixFileExtensions.py <folder_path>")
sys.exit(1)
folder_path = sys.argv[1]
log_file = "./logs/fixFileExtensions.log"
rename_files_based_on_format(folder_path, log_file)
print(f"Renaming and logging process completed. Check {log_file} for details.")