繁体   English   中英

如何使用Python和Mutagen访问这些ID3值

[英]How do I access these ID3 values using Python and Mutagen

我正在尝试从我的大量卡拉OK集中读取ID3标签,以便可以将它们存储在CSV文件中,以便进行格式化和打印,但实际上在获取值方面存在问题! 如果我打印方法items() ,我设法以这种格式获取它们:

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

我正在使用的代码如下:

#! /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())

我尝试使用整数来访问它们,就好像它们是列表一样,也使用字符串来访问它,就好像它是字典一样,但是每次都会出错。 我在SE上的某处读到,函数items()返回“类似于字典的对象”,但是在google上搜索也没有帮助我!

我设法解决了这个问题,只花了几个小时,并有足够的CIG中断时间! 如果有人在开始使用Python编程时遇到此问题,这是我发现的解决方案:

#! /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")

事实证明,我试图获取的数据类型是多维Tupal,但我仍然不确定是否与列表之间的区别! 但是您似乎无法像使用其他语言的数组那样访问它们的值。 value[a][b] ,但在我看来,一些嵌套的for循环似乎可以解决问题! 在写入CSV文件时,我还对UTF编码带有重音的字符有一些问题,因此需要过滤器。

由于我发现唯一可以解决我所遇到的特定问题的软件(制作卡拉OK文件的歌曲列表)的价格在40到60英镑之间,或者免费软件的卡拉OK库限制非常低,因此我将致力于制作这种开源软件,具有基本的GUI,因此,我欢迎任何人想要添加的任何改进!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM