This commit is contained in:
teitoku
2025-07-16 16:01:42 +09:00
committed by teo3300
commit d71ffb714f
9 changed files with 290 additions and 0 deletions

28
scripts/data.py Normal file
View File

@ -0,0 +1,28 @@
ENV = {
'usage': [
'music_import.py',
'<translation_db>',
'<source_directory>',
'[destination_directory]'
],
'help': [
"Provide a translation, a source and a destination path to copy the files renaming them accordingly, ",
"The translation file is a csv, containing the following entries:",
""
"<source_file>" "<destination_file>"
"",
"Using relative path for source and destination file",
"Destination file name should looke something like:",
"",
"<CODE>_<description>.<extension>",
"",
"CODE: weather condition name",
"description: just a way for you to recognize the file and for the system's FS to not overwrite files",
"extension: any format supported by mpv should be ok",
"",
"CODE recognized values are:", "'day' 'night' 'fog' 'rain' 'snow' 'thunder'",
"Only when teh weather is clear (day/night): a check on current time is performed against the known",
"sunset/sunrise time to determine which to pick"
],
'dst_dir': './music'
}

View File

@ -0,0 +1,49 @@
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)

71
scripts/music_import.py Normal file
View File

@ -0,0 +1,71 @@
import sys
import os
from data import ENV
def eprint(*args):
print(*args, file=sys.stderr)
def solve_or_escape(param):
if not param[0] == '$':
return param
if x[1] == '$':
return param[1:]
return ENV[param[1:]]
def l_map(x,y): return list(map(x,y))
def cp_file(command):
dst = os.path.normpath(f"{ENV['dst']}/{command[0]}")
src = os.path.normpath(f"{ENV['src']}/{command[1]}")
eprint(f'Copying ... "{src}" to "{dst}"')
shutil.copy(src, dst)
import shutil
def file_import(commands):
try:
l_map(cp_file, commands)
except FileNotFoundError as e:
eprint('Error:', e)
def main():
if "-h" in sys.argv or "--help" in sys.argv:
eprint(); eprint(' ', sys.argv[0])
eprint(); eprint(' -', ' '.join(ENV['usage']))
eprint(); [eprint(' ', x) for x in ENV['help']]
eprint()
exit(0)
if len(sys.argv) < len(ENV['usage']):
eprint('Usage:', ' '.join(ENV['usage']))
exit(1)
if len(sys.argv) == len(ENV['usage']):
ENV['dst'] = os.path.normpath(sys.argv[-1])
ENV['src'] = os.path.normpath(sys.argv[-2])
ENV['mus_db'] = os.path.normpath(sys.argv[-3])
dst_dir = ENV['dst']
src_dir = ENV['src']
mus_db = ENV['mus_db']
if not os.path.isfile(mus_db):
eprint(f"{mus_db}: Not a file")
if not os.path.isdir(src_dir):
eprint(f"{src_dir}: Not a directory")
if not os.path.isdir(dst_dir):
eprint(f"{dst_dir}: Not a directory")
import csv
config = []
with open(mus_db, newline='') as csvfile:
config = [row for row in csv.reader(csvfile, delimiter=' ')]
file_import(config)
if __name__ == "__main__":
main()