我想做的是从音频或视频文件中获取元数据并将其保存到数据库记录中,到目前为止,这样做的唯一方法似乎是使用子进程将 AVCONV 保存到文件中。打开调用然后读取该文件,是有没有图书馆可以做到这一点来节省一些步骤?我找不到使用 Pydub 或 PySox 的方法。这是我使用的简单粗暴的初学者代码,它确实有效,并将比特率、持续时间等信息放入变量 audio_info 并将元数据放入元数据。OGG 输出的工作方式与我测试的其他格式不同(这是大量的视频和音频!)。
try:
p = subprocess.Popen(["avconv" , "-i" , music_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
retcode = p.wait()
except IOError,e:
pass
extension = uploaded_music_file[-3:]
if "ogg" not in [err , extension]:
if "Metadata:" in err:
list = err.split("Metadata:")
holder = list[1].split("Duration:")
metadata = holder[0]
audio_info = holder[1].replace("At least one output file must be specified","")
print metadata
print audio_info
else:
list = err.split("Duration:")
audio_info = list[1].replace("At least one output file must be specified","")
print "No Metadata"
print audio_info
else:
list = err.split("Duration:")
if "Metadata:" in list[1]:
data = list[1].split("Metadata:")
metadata = data[1].replace("At least one output file must be specified","")
audio_info = data[0]
print metadata
print audio_info
else:
audio_info = list[1].replace("At least one output file must be specified","")
print "No Metadata"
print audio_info
if (audio_info):
print "AUDIO INFO:"
cursor.execute("UPDATE songDB SET audio_info = %s WHERE id = %s" ,[ audio_info , song_id ] )
if (metadata):
print "METADATA:"
cursor.execute("songDB pack_song SET metadata = %s WHERE id = %s" ,[ metadata , song_id ] )