我试图在mp3文件中嵌入中文名称(标题,艺术家,年份,流派和专辑)。标题名称将取自CSV文件(包含中文数据)。
代码正在运行但是当我查看mp3文件的详细信息时,每件事都显示在'?????'只有当数据是中文时才会发生这种情况。但是当数据是英文的时候。它的工作正常。我不知道我的代码中出了什么问题。
请找到以下代码。
from argparse import ArgumentParser
from mutagen.easyid3 import EasyID3
from datetime import datetime
from string import Template
import os
import sys
import datetime
import codecs
import string
import shutil
import re
import logging
import io
import eyed3
import id3
import csv
# other
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
save_stdout = sys.stdout # save for restore
# default input filenames
filename_mp3_csv = "mp3_ArtTitAlbGenYer.csv"
outfile = open ('mp3_cop.cop', 'wb')
def get_args():
"""parse command line arguments"""
parser = ArgumentParser()
parser.add_argument("-t", "--mp3template", help="filename of mp3 file to be used as template",default="template.mp3")
parser.add_argument("-l", "--language", help="name of language folder e.g. enu", default="enu")
parser.add_argument("-ic", "--mp3_scv", help="filename of csv file to be used as input", default="mp3_ArtTitAlbGenYer.csv")
args = parser.parse_args()
return args
########################################################################################################################
# main program
def something():
if __name__ == '__main__':
args = get_args()
i=0
while os.path.exists("%s.mp3" % i):
i+=1
fh = open("%s.mp3" % i, "w")
shutil.copy2(args.mp3template, "%s.mp3" %i)
audio = EasyID3("%s.mp3" %i)
audio["title"] =Title
audio["artist"] = Artist
audio["album"] = Album
audio["date"] = Year
audio["genre"] = Genre
audio["tracknumber"] = u"1"
audio.save()
return
# read mp3_csv utf-8-sig ignores BOM \xef\xbb\xbf at begin of file
try:
file = codecs.open(filename_mp3_csv, "r",encoding='utf-8-sig')
for line in file:
# print(' \n '.join(line))
#print(len(line.rstrip().encode('utf8')))
item = line.split(';')
Artist = item[0].rstrip()
Title = item[1].rstrip()
Album = item[2].rstrip()
Genre = item[3].rstrip()
Year = item[4].rstrip()
print(line.rstrip(';'))
something()
file.close()
except IOError as err: # e.g. file not existing
mustwritesomethinghere = 0
print >> sys.stderr, "error: unable to read mandatory inputfile " + filename_mp3_csv