Files
weather-bg/scripts/extract_minecraft_music.py
teitoku d71ffb714f Yikes
2025-07-28 16:31:36 +09:00

50 lines
2.0 KiB
Python

import json, os, platform, shutil, sys
'''
Copies audio files from indescript hashed folders to named sorted folders.
You may need to change output path.
'''
MC_ASSETS = ''
# This section should work on any system as well
# print("Your OS is " + platform.system())
if platform.system() == "Windows":
MC_ASSETS = os.path.expandvars(r"%APPDATA%/.minecraft/assets")
elif platform.system() == "Darwin":
MC_ASSETS = os.path.expanduser(r"~/Library/Application Support/minecraft/assets")
else:
MC_ASSETS = os.path.expanduser(r"~/.minecraft/assets")
# Find the latest installed version of minecraft (choose the last element in assets/indexes)
MC_VERSION = os.listdir(MC_ASSETS+"/indexes/")[-1]
# print("Your latest installed version of minecraft is " + MC_VERSION[:-5])
# Change this if you want to put the sound files somewhere else
OUTPUT_PATH = os.path.normpath(os.path.expandvars(os.path.expanduser(sys.argv[1] if len(sys.argv) > 1 else f"./tmp")))
# These are unlikely to change
MC_OBJECT_INDEX = f"{MC_ASSETS}/indexes/{MC_VERSION}"
MC_OBJECTS_PATH = f"{MC_ASSETS}/objects"
MC_SOUNDS = r"minecraft/sounds/"
with open(MC_OBJECT_INDEX, "r") as read_file:
# Parse the JSON file into a dictionary
data = json.load(read_file)
# Find each line with MC_SOUNDS prefix, remove the prefix and keep the rest of the path and the hash
sounds = {k[len(MC_SOUNDS):] : v["hash"] for (k, v) in data["objects"].items() if k.startswith(MC_SOUNDS)}
for fpath, fhash in sounds.items():
if fpath.startswith('music'):
# Ensure the paths are good to go for Windows with properly escaped backslashes in the string
src_fpath = os.path.normpath(f"{MC_OBJECTS_PATH}/{fhash[:2]}/{fhash}")
dest_fpath = os.path.normpath(f"{OUTPUT_PATH}/sounds/{fpath}")
# Make any directories needed to put the output file into as Python expects
os.makedirs(os.path.dirname(dest_fpath), exist_ok=True)
# Copy the file
shutil.copyfile(src_fpath, dest_fpath)