使用python部分下载提取MP3 URL的ID3标签

Ani*_*ish 7 python metadata id3 mutagen

我需要提取远程mp3文件的ID3标签和元数据.

我写了几行可以得到本地文件的ID3标签:

from mutagen.mp3 import MP3
import urllib2

audio = MP3("Whistle.mp3")

songtitle = audio["TIT2"]
artist = audio["TPE1"]

print "Title: " + str(songtitle)
print "Artist: "+str(artist)
Run Code Online (Sandbox Code Playgroud)

我需要为mp3文件的url链接实现这一点.我试图使用urllib2部分下载文件.

import urllib2
from mutagen.mp3 import MP3

req = urllib2.Request('http://www.1songday.com/wp-content/uploads/2013/08/Lorde-Royals.mp3')
req.headers['Range'] = 'bytes=%s-%s' % (0, 100)
response = urllib2.urlopen(req)
headers = response.info()
print headers.type
print headers.maintype

data = response.read()
print len(data)
Run Code Online (Sandbox Code Playgroud)

如何在不完全下载文件的情况下提取MP3网址的ID3标签?

Pie*_*eau 0

在您的示例中,未获取 ID3 标签,因此您无法提取它们。

在阅读了 ID3 的规范后,我玩了一下,这是一个很好的入门方法。

#Search for ID3v1 tags
import string
tagIndex = string.find(data,'TAG')
if (tagIndex>0):
  if data[tagIndex+3]=='+': 
    print "Found extended ID3v1 tag!"
    title = data[tagIndex+3:tagIndex+63]
    print title
  else:
    print  "Found ID3v1 tags"
    title = data[tagIndex+3:tagIndex+33]
    print title
    #So on.
else:
  #Look for ID3v2 tags
  if 'TCOM' in data:
    composerIndex = string.find(data,'TCOM')
    #and so on. See wikipedia for a full list of frame specifications
Run Code Online (Sandbox Code Playgroud)