为什么AudioSegment无法读取“ mp3”?

如何解决为什么AudioSegment无法读取“ mp3”?

我试图读取绝对路径给出的文件。 当我第一次运行代码时,我看到的是以下消息:

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')

解决方法

能否请您分享您正在使用的代码,这将有助于找到确切的问题。

if ($total_arr) { $classes = array(); foreach ($total_arr as $bb_ky => $bb_v) { $dayToCheck = date('l',strtotime($bb_v->openingtimed)); switch($dayToCheck) { case "Monday" : $classes[1]['monday'][] = $bb_v; break; case "Tuesday" : $classes[2]['tuesday'][] = $bb_v; break; case "Wednesday" : $classes[3]['wednesday'][] = $bb_v; break; case "Thursday" : $classes[4]['thursday'][] = $bb_v; break; case "Friday" : $classes[5]['friday'][] = $bb_v; break; case "Saturday" : $classes[6]['saturday'][] = $bb_v; break; } } if(!empty($classes)) { foreach($classes as $weekDay) { foreach($weekDay as $theDayName=>$class) { echo "<h2>".ucfirst($theDayName)."</h2>"; foreach($class as $row) { $dayName = date('l',strtotime($row->openingtimed)); $setStartD = date('m-d-Y',strtotime($row->openingtimed)); $setStartT = date('h.i a',$row->openingtime); $setEndT = date('h.i a',$row->closingtime); $chkStart = date('Y-m-d H:i',$row->openingtime); $chkEnd = date('Y-m-d H:i',$row->closingtime); ?> <div class="col-md-12"> <h5 class="subtitle"><?php echo $dayName . ' ' . $setStartD; ?></h5> </div> <div class="col-xl-3 col-sm-6 mb-3" > <div class="card dark-white bg-default o-hidden h-100 mt-3"> <div class="card-body pb-0"> <div class="class-status"><?php echo $status; ?></div> <div class="live-btn"><?php echo $livebtn; ?></div> <p class="live-class-left"><?php echo $row->name ?></p> <small><i class="far fa-calendar-alt"></i> <?php echo $setStartD; ?></small><br> <small><i class="far fa-clock"></i> <?php echo $setStartT; ?> to <?php echo $setEndT; ?></small> <img class="mr-1 float-right" src="<?php echo $CFG->wwwroot ?>/theme/iomadboost/images/mod_images/live-icon.png"> </div> </div> </div> <?php } } } } }

该错误还表明该文件被视为列表,而不是以下代码中的文件

AttributeError: 'list' object has no attribute 'read'

希望我给出了解决方案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-