我是靠谱客的博主 大胆铅笔,最近开发中收集的这篇文章主要介绍使用python讲二进制转换为音频文件,Python使用Pydub将mp3转换为wav,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Ok, now I am stuck up in converting mp3 to wav. I have seen different answers but i think i would to go for the one of pydub, which i already did using these few lines

from pydub import AudioSegment

AudioSegment.from_mp3("/input/file.mp3").export("/output/file.wav", format="wav")

but when I run the above code, i get the following error

C:Python27libsite-packagespydub-0.14.2-py2.7.eggpydubutils.py:165: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work

Traceback (most recent call last):

File "C:/Users/phourlhar/Desktop/VoiceDetector/yeah.py", line 7, in

stereo_to_mono()

File "C:UsersphourlharDesktopVoiceDetectorutils.py", line 25, in

stereo_to_mono

sound = AudioSegment.from_mp3(PROJECT_DIR+'\files\rec'+str(c)+'.mp3')

File "buildbdist.win32eggpydubaudio_segment.py", line 346, in

from_file

File "C:Python27libsubprocess.py", line 711, in init

errread, errwrite)

File "C:Python27libsubprocess.py", line 948, in _execute_child

startupinfo)

WindowsError: [Error 2] The system cannot find the file specified

I don't know why it raises this error as i am very sure the file exists. Although i have answers suggesting the installation of ffmpeg, but i dont know if affect the app deployment in any way later on

解决方案

The pydub module uses either ffmpeg or avconf programs to do the actual conversion. So you do have to install ffmpeg to make this work.

But if you don't need pydub for anything else, you can just use the built-in subprocess module to call a convertor program like ffmpeg like this:

import subprocess

subprocess.call(['ffmpeg', '-i', '/input/file.mp3',

'/output/file.wav'])

This requires that the ffmpeg binary is in a location in your $PATH, by the way.

Edit: With ffmeg, you cannot convert stereo to mono, as far as I know. You can only choose the left or right channel. I'm assuming this is not what you want.

The sox program can convert stereo to mono:

import subprocess

subprocess.call(['sox', '/input/file.mp3', '-e', 'mu-law',

'-r', '16k', '/output/file.wav', 'remix', '1,2'])

This will sample at 16 kHz, with 8 bits/sample, giving you 16 kb/s.

最后

以上就是大胆铅笔为你收集整理的使用python讲二进制转换为音频文件,Python使用Pydub将mp3转换为wav的全部内容,希望文章能够帮你解决使用python讲二进制转换为音频文件,Python使用Pydub将mp3转换为wav所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(75)

评论列表共有 0 条评论

立即
投稿
返回
顶部