-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3.py
59 lines (44 loc) · 1.65 KB
/
mp3.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
# https://github.com/CFC-Servers/gm_addon_optimization_tricks/blob/main/tools/sound-compression/wav-to-mp3.py
# Purpose: WAV -> MP3 since it's saves size and doesnt' have any comprehessions to it's quality
try:
import pydub
except:
print("[!] pydub is not installed, and it's required to use this script.")
exit(0)
import os
PATH_TO_DIR = r"..."
old_size, new_size = 0, 0
replaced_count = 0
replaced_files = {}
# Convert .wav to .mp3
for path, dirs, files in os.walk(PATH_TO_DIR):
for name in files:
fpath = os.path.join(path, name)
ftype = name.split(".")[-1]
if ftype == ".wav":
old_size += os.path.getsize(fpath)
try:
sound = pydub.AudioSegment.from_wav(fpath)
sound.export(fpath, format="mp3")
except:
print(f"[!] {fpath} couldn't be exported to .mp3")
new_size += os.path.getsize(fpath)
replaced_count += 1
fname = os.path.basename(fpath)
replaced_files[fname] = fname.replace(".wav", ".mp3")
print(f"[+] {fname} was converted to .mp3")
# Replacing all .wav occurences in .lua to .mp3
for path, dirs, files in os.walk(PATH_TO_DIR):
for name in files:
fpath = os.path.join(path, name)
ftype = name.split(".")[-1]
if ftype == ".lua":
with open(fpath, "r", encoding="utf8") as file:
contents = file.read()
for old, new in replaced_files.items():
contents = contents.replace(old, new)
with open(fpath, "w", encoding="utf8") as file:
file.write(contents)
# fi
print(f"[#] {replaced_count} files were replaced.")
print(f"[#] Reduced Size: {round((old_size - new_size) / 1048576)}MiB ({round((1 - new_size / old_size) * 100, 2)}%)")