在 Windows 7 中使用 Python 操作 .mp3 元数据

发布于 2024-12-25 16:04:05 字数 2235 浏览 5 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧梦荧光笔 2025-01-01 16:04:05

当您在 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:

  • 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.)

∞觅青森が 2025-01-01 16:04:05

我的建议是甚至不要弄乱反斜杠。 Windows 中的 Python 可以接受带有正斜杠的路径。

接下来,不要忘记文件的 .mp3 扩展名。 Mutagen/Python 对于文件的确切位置非常有特殊要求。如果您不确定文件名或扩展名是什么,请随时在交互式 shell 中使用以下代码片段进行检查:

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

这将向您显示该目录中所有文件的列表以及结尾扩展名。

因此,您的新路径将变为(如果这是文件的实际名称):

"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:

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"

雪落纷纷 2025-01-01 16:04:05

这是 Windows 的问题(虽然不是真正的 Windows 错误)。
将这些行添加到代码顶部,它将报告正确的 mime 类型。

import mimetypes
mimetypes.init([])

有关详细信息,请参阅 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.

import mimetypes
mimetypes.init([])

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文