Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Unix only. Call with "./m3upi_to_m3u8.py [path to PiPlaylistSong.m3upi] [path to music directory on PC].
  4. # 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)
  5. # Change constants as needed below.
  6. import json
  7. import os
  8. import sys
  9.  
  10. # The names you want the playlists to be saved under; Pi does not seem to export names, only IDs.
  11. PHONE_PLAYLIST_NAMES = []
  12. # 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
  13. PHONE_PLAYLIST_ID_OFFSET = 2
  14. # The absolute path to the Music folder as seen by the applications on your phone. The default should work for most people.
  15. PHONE_MUSIC_DIR = "/storage/emulated/0/Music"
  16. PC_MUSIC_DIR = sys.argv[2]
  17.  
  18. with open(sys.argv[1], "rt") as raw_file:
  19. pi_music_list = json.load(raw_file)["playlistSongList"]
  20.  
  21. print("Got playlist data")
  22.  
  23. playlists = {name: set() for name in PHONE_PLAYLIST_NAMES}
  24.  
  25. for song in pi_music_list:
  26. target_playlist = PHONE_PLAYLIST_NAMES[song["playlistId"] -
  27. PHONE_PLAYLIST_ID_OFFSET]
  28.  
  29. playlists[target_playlist] = playlists[target_playlist] | {(
  30. song["songName"],
  31. song["albumName"],
  32. song["songDuration"] // 1000,
  33. )}
  34.  
  35. for playlist_name, songs in playlists.items():
  36. print(f"\nExporting: {playlist_name}")
  37.  
  38. playlist_lines = [
  39. "#EXTM3U\n",
  40. f"#PLAYLIST:{playlist_name}\n",
  41. ]
  42.  
  43. for song in songs:
  44. found_file = False
  45. for root, _, files in os.walk(PC_MUSIC_DIR):
  46. for file in files:
  47. if song[0] in file:
  48. song_path = os.path.join(
  49. PHONE_MUSIC_DIR, os.path.relpath(root, PC_MUSIC_DIR),
  50. file)
  51. found_file = True
  52.  
  53. if found_file:
  54. playlist_lines.extend([
  55. "\n",
  56. f"#EXTINF:{song[2]}, {song[0]}\n",
  57. f"#EXTALB:{song[1]}\n",
  58. f"{song_path}\n",
  59. ])
  60. else:
  61. print(f"Couldn't find {song[0]}")
  62.  
  63. with open(f"{playlist_name}.m3u8", "wt") as playlist_file:
  64. playlist_file.writelines(playlist_lines)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement