python - 为什么 AudioSegment 不读取 'mp3' ?

标签 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 - 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'
而且我单独安装了ffmpegpip .但这没有帮助。
当我尝试这个时:
raw_sound = pydub.AudioSegment.from_mp3(file=track_path)
在哪里 track_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
我使用它时的完整代码:
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')

最佳答案

您能否分享您正在使用的代码,这将有助于找到确切的问题。AttributeError: 'list' object has no attribute 'read'此外,该错误表明文件被视为列表,而不是以下代码中的文件

 stdin_data = file.read()
希望我已经给出了解决方案。

关于python - 为什么 AudioSegment 不读取 'mp3' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64479843/

相关文章:

python - 条件 numpy 累计和

android - ffmpeg 中的 "No opaque field provided"是什么意思?

python - 在python中使用ffmpeg切割mp3 - 编解码器复制错误

video - FFMpeg 命令在 php 脚本中从 avi 转换为 mp4

python 如何改变代码的结尾

ffmpeg - 无法将 mp4 上传到 youtube

python - 使用 pydub 处理 24 位 wavs

python - 为什么不能在 reversed() 函数之后将它返回给 str() 内置函数?

python - 带有地理编码的 Twitter 搜索 api 几乎不会返回任何匹配项

python - 将霍夫曼编码字符串转换为二进制