0

I'm writing a script in Python 2.7 that needs to fetch the title, artist and preferably length (but that's less important), of linked MP3 files. I'm not really sure how to go about doing this, I've tried a few approaches with urllib and mutagen, but none have worked. Well, one worked, but for some reason stopped working. urllib started saying that there were too many values to unpack, I'm not sure why. Here is what used to work:

from urllib import urlopen
from mutagen.mp3 import MP3

def getInfo(url):
    filename, headers = urlopen(url)
    audio = MP3(filename)

That worked fine, and I'm not sure what changed, but I haven't found anything else that worked since. I may have been a bit more vague than I realize here, so please let me know if you need more information. Thank you!

2 Answers2

1

I think, you should download mp3 to aremporary folder. After that, you xan read its information. For example,

from urllib2 import Request, urlopen
from mutagen.mp3 import MP3

def getInfo(url):
    start_byte = 0
    end_byte = 5000
    url = Request(url)
    url.add_header('Range', 'bytes=' + str(start_byte) + '-' + str(end_byte))
    filename = urlopen(url)

    output = open("test_file.mp3",'wb')
    output.write(filename.read())
    output.close()
    audio = MP3("test_file.mp3")

    print audio.info.pprint()

But, this is not real solution. Because, i dont know anything about mp3 file structure, which bytes return id3 header. This is an example to how can implement it.

hkulekci
  • 1,894
  • 15
  • 27
  • I considered this, but it takes a long time to download it, and I need it to be pretty quick Do you know if there's a way to just download the ID3 tags? – JuniorGenius Sep 09 '14 at 05:56
  • Oh also, as I said; as of recently, line 5 doesn't work, it errors `ValueError: too many values to unpack` And when I change it to 1 value like this: `filename = urlopen(url)` Line 9 gives an error: `TypeError: coercing to Unicode: need string or buffer, instance found` – JuniorGenius Sep 09 '14 at 06:05
  • 1
    i fexed my error. Maybe you can read only mp3 file header with HTTP Range header. And you can parse it. – hkulekci Sep 09 '14 at 06:23
  • I'm confused. I still get this, even after your fix: `TypeError: coercing to Unicode: need string or buffer, instance found` – JuniorGenius Sep 09 '14 at 06:26
  • 1
    Oh sorry. I did not test, I was writing codes my pad. Now, i is working. – hkulekci Sep 09 '14 at 06:32
  • Awesome, thanks! Now is there any way to make it faster? Like maybe just downloading the ID3 somehow? If not, I'll take this :) – JuniorGenius Sep 09 '14 at 06:35
  • i edit my code and it is faster than older. Do you know which bytes return me mp3 id3 header? – hkulekci Sep 09 '14 at 06:47
  • Awesome, that is so much faster, thanks! And no I don't, but I'll do some research – JuniorGenius Sep 09 '14 at 06:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/60866/discussion-between-juniorgenius-and-hkulekci). – JuniorGenius Sep 09 '14 at 07:03
1

If you are using Python 3 you will need to use mutagenx instead of mutagen. See here

import mutagenx
import mutagenx.id3
audio = mutagenx.id3.ID3(filename, translate=False)
print(audio["title"], audio["artist"], audio["length"])
acutesoftware
  • 1,091
  • 3
  • 14
  • 33