使用诱变剂获取文件夹中 mp3 的长度
get lenght of mp3 in a folder using mutagen
我编写了这段代码来获取文件夹中所有 mp3 文件的持续时间,但它给我一个错误:
import os
from mutagen.mp3 import MP3
path = "D:/FILE/P. F. Ford - A Body on the Beach/"
filenames = next(os.walk(path, topdown=True))
for filename in filenames:
audio = MP3(filename)
print(audio.info.length)
错误:
Traceback (most recent call last):
File "C:\Desktop\.venv\lib\site-packages\mutagen\_util.py", line 251, in _openfile
fileobj = open(filename, "rb+" if writable else "rb")
PermissionError: [Errno 13] Permission denied: 'D:/FILE/P. F. Ford - A Body on the Beach/'
我有所有的权限,你能告诉我正确的路径吗?
当您从代码行
中获取文件名时,由于文件名错误而发生错误
filenames = next(os.walk(path, topdown=True))
然后您将获得该文件夹中存在的所有类型(扩展名)的文件。
所以,你只需要过滤这些文件*.mp3个文件,然后重新运行你的代码
我已经通过我改编的这段代码解决了我的问题:
import os
from mutagen.mp3 import MP3
path = "D:/FILE/P. F. Ford - A Body on the Beach"
def convert(seconds):
hours = seconds // 3600
seconds %= 3600
mins = seconds // 60
seconds %= 60
return(hours, mins, seconds)
for root, dirs, files in os.walk(os.path.abspath(path)):
for file in files:
if file.endswith(".mp3"):
print(os.path.join(root, file))
audio = MP3(os.path.join(root, file))
# print(audio.info.length)
hours, mins, seconds = convert(audio.info.length)
print(str(int(hours)) + ":" +
str(int(mins)) + ":" + str(int(seconds)))
我编写了这段代码来获取文件夹中所有 mp3 文件的持续时间,但它给我一个错误:
import os
from mutagen.mp3 import MP3
path = "D:/FILE/P. F. Ford - A Body on the Beach/"
filenames = next(os.walk(path, topdown=True))
for filename in filenames:
audio = MP3(filename)
print(audio.info.length)
错误:
Traceback (most recent call last):
File "C:\Desktop\.venv\lib\site-packages\mutagen\_util.py", line 251, in _openfile
fileobj = open(filename, "rb+" if writable else "rb")
PermissionError: [Errno 13] Permission denied: 'D:/FILE/P. F. Ford - A Body on the Beach/'
我有所有的权限,你能告诉我正确的路径吗?
当您从代码行
中获取文件名时,由于文件名错误而发生错误filenames = next(os.walk(path, topdown=True))
然后您将获得该文件夹中存在的所有类型(扩展名)的文件。 所以,你只需要过滤这些文件*.mp3个文件,然后重新运行你的代码
我已经通过我改编的这段代码解决了我的问题:
import os
from mutagen.mp3 import MP3
path = "D:/FILE/P. F. Ford - A Body on the Beach"
def convert(seconds):
hours = seconds // 3600
seconds %= 3600
mins = seconds // 60
seconds %= 60
return(hours, mins, seconds)
for root, dirs, files in os.walk(os.path.abspath(path)):
for file in files:
if file.endswith(".mp3"):
print(os.path.join(root, file))
audio = MP3(os.path.join(root, file))
# print(audio.info.length)
hours, mins, seconds = convert(audio.info.length)
print(str(int(hours)) + ":" +
str(int(mins)) + ":" + str(int(seconds)))