Skip to content

Instantly share code, notes, and snippets.

@anupdhml
Created November 28, 2014 22:07
Show Gist options
  • Save anupdhml/a67c3f9ed2071304c44d to your computer and use it in GitHub Desktop.
Save anupdhml/a67c3f9ed2071304c44d to your computer and use it in GitHub Desktop.
Tag the files from gmusic app cache
#!/usr/bin/env python
# -*-coding:utf-8 -*
# mp3 files from gmusic app's cache don't have id3 tags assosciated with them.
# This assigns tags for them based on music.db found in the app dirs.
# Useful if you want to just copy mp3 from your phone and use them somewhere
# (instead of waiting to download them from the gmusic web (or desktop) app)
#
# Run it from the mp3 folder, and keep music.db in the same folder
#
# By <anupdhml@gmail.com>, October 2014
import glob, os
import sqlite3
from mutagen.easyid3 import EasyID3
from mutagen.id3 import ID3NoHeaderError
#from pprint import pprint
conn = sqlite3.connect('music.db')
c = conn.cursor()
#c.execute("SELECT name FROM sqlite_master WHERE type='table';")
#print(c.fetchall())
#pprint(EasyID3.valid_keys.keys())
for mp3 in glob.glob("./*.mp3"):
print "Working on", mp3, '...'
base = os.path.basename(mp3)
id = os.path.splitext(base)[0]
c.execute("SELECT Title, Album, Artist, AlbumArtist, Composer, Genre, Year, TrackCount, TrackNumber FROM MUSIC WHERE Id=" + id)
mp3tags = c.fetchone()
#pprint(mp3tags)
try:
audio = EasyID3(mp3)
except ID3NoHeaderError:
print "Adding id3 tag headers"
audio = EasyID3()
audio["title"] = mp3tags[0]
audio["album"] = mp3tags[1]
audio["artist"] = mp3tags[2]
audio["composer"] = mp3tags[4]
audio["genre"] = mp3tags[5]
audio["date"] = str(mp3tags[6])
audio["tracknumber"] = str(mp3tags[8])
audio.save(mp3)
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment