Skip to content

Instantly share code, notes, and snippets.

@Raltyro
Last active July 23, 2023 10:26
Show Gist options
  • Select an option

  • Save Raltyro/ae55209167bda5046f621030d2020b46 to your computer and use it in GitHub Desktop.

Select an option

Save Raltyro/ae55209167bda5046f621030d2020b46 to your computer and use it in GitHub Desktop.
optimize-to-ogg.py, optimize-png.py
# Script by Raltyro
# drag and drop the png files or folders to this script that includes png files
# be careful! it's wildfire, so subdirectories are applied too!
# Requires oxipng: https://github.com/shssoichiro/oxipng
import sys
import os
import subprocess, shlex
PNG_EXT = ".png"
linethingylol = "---------------------------------------------------------"
cwd = os.getcwd().replace("\\", "/")
oxipng = "oxipng" #'"' + cwd + "/oxipng.exe\""
def usage():
print("Usage: {} [dir, png]".format(sys.argv[0]))
sys.exit(1)
def scan(path):
try:
subprocess.run(shlex.split(oxipng + " -o max \"" + path + "\""))
except:
print("Something went wrong while trying to optimize '" + path + "'")
def iterate(par, fake_par=''):
for filename in os.listdir(par):
path = (par + "/" + filename).replace("\\", "/")
splits = os.path.splitext(filename)
name = splits[0]
ext = splits[1].lower()
if os.path.isdir(path):
iterate(path, fake_par + name + "/")
elif ext == PNG_EXT:
scan(path)
def main():
if len(sys.argv) < 2:
usage()
for i in range(len(sys.argv)):
if i == 0: continue
path = sys.argv[i].replace("\\", "/")
if os.path.isdir(path):
iterate(path)
elif os.path.isfile(sys.argv[i]):
scan(path)
if __name__ == "__main__":
main()
# Script by Raltyro
# drag and drop the wav files or folders to this script that includes wav files
# be careful! it's wildfire, so subdirectories are applied too!
# Requires ffmpeg and optivorbis
# ffmpeg: https://ffmpeg.org/download.html (Executable Shared Build)
# optivorbis: https://github.com/OptiVorbis/OptiVorbis/releases
import sys
import os
import subprocess, shlex
EXT_DIR_NAMES = {
".wav": "original-wavs",
".mp3": "original-mp3s",
".ogg": "original-oggs",
".flac": "original-flacs",
".xma": "original-xmas"
}
EXT = ".ogg"
deleteOGfiles = False
ffmpeg = "ffmpeg" #'"' + cwd + "/ffmpeg/bin/ffmpeg.exe\""
optivorbis = "optivorbis" #'"' + cwd + "/optivorbis.exe\""
linethingylol = "---------------------------------------------------------"
EXTS = EXT_DIR_NAMES.keys()
EXT_DIR_NAMES_LIST = EXT_DIR_NAMES.values()
cwd = os.getcwd().replace("\\", "/")
print(ffmpeg)
print(optivorbis)
def usage():
print("Usage: {} [dir, wav, mp3, ogg, flac]".format(sys.argv[0]))
sys.exit(1)
def scan(og_path, new_path, backup_path):
print(linethingylol)
print(og_path + " => " + new_path)
try:
if not os.path.isfile(backup_path): os.rename(og_path, backup_path)
subprocess.run(shlex.split(ffmpeg + " -i \"" + backup_path + "\" -acodec libvorbis -f ogg -q 4.0 \"" + new_path + ".temp\""))
if deleteOGfiles: os.remove(backup_path)
subprocess.run(shlex.split(optivorbis + " -q -r ogg2ogg \"" + new_path + ".temp\" \"" + new_path + "\""))
os.remove(new_path + ".temp")
except:
print("Something went wrong while trying to convert '" + og_path + "'")
if not os.path.isfile(og_path): os.rename(backup_path, og_path)
def iterate(par, real_par, fake_par=''):
for filename in os.listdir(par):
if filename in EXT_DIR_NAMES_LIST:
continue
splits = os.path.splitext(filename)
name = splits[0]
ext = splits[1].lower()
og_path = (par + "/" + filename).replace("\\", "/")
new_path = (par + "/" + name + EXT).replace("\\", "/")
if os.path.isdir(og_path):
if not deleteOGfiles:
for dir in EXT_DIR_NAMES.values(): check_dir(real_par + "/" + dir + fake_par + name)
iterate(og_path, real_par, fake_par + name + "/")
elif ext in EXTS:
backup_path = (real_par + "/" + EXT_DIR_NAMES.get(ext) + '/' + fake_par + filename).replace("\\", "/")
if deleteOGfiles: backup_path = og_path + ".temp"
scan(og_path, new_path, backup_path)
def check_dir(diraa):
if not os.path.isdir(diraa):
os.mkdir(diraa)
elif os.path.isfile(diraa):
print("cannot run cause there is already an existing file named \"" + diraa + "\"")
sys.exit(1)
def main():
if len(sys.argv) < 2:
usage()
for i in range(len(sys.argv)):
if i == 0: continue
path = sys.argv[i].replace("\\", "/")
if os.path.isdir(path):
for dir in EXT_DIR_NAMES.values(): check_dir(path + "/" + dir + "/")
iterate(path, path)
elif os.path.isfile(sys.argv[i]):
filename = os.path.basename(path)
splits = os.path.splitext(filename)
if not splits[1] in EXTS: continue
name = splits[0]
par = os.path.dirname(path) + "/"
scan(path, par + name + EXT, par + "OG-" + filename)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment