python音频格式转换:将MP3格式转换为wav格式

https://www.cnblogs.com/lotusto/p/5812684.html
合并、调节音量

将MP3格式转换为wav格式

https://www.ffmpeg.org/download.html
https://www.gyan.dev/ffmpeg/builds/
https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-full.7z
下载ffmpeg,解压得到

image.png

image.png

把bin文件夹中的三个文件与待执行的python文件放到同级别目录之后,(将bin文件夹加入到环境变量的path应该是不起作用的),python:

from pydub import AudioSegment
MP3_File = AudioSegment.from_file(file=dir+'music1.mp3', format='mp3')
# 等价于MP3_File = AudioSegment.from_mp3(file=dir+'music1.mp3')
MP3_File.export(dir+'music1.wav', format="wav")

下方代码也可

import wave
import io
import pydub
# pydub.AudioSegment.converter = 'D:\\ffmpeg-4.3.1-2020-11-19-full_build\\bin\\ffmpeg.exe'
def mp3_to_wav(mp3_path, wav_path):
    with open(mp3_path, 'rb') as fh:
        data = fh.read()

    aud = io.BytesIO(data)
    sound = pydub.AudioSegment.from_file(aud, format='mp3')
    raw_data = sound._data

    size = len(raw_data)
    f = wave.open(wav_path, 'wb')
    f.setnchannels(1)
    f.setsampwidth(2)
    f.setframerate(16000)
    f.setnframes(size)
    f.writeframes(raw_data)
    f.close()

    return wav_path
mp3_to_wav(dir+'music1.mp3', dir+'music1.wav')

也可命令行运行bin中exe
D:\ffmpeg-4.3.1-2020-11-19-full_build\bin\ffmpeg -i clock.mp3 -acodec pcm_s16le -ac 2 -ar 44100 clock.wav

参考:
ffmpeg官方文档https://ffmpeg.org/ffmpeg.html#Stream-specifiers-1
一些音频格式的转换https://blog.csdn.net/u012734708/article/details/78095135/

你可能感兴趣的:(python音频格式转换:将MP3格式转换为wav格式)