Skip to content

Instantly share code, notes, and snippets.

@ejs94
Created January 12, 2025 00:05
Show Gist options
  • Save ejs94/c0d6b2da66cfb38dc08fa3960752126c to your computer and use it in GitHub Desktop.
Save ejs94/c0d6b2da66cfb38dc08fa3960752126c to your computer and use it in GitHub Desktop.
I created a script to automate the creation of m3u inside the .hidden folder for psx games
import os
import glob
from collections import defaultdict
def delete_m3u_files():
"""
Deletes all .m3u files in the current directory, including the 'output' folder and any subfolders.
"""
m3u_files = glob.glob("**/*.m3u", recursive=True) # Find .m3u files in all subdirectories
for m3u_file in m3u_files:
os.remove(m3u_file)
print(f"{len(m3u_files)} .m3u file(s) deleted.")
def create_output_folder():
"""
Creates the 'output' folder if it doesn't exist.
"""
if not os.path.exists("output"):
os.makedirs("output")
print("Folder 'output' created.")
def get_games_from_chd_files():
"""
Finds all .chd files that contain "Disc" in their names and organizes them by game.
Returns:
dict: A dictionary where the key is the base name of the game (without Disc and number)
and the value is a sorted list of corresponding disc files.
"""
chd_files = glob.glob("*.chd")
games = defaultdict(list)
for chd_file in chd_files:
if "Disc" in chd_file: # Only consider files with "Disc" in the name
# Extract the base name of the game, without the disc and extension
base_name = chd_file.split("(Disc")[0].strip()
games[base_name].append(chd_file)
# Sort the discs for each game
for game_name in games:
games[game_name].sort() # Sort alphabetically (Disc 1, Disc 2, ...)
return games
def create_m3u_files_for_all_games(games):
"""
Creates .m3u files for all the games provided.
:param games: A dictionary where the key is the game name and the value is the list of discs.
"""
create_output_folder() # Ensure the 'output' folder exists
for game_name, disc_files in games.items():
m3u_filename = os.path.join("output", f"{game_name.strip()}.m3u")
with open(m3u_filename, "w") as file:
for disc_file in disc_files:
# Write the path with the `.hidden/` prefix
file.write(f".hidden/{disc_file}\n")
print(f"File '{m3u_filename}' created successfully!")
# Main usage example
if __name__ == "__main__":
# Delete existing .m3u files in the current directory and subfolders
delete_m3u_files()
# Get all games from the .chd files
games = get_games_from_chd_files()
# Create .m3u files for each game
create_m3u_files_for_all_games(games)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment