Skip to content

Instantly share code, notes, and snippets.

@hopbit
Last active March 8, 2016 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopbit/9196828 to your computer and use it in GitHub Desktop.
Save hopbit/9196828 to your computer and use it in GitHub Desktop.
simple python script that converts mp3 files imported with itunes in "my way"
# coding=UTF-8
import os, shutil
# needs python2.7
# works only with albums that are NOT marked as compilations
# --------------------------------------------------------------
# usage:
# cd %itunes_library%\Music # cd $itunes_library/Music
# python _convert_mp3_filenames_imported_with_itunes.py
cwd = os.getcwd() # current directory # np. ~/path/to/itunes/Music/.
for artistDir in os.listdir('.'): # list all content of current directory # e.g. "./DJ Tonka ft. Lara Mc Allen"
if os.path.isdir(artistDir) == True: # only if current object is directory
artistPath = os.path.join(cwd, artistDir) # prepara path to artist directory, e.g. "~/DJ/RIPPED/Music/DJ Tonka ft. Lara Mc Allen/."
os.chdir(artistPath) # cd "DJ Tonka ft. Lara Mc Allen"
for albumDir in os.listdir('.'): # for each album e.g. "DMC November 2004 CD4"
albumPath = os.path.join(artistPath,albumDir) # ".../DJ Tonka ft. Lara Mc Allen/DMC November 2004 CD4"
os.chdir(albumPath) # get into "DMC November 2004 CD4" || "DMC March 2001 CD2"
for fileName in os.listdir('.'): # for each mp3 song in this directory # e.g. "11 Get Back (RMX).mp3" || "2-08 All I Do (Tonka Remix).mp3"
if os.path.isdir(fileName)==False: # only for objects that are NOT a directory (mp3 files in this case)
cdNumber = "" # settle number of cd as empty string (I assume that there's only one cd)
titleStartMarker = 3 # split marker # e.g. 11^Get Back (RMX).mp3 or "2-08^All I Do (Tonka Remix).mp3"
if "-" in fileName: # if name contains dash, then I assume that we're working with multiple CD album
cdNumber = "CD" + fileName[:1] # so I must prepare directory for current CD, I get it from first char of filename
titleStartMarker = 5 # and (of coz) the marker of title start has different position
newAlbumPath = os.path.join(cwd,albumDir,cdNumber) # now when i have above 2 informations I can glue proper path for new CD directory # e.g. "~/DJ/RIPPED/Music/DMC November 2004 CD4/" or "~/DJ/RIPPED/Music/DMC November 2004/CD2/"
if not os.path.exists(newAlbumPath): # if such directory not exist yet
os.makedirs(newAlbumPath) # create it
newFileName = artistDir + " - " + fileName[titleStartMarker:len(fileName)] # prepare new mp3 file name # e.g. "DJ Tonka - Get Back (RMX).mp3"
newFileName = newFileName.lower() # make mp3 file name lower case in order to be easier to find on filesystem with Win+F on Windows or Cmd+Space on Mac
os.rename(fileName,newFileName) # change file name to new one
newFilePath = os.path.join(newAlbumPath,newFileName) # prepare new path for mp3 file
print newFilePath # show new mp3 file name with it's path
shutil.move(newFileName,newFilePath) # move changed mp3 file to new directory
os.chdir(cwd) # ok, let's go back to start directory # e.g. "~/DJ/RIPPED/".
shutil.rmtree(artistDir) # usun niepotrzebny mi juz katalog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment