سؤال

I am attempting to edit certain MP3 files ID3 tags through Python. I have done some research, and found the mutagen, and eyeD3 libraries. However, I have run into similar issues with both of these libraries. Both implementations complain that the file I'm providing either does not exist, or is not an .mp3 file.

I have downloaded Mp3tag (http://www.mp3tag.de/en/download.html) and confirmed that the file I am providing is Tagged as ID3v2.3(ID3v1 ID3v2.3) and that is an .mp3 file.

Below is the mutagen code, followed by the error I receive:

from mutagen.mp3 import MP3

audio = MP3("C:\Users\557319\Music\Trance\Paul van Dyk - We Come Together (Arty Remix)")
print audio.info.length, audio.info.bitrate

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    audio = MP3("C:\Users\557319\Music\Trance\Paul van Dyk - We Come Together (Arty Remix)  www.freshnewtracks.com.mp3")
  File "C:\Python27\lib\site-packages\mutagen\__init__.py", line 75, in __init__
    self.load(filename, *args, **kwargs)
  File "C:\Python27\lib\site-packages\mutagen\id3.py", line 1995, in load
    try: self.tags = ID3(filename, **kwargs)
  File "C:\Python27\lib\site-packages\mutagen\id3.py", line 74, in __init__
super(ID3, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\mutagen\_util.py", line 105, in __init__
    super(DictProxy, self).__init__(*args, **kwargs)
  File "C:\Python27\lib\site-packages\mutagen\__init__.py", line 39, in __init__
    self.load(*args, **kwargs)
  File "C:\Python27\lib\site-packages\mutagen\id3.py", line 109, in load
    self.__fileobj = file(filename, 'rb')
IOError: [Errno 2] No such file or directory: 'C:\\Userso319\\Music\\Trance\\Paul van Dyk - We Come Together (Arty Remix) www.freshnewtracks.com.mp3'

Now when I try to view the ID3 data using eyeD3, I experience the following:

import eyeD3

trackInfo = eyeD3.Mp3AudioFile("C:\Users\557319\Music\Trance\Paul van Dyk - We Come Together (Arty Remix) www.freshnewtracks.com")

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
trackInfo = eyeD3.Mp3AudioFile("C:\Users\557319\Music\Trance\Paul van Dyk - We Come Together (Arty Remix) www.freshnewtracks.com")
  File "C:\Python27\lib\site-packages\eyeD3\tag.py", line 1618, in __init__
    raise InvalidAudioFormatException("File is not mp3");
InvalidAudioFormatException: File is not mp3

My guess is that I am not viewing the file properly? Do I need to use some type of os library to properly view these files ID3 data for Windows 7? Any help is greatly appreciated.

Thanks.

هل كانت مفيدة؟

المحلول

When you deal with Windows paths in Python, you have three options:

  • escape all backslashes with an additional one ("C:\\mydir\\myfile.mp3")
  • specify it as a "raw" string (r'C:\mydir\myfile.mp3' -- note the initial "r")
  • use forward-slashes. Some libs might not like it.

Whenever you manipulate paths, you should always use the functions in the os.path module, e.g. os.path.join(r'C:\mydir','myfile.mp3')

(as an aside, note that ID3 libs like mutagen are known for being buggy and temperamental, so expect breakages or weird behaviour; this is unfortunately due to ID3 being a poorly-specified non-standard full of corner cases and strange implementations.)

نصائح أخرى

My advice is to not even mess with the backslash. Python in Windows will accept a path just fine with forward slash.

Next, don't forget the .mp3 extension with the file. Mutagen/Python is very particular on the exact location of the file. If you're ever unsure as to what the filename or extension could be, feel free to check it by using the following code snippet in an interactive shell:

import os
os.listdir('C:/Userso319/Music/Trance/')

And this will show you the list of all files in that directory complete with ending extensions.

So, your new path becomes (if this is the actual name of the file):

"C:/Users/557319/Music/Trance/Paul van Dyk - We Come Together (Arty Remix) www.freshnewtracks.com.mp3"

It's a windows thing (though not really Windows fault). Add these lines to the top of your code and it will report the correct mime types.

import mimetypes
mimetypes.init([])

See http://bugs.python.org/issue10551 for more info

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top