今天继续探讨一下利用Python剪辑音频,首先来看最简单的实例代码:
from pydub import AudioSegment
song=AudioSegment.from_mp3("XC14295.mp3")
song[25*1000:68*1000].export('short.mp3')
当然这运行之前需要首先安装pydub软件包,具体终端命令如下:
pip install pydub
安装完成之后,运行程序,结果大失所望,报错退出了。。。。。。。
D:\Python36\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)
D:\Python36\lib\site-packages\pydub\utils.py:198: RuntimeWarning: Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work
warn("Couldn't find ffprobe or avprobe - defaulting to ffprobe, but may not work", RuntimeWarning)
Traceback (most recent call last):
File "E:/dataMount/audio/audioCut.py", line 2, in
song=AudioSegment.from_mp3("XC14295.mp3")
File "D:\Python36\lib\site-packages\pydub\audio_segment.py", line 738, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)
File "D:\Python36\lib\site-packages\pydub\audio_segment.py", line 685, in from_file
info = mediainfo_json(orig_file, read_ahead_limit=read_ahead_limit)
File "D:\Python36\lib\site-packages\pydub\utils.py", line 274, in mediainfo_json
res = Popen(command, stdin=stdin_parameter, stdout=PIPE, stderr=PIPE)
File "D:\Python36\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "D:\Python36\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
根据系统类型选定版本之后,点击“Download Build”进行下载,解压至自行选定的位置,然后将路径(假设为d:/ffmpeg)添加至PATH环境变量中。
在windows7中具体设置方法为:右键点击“
我的电脑
”,选择“
属性
”,在弹出窗口中选择左侧的“
高级系统设置
”,然后新弹出窗口中选择“
环境变量
”,在新弹出窗口中的“
系统变量
”栏目选择"
Path
“之后点击”
编辑
“,在末尾首先添加英文分号,然后加入d:/ffmpeg/bin,点击
确定
按钮,即可设置完成。
重新运行该程序(注意需要重新打开一个cmd窗口),成功运行!
-------------------- 正文到此结束------------------------
推荐一个公众号:
健哥聊量化
,会持续推出股票相关基础知识,以及python实现的一些基本的分析代码。欢迎大家关注,二维码如下:
相关文章列表如下: