Created
November 2, 2013 12:02
-
-
Save carelvwyk/7278213 to your computer and use it in GitHub Desktop.
Python script to export tagged albums purchased in iTunes on an iPhone to PC. The script will rename purchased tracks from the strange 4837260613109701064.m4a format to human-readable filenames in the form: album artist/album title (year)/trackno - artist - track title.m4a (e.g. 01 - Rammstein - Sonne.m4a). It will also embed the cover art and a…
This file contains 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
OUT_DIR = './out' | |
GROUP_KEY = 'playlistName' # playlistName -> Album Title | |
# get list of plist files: | |
import os | |
import glob | |
import shutil | |
import biplist | |
import mutagen | |
from mutagen import mp4 | |
albums = dict() # Grouping files by album title | |
for plist_file in glob.glob("*.plist"): | |
plist_data = biplist.readPlist(plist_file) | |
if not plist_data.has_key(GROUP_KEY): | |
continue | |
key = plist_data[GROUP_KEY] | |
albums.setdefault(key, []).append(plist_data) | |
# Create output directory | |
if not os.path.exists(OUT_DIR): | |
os.makedirs(OUT_DIR) | |
def export_track(plist_data): | |
# Relevant info: | |
# - Files | |
m4a_filename = plist_data['com.apple.iTunesStore.downloadInfo']['mediaAssetFilename'] | |
art_filename = plist_data['com.apple.iTunesStore.downloadInfo']['artworkAssetFilename'] | |
# - Tags | |
release_date = plist_data['releaseDate'] | |
album_title = plist_data['playlistName'] | |
album_artist = plist_data['playlistArtistName'] | |
album_year = plist_data['year'] | |
copyright = plist_data.get('copyright','') | |
track_title = plist_data['itemName'] | |
track_artist = plist_data['artistName'] | |
track_comment = plist_data.get('comments','') | |
track_number = plist_data['trackNumber'] | |
track_count = plist_data['trackCount'] | |
track_genre = plist_data['genre'] | |
disc_number = plist_data['discNumber'] | |
disc_count = plist_data['discCount'] | |
# Copy the audio track to output directory | |
album_directory = "%s/%s/%s (%s)" % (OUT_DIR, album_artist, album_title, album_year) | |
if not os.path.exists(album_directory): | |
os.makedirs(album_directory) | |
new_file = album_directory+"/%02d - %s - %s.m4a" % (track_number, track_artist, track_title) # eg. 01 - Rammstein - Sonne | |
shutil.copyfile(m4a_filename, new_file) | |
# Add MP4 tags: | |
audio_file = mp4.MP4(new_file) | |
# Embed album art if it exists | |
if os.path.isfile(art_filename): | |
image_data = None | |
with open(art_filename, 'rb') as f: | |
image_data = f.read() | |
if image_data != None: | |
image_format = mp4.MP4Cover.FORMAT_PNG if art_filename.endswith('png') else mp4.MP4Cover.FORMAT_JPEG | |
covr = [mp4.MP4Cover(image_data, image_format)] # Must be array, can have multiple elements | |
audio_file.tags['covr'] = covr | |
# Set other tags | |
# track | |
audio_file.tags['\xa9nam'] = track_title | |
audio_file.tags['\xa9ART'] = track_artist | |
audio_file.tags['\xa9alb'] = album_title | |
audio_file.tags['aART'] = album_artist | |
audio_file.tags['\xa9day'] = release_date | |
audio_file.tags['\xa9cmt'] = track_comment | |
audio_file.tags['\xa9gen'] = track_genre | |
audio_file.tags['cprt'] = copyright | |
audio_file.tags['trkn'] = [(track_number, track_count)] | |
if disc_count > 1: | |
audio_file.tags['disk'] = [(disc_number, disc_count)] | |
audio_file.save() | |
for album in albums.keys(): | |
for track in albums[album]: | |
export_track(track) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank You for this awesome script! Have you any idea, that how can I implement the new iTunes plist file format in this script? Because the "mediaAssetFilename" key does not exists.
https://www.dropbox.com/s/jl6994deo29xq5k/1749832100114107262.text.plist?dl=0