Python 为什么AudioSegment没有';不读';mp3';?
我试图读取我给出的具有绝对路径的文件。 当我第一次运行代码时,我看到以下消息:Python 为什么AudioSegment没有';不读';mp3';?,python,ffmpeg,pydub,audiosegment,Python,Ffmpeg,Pydub,Audiosegment,我试图读取我给出的具有绝对路径的文件。 当我第一次运行代码时,我看到以下消息: D:\prog\datascience\anaconda\lib\site-packages\pydub\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work warn("Couldn't find ffmpeg or avconv - defaultin
D:\prog\datascience\anaconda\lib\site-packages\pydub\utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
我试过这个:
PATH_TO_FFMPEG = 'D:\\prog\\ffmpeg-win-2.2.2\\ffmpeg.exe'
pydub.AudioSegment.converter = r'D:\\prog\\ffmpeg-win-2.2.2\\ffmpeg.exe'
我用pip
分别安装了ffmpeg
。但这没用。
当我尝试这个:
raw_sound = pydub.AudioSegment.from_mp3(file=track_path)
其中,track\u path
是自动生成的正确绝对路径。
所以我得到了这个错误:
Traceback (most recent call last):
File "D:\prog\PyCharm Community Edition 2020.2.2\plugins\python-ce\helpers\pydev\pydevd.py", line 1448, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "D:\prog\PyCharm Community Edition 2020.2.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/testtask2/test_task/testtask/get_mffc.py", line 165, in <module>
slice_all_in_a_dir('May 27 2020 LNC/Hydrophone 1/raw_records')
File "D:/testtask2/test_task/testtask/get_mffc.py", line 70, in slice_all_in_a_dir
slice_samples(track_path= [file],
File "D:/testtask2/test_task/testtask/get_mffc.py", line 48, in slice_samples
raw_sound = pydub.AudioSegment.from_mp3(file=track_path)
File "D:\prog\datascience\anaconda\lib\site-packages\pydub\audio_segment.py", line 738, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "D:\prog\datascience\anaconda\lib\site-packages\pydub\audio_segment.py", line 680, in from_file
stdin_data = file.read()
AttributeError: 'list' object has no attribute 'read'
python-BaseException
回溯(最近一次呼叫最后一次):
文件“D:\prog\PyCharm Community Edition 2020.2.2\plugins\python ce\helpers\pydev\pydevd.py”,第1448行,在_exec中
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“D:\prog\PyCharm Community Edition 2020.2.2\plugins\python ce\helpers\pydev\\u pydev\u imps\\u pydev\u execfile.py”,execfile中第18行
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“D:/testtask2/test_task/testtask/get_mffc.py”,第165行,在
在目录中切片所有记录(“2020年5月27日LNC/水听器1/原始记录”)
文件“D:/testtask2/test_task/testtask/get_mffc.py”,第70行,在_a_dir的slice_all_中
切片样本(轨迹路径=[文件],
文件“D:/testtask2/test_task/testtask/get_mffc.py”,第48行,在切片样本中
原始声音=pydub.AudioSegment.from\u mp3(文件=曲目路径)
文件“D:\prog\datascience\anaconda\lib\site packages\pydub\audio\u segment.py”,第738行,from\u mp3
从_文件(文件'mp3',参数=参数)返回cls
文件“D:\prog\datascience\anaconda\lib\site packages\pydub\audio\u segment.py”,第680行,在from\u文件中
stdin_data=file.read()
AttributeError:“list”对象没有属性“read”
python BaseException
使用时的完整代码:
def slice_samples(track_path: list, save_path: str,
sample_folder_name: str, interval: float, given_format, name: str = "part", export_format = 'wav'):
"""
This metod slice given track to parts.
:param track_path: str, a path to the track you want to slice
:param save_path: str, a path to folder, where you want save sliced tracks
:param sample_folder_name: str, you don't need to create a folder for sliced tracks,
you can just write the name of the folder in this argument where you want to save tracks
:param interval: float, measure in seconds, the length of sliced tracks
:param name: str, name of sliced tacks
:param given_format: str, I strongly recommend use .wav format initially, when you record sounds
:return: folder with sliced tracks
"""
# it cuts a file in mp3 or wav formats (wav recommended)
interval_secs = interval * 10 ** 3
raw_sound = None
if given_format == "WAV":
raw_sound = pydub.AudioSegment.from_wav(file=track_path)
elif given_format == "MP3":
raw_sound = pydub.AudioSegment.from_mp3(file=track_path)
else:
raise Exception("It's temporarily unsupported given_format: " + given_format)
start = 0
end = interval_secs
i = 0
while end < len(raw_sound):
save_to = save_path + sample_folder_name + "/" + name + str(i)
part = raw_sound[start:end]
part.export(save_to, format=export_format)
i += 1
start += interval_secs
end += interval_secs
return save_path + sample_folder_name
def slice_all_in_a_dir(tracks_folder: str):
files = os.listdir(tracks_folder)
for file in files:
folder_name = file.split('.')
f_name = folder_name[0]
file = tracks_folder+'/'+file
file = os.path.abspath(file)
slice_samples(track_path= [file],
save_path= PATH_FOR_SLICED,
sample_folder_name= f_name,
interval=5,
given_format=folder_name[1])
if __name__ == "__main__":
slice_all_in_a_dir('May 27 2020 LNC/Hydrophone 1/raw_records')
def slice_samples(跟踪路径:list,保存路径:str,
示例文件夹名称:str,间隔:float,给定格式,名称:str=“part”,导出格式='wav'):
"""
这是一个给定零件轨迹的metod切片。
:param track_path:str,指向要切片的轨迹的路径
:param save_path:str,文件夹的路径,用于保存切片轨迹
:param sample_folder_name:str,您不需要为切片轨迹创建文件夹,
您只需在此参数中写入要保存曲目的文件夹的名称
:param interval:float,以秒为单位测量切片轨迹的长度
:参数名称:str,切片大头钉的名称
:param given_format:str,我强烈建议在录制声音时最初使用.wav格式
:return:带有切片轨迹的文件夹
"""
#它以mp3或wav格式剪切文件(建议使用wav)
间隔时间=间隔时间*10**3
原始声音=无
如果给定_格式==“WAV”:
原始声音=pydub.AudioSegment.from_wav(文件=音轨路径)
elif给定格式==“MP3”:
原始声音=pydub.AudioSegment.from\u mp3(文件=曲目路径)
其他:
引发异常(“给定格式暂时不支持:+给定格式”)
开始=0
结束=间隔_秒
i=0
当end
能否请您分享您正在使用的代码,这将有助于找到确切的问题
AttributeError:“list”对象没有属性“read”
该错误还表明,在下面的代码中,文件被视为列表,而不是文件
stdin_data = file.read()
希望我已经给出了一个解决方案。是的,我将在问题中分享,因为有很多代码。是的,我理解,我确实给出了一个列表。您帮助解决了这个问题,谢谢。