在 Windows 7 中使用 Python 操作 .mp3 元数据
我正在尝试通过 Python 编辑某些 MP3 文件 ID3 标签。我做了一些研究,找到了诱变剂和 eyeD3 库。但是,我在这两个库中都遇到了类似的问题。两种实现都会抱怨我提供的文件不存在,或者不是 .mp3 文件。
我已经下载了 Mp3tag (http://www.mp3tag.de/en/download.html) 并确认我提供的文件被标记为 ID3v2.3(ID3v1 ID3v2.3) 并且这是一个 .mp3 文件。
下面是诱变代码,后面是我收到的错误:
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'
现在,当我尝试使用 eyeD3 查看 ID3 数据时,我遇到以下情况:
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
我的猜测是我没有正确查看文件?我是否需要使用某种类型的操作系统库才能正确查看 Windows 7 的这些文件 ID3 数据?非常感谢任何帮助。
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在 Python 中处理 Windows 路径时,您有以下三种选择:
"C:\\mydir\\myfile.mp3"
)r'C:\mydir\myfile.mp3'
- 注意开头的“r”)每当你操作路径时,你应该始终使用 os.path 模块中的函数,例如 os.path.join(r'C:\mydir','myfile.mp3')< /code>
(顺便说一句,请注意,像 mutagen 这样的 ID3 库因存在错误和喜怒无常而闻名,因此预计会出现损坏或奇怪的行为;不幸的是,这是由于 ID3 是一个指定不明确的非标准,充满了极端情况和奇怪的实现.)
When you deal with Windows paths in Python, you have three options:
"C:\\mydir\\myfile.mp3"
)r'C:\mydir\myfile.mp3'
-- note the initial "r")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.)
我的建议是甚至不要弄乱反斜杠。 Windows 中的 Python 可以接受带有正斜杠的路径。
接下来,不要忘记文件的 .mp3 扩展名。 Mutagen/Python 对于文件的确切位置非常有特殊要求。如果您不确定文件名或扩展名是什么,请随时在交互式 shell 中使用以下代码片段进行检查:
这将向您显示该目录中所有文件的列表以及结尾扩展名。
因此,您的新路径将变为(如果这是文件的实际名称):
"C:/Users/557319/Music/Trance/Paul van Dyk - We Come Together (Arty Remix) www.freshnewtracks.com。 mp3”
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:
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"
这是 Windows 的问题(虽然不是真正的 Windows 错误)。
将这些行添加到代码顶部,它将报告正确的 mime 类型。
有关详细信息,请参阅 http://bugs.python.org/issue10551
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.
See http://bugs.python.org/issue10551 for more info