How do I access these ID3 values using Python and Mutagen

人走茶凉 提交于 2019-12-25 02:03:15

问题


I'm trying to read the ID3 tags from my vast karaoke collection, so that I can store them in a CSV file, for easy formatting and printing, but I'm having issues actually getting the values! I manage to get them in a format like this, if I print the method items():

[('artist', [u'Steve Miller Band']), ('title', [u'Space Cowboy [Karaoke]'])] [('artist', [u'Stevie Wonder']), ('title', [u"Livin' For The City [Karaoke]"])]

The code I'm using is below:

#! /usr/bin/env python2.7
import os
import glob
import os.path
import csv

from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3


#form stagger.id3 import *

directory = os.path.normpath("C:/Users/Adrian/Music/Karaoke/Kar/*/*.cdg")

files = glob.glob(directory)
csvw = open('C:\\python\\karaoke.csv', 'wb')
info = []
for f in files:
    split = os.path.splitext(f)
    newpath = split[0] + ".mp3"
    if(os.path.isfile(newpath)):
        audio = MP3(newpath, ID3=EasyID3)
        stuff = audio.items()
        print stuff
        try:
            print>>csvw, (stuff[0] + ", " + stuff[1] + "\r\n")
        except:
            print>>csvw, (os.path.basename(newpath) + "\r\n")
        info.append(audio.items())

I have tried using integers to access them as if they were a list, and also strings as if it were a dictionary, but I get an error each time. I've read somewhere on SE that the function items() returns a "dictionary-like-object", but searching on google hasn't helped me either!


回答1:


I managed to fix this problem, with a few hours and more than enough cig breaks! If anyone come across this problem when starting to program with Python, this is the solution I found:

#! /usr/bin/env python2.7

import os
import glob
import os.path
import csv
import string

from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3


#form stagger.id3 import *

directory = os.path.normpath("C:/Users/Adrian/Music/Karaoke/Kar/*/*.cdg")

files = glob.glob(directory)
csvw = open('C:\\python\\karaoke.csv', 'wb')
info = []
for f in files:
    split = os.path.splitext(f)
    newpath = split[0] + ".mp3"
    if(os.path.isfile(newpath)):
        audio = MP3(newpath, ID3=EasyID3)
        stuff = audio.items()
        artist = ""
        titley = ""
        for stu in stuff:
            i = 0
            for s in stu:
                if (s == "artist"):
                    artist = filter(lambda x: x in string.printable, ''.join(stu[(i+1)]))
                    artist = artist.encode('ascii',errors='ignore')
                if (s == "title"):
                    titley = filter(lambda x: x in string.printable, ''.join(stu[(i+1)]))
                    titley = titley.encode('ascii',errors='ignore')

            i = i+1
            if(artist != "" and titley != ""):
                print>>csvw, (artist + ", " + titley + "\r\n")

It turns out that the data type I was trying to get was a multi-dimensional tupal, which I'm still no entirely sure what the difference between them and a list is! But you don't seem to be able to access their values as you would an array in other languages eg. value[a][b], but some nested for loops seem to have done the trick in my case! I also had some problems with UTF encoding characters with accents when writing to a CSV file, hence the need for the filters.

Since the only software I have found that solve the particular problem I had (Making song lists for karaoke files) cost between £40-60, or the free ones have stupidly low limits for a karaoke library, I will work on making this open source, with a basic GUI, SO I welcome any improvements anyone may like to add!



来源:https://stackoverflow.com/questions/22080486/how-do-i-access-these-id3-values-using-python-and-mutagen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!