Last active
December 7, 2024 02:57
-
-
Save Laura7089/aae8f9a7548ad70ddd1b0b4aaeadb840 to your computer and use it in GitHub Desktop.
Script to convert the json-formatted ".m3upi" files that Pi Music Player (for android) uses for storage into standard m3u files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Unix only. Call with "./m3upi_to_m3u8.py [path to PiPlaylistSong.m3upi] [path to music directory on PC]. | |
# This script assumes you have the same music in the folder you specify as in the Music folder on your phone. (You can mount your phone with mtpfs and point this script at the Music folder in that if you want) | |
# Change constants as needed below. | |
import json | |
import os | |
import sys | |
# The names you want the playlists to be saved under; Pi does not seem to export names, only IDs. | |
PHONE_PLAYLIST_NAMES = [] | |
# The amount to subtract from the IDs of playlists in Pi when matching them to one of the names above - use the smallest "playlistId" value in PiPlaylistSong.m3upi | |
PHONE_PLAYLIST_ID_OFFSET = 2 | |
# The absolute path to the Music folder as seen by the applications on your phone. The default should work for most people. | |
PHONE_MUSIC_DIR = "/storage/emulated/0/Music" | |
PC_MUSIC_DIR = sys.argv[2] | |
with open(sys.argv[1], "rt") as raw_file: | |
pi_music_list = json.load(raw_file)["playlistSongList"] | |
print("Got playlist data") | |
playlists = {name: set() for name in PHONE_PLAYLIST_NAMES} | |
for song in pi_music_list: | |
target_playlist = PHONE_PLAYLIST_NAMES[song["playlistId"] - | |
PHONE_PLAYLIST_ID_OFFSET] | |
playlists[target_playlist] = playlists[target_playlist] | {( | |
song["songName"], | |
song["albumName"], | |
song["songDuration"] // 1000, | |
)} | |
for playlist_name, songs in playlists.items(): | |
print(f"\nExporting: {playlist_name}") | |
playlist_lines = [ | |
"#EXTM3U\n", | |
f"#PLAYLIST:{playlist_name}\n", | |
] | |
for song in songs: | |
found_file = False | |
for root, _, files in os.walk(PC_MUSIC_DIR): | |
for file in files: | |
if song[0] in file: | |
song_path = os.path.join( | |
PHONE_MUSIC_DIR, os.path.relpath(root, PC_MUSIC_DIR), | |
file) | |
found_file = True | |
if found_file: | |
playlist_lines.extend([ | |
"\n", | |
f"#EXTINF:{song[2]}, {song[0]}\n", | |
f"#EXTALB:{song[1]}\n", | |
f"{song_path}\n", | |
]) | |
else: | |
print(f"Couldn't find {song[0]}") | |
with open(f"{playlist_name}.m3u8", "wt") as playlist_file: | |
playlist_file.writelines(playlist_lines) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment