Python实现批量将MP3音频转为WAV格式详解

目录
  • 前言
  • 环境依赖
  • 代码
  • 验证一下

前言

本文提供批量将MP3格式的音频转为wav格式的工具代码,一如既往的实用主义。

环境依赖

ffmpeg环境安装,可以参考:windows ffmpeg安装部署

ffmpy安装:

pip install ffmpy -i https://pypi.douban.com/simple

代码

不废话,上代码。

#!/user/bin/env python
# coding=utf-8
"""
@project : batch-pro
@author  : 剑客阿良_ALiang
@file   : audio_tool.py
@ide    : PyCharm
@time   : 2021-12-21 14:48:18
"""
from ffmpy import FFmpeg

import os

# MP3转wav
def audio_transfor(audio_path: str, output_dir: str):
    ext = os.path.basename(audio_path).strip().split('.')[-1]
    if ext != 'mp3':
        raise Exception('format is not mp3')

    result = os.path.join(output_dir, '{}.{}'.format(os.path.basename(audio_path).strip().split('.')[0], 'wav'))
    filter_cmd = '-f wav -ac 1 -ar 16000'
    ff = FFmpeg(
        inputs={
            audio_path: None}, outputs={
            result: filter_cmd})
    print(ff.cmd)
    ff.run()
    return result

def handle(audio_dir: str, output_dir: str):
    for x in os.listdir(audio_dir):
        audio_transfor(os.path.join(audio_dir, x), output_dir)

代码说明:

1、handle方法主要参数为:音频目录、输出目录。

2、audio_transfor方法主要参数为:音频文件地址、输出目录。

3、主要通过handle方法批量对MP3音频目录进行遍历处理。

4、会对音频文件的后缀进行判断,看是否为MP3。

验证一下

准备了10个MP3文件。

执行代码:

if __name__ == '__main__':
    handle('C:/Users/xx/Desktop/test1', 'C:/Users/xx/Desktop/test2')

执行过程

C:\Users\huyi\.conda\envs\ai_detection\python.exe D:/spyder/batch-pro/audio_tool.py
ffmpeg -i C:/Users/huyi/Desktop/test1\1.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\1.wav
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 000001ad059c4640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\1.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:08:20.95, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\1.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   15655kB time=00:08:20.95 bitrate= 256.0kbits/s speed= 896x
video:0kB audio:15655kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000487%
ffmpeg -i C:/Users/huyi/Desktop/test1\10.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\10.wav
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0000021a839a4640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\10.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:12:50.25, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\10.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   24070kB time=00:12:50.24 bitrate= 256.0kbits/s speed= 862x
video:0kB audio:24070kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000316%
ffmpeg -i C:/Users/huyi/Desktop/test1\2.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\2.wav
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 000002900b784640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\2.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:09:36.24, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\2.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   18007kB time=00:09:36.23 bitrate= 256.0kbits/s speed= 879x
ffmpeg -i C:/Users/huyi/Desktop/test1\3.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\3.wav
video:0kB audio:18007kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000423%
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 00000238ce244640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\3.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:07:00.73, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\3.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   13148kB time=00:07:00.72 bitrate= 256.0kbits/s speed= 886x
video:0kB audio:13148kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000579%
ffmpeg -i C:/Users/huyi/Desktop/test1\4.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\4.wav
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0000024ada3a4640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\4.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:08:14.58, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\4.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   15456kB time=00:08:14.57 bitrate= 256.0kbits/s speed= 873x
video:0kB audio:15456kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000493%
ffmpeg -i C:/Users/huyi/Desktop/test1\5.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\5.wav
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0000023e42064640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\5.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:07:08.67, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\5.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   13396kB time=00:07:08.66 bitrate= 256.0kbits/s speed= 850x
video:0kB audio:13396kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000569%
ffmpeg -i C:/Users/huyi/Desktop/test1\6.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\6.wav
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0000021c487e4640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\6.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:06:22.93, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\6.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   11967kB time=00:06:22.92 bitrate= 256.0kbits/s speed= 883x
video:0kB audio:11967kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000637%
ffmpeg -i C:/Users/huyi/Desktop/test1\7.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\7.wav
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 000001bd2d4e4640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\7.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:08:09.64, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\7.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   15301kB time=00:08:09.63 bitrate= 256.0kbits/s speed= 837x
ffmpeg -i C:/Users/huyi/Desktop/test1\8.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\8.wav
video:0kB audio:15301kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000498%
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0000026043684640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\8.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:08:05.04, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\8.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   15158kB time=00:08:05.04 bitrate= 256.0kbits/s speed= 614x
video:0kB audio:15158kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000503%
ffmpeg -i C:/Users/huyi/Desktop/test1\9.mp3 -f wav -ac 1 -ar 16000 C:/Users/huyi/Desktop/test2\9.wav
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 9.3-win32 (GCC) 20200320
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --disable-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-libbluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --enable-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[mp3 @ 0000016a678d4640] Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'C:/Users/huyi/Desktop/test1\9.mp3':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    encoder         : Lavf57.71.100
  Duration: 00:08:15.12, start: 0.000000, bitrate: 64 kb/s
    Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 64 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (mp3 (mp3float) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'C:/Users/huyi/Desktop/test2\9.wav':
  Metadata:
    major_brand     : M4A
    minor_version   : 1
    compatible_brands: isomiso2M4A mp42
    ISFT            : Lavf58.45.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
    Metadata:
      encoder         : Lavc58.91.100 pcm_s16le
size=   15473kB time=00:08:15.12 bitrate= 256.0kbits/s speed= 680x
video:0kB audio:15473kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000492%

Process finished with exit code 0

结果目录:

OK,没什么问题。 

到此这篇关于Python实现批量将MP3音频转为WAV格式详解的文章就介绍到这了,更多相关Python MP3音频转为WAV格式内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • python调用pyaudio使用麦克风录制wav声音文件的教程

    python的pyaudio可以进行录音,播放,生成wav文件等等,WAVE是录音时用的标准的WINDOWS文件格式,文件的扩展名为WAV,数据本身的格式为PCM或压缩型,属于无损音乐格式的一种.在我们研究语音识别,自然语言处理的过程中,常常会使用到它,比如我们调用百度语音识别所以我们首先研究一下pyaudio库的安装与使用. 安装: pip install pyaudio 调用pyaudio使用麦克风录制声音: python中的pyaudio库可以直接通过麦克风录制声音,我们可以通过调用该库,

  • Python使用ffmpy将amr格式的音频转化为mp3格式的例子

    最近做了一个项目,将从微信下载的音频文件(默认为.amr格式)转化为mp3格式(否则前端播放将会遇到困难)上传到云端.经过一番研究,最终决定采用Python的ffmpy包. 首先是ffmpy的文档:http://ffmpy.readthedocs.io/en/latest/index.html 安装只需执行 pip install ffmpy 注意,ffmpy只是命令行工具FFmpeg的一个包装,若要成功执行任务,还需要安装FFmpeg 在Ubuntu上: sudo add-apt-reposi

  • python脚本实现音频m4a格式转成MP3格式的实例代码

    前言 群里看到有人询问:谁会用python将微信音频文件后缀m4a格式转成mp3格式,毫不犹豫回了句:我会. 然后就私下聊起来了 解决方法介绍如下: 工具:windows系统,python2.7,转换库ffmpeg 安装ffmpeg库:下载对应电脑系统版本 https://ffmpeg.zeranoe.com/builds/ 我这里用的是window 64位 这里由于路径问题,我没有把ffmpeg加入到系统环境变量中,所以我用的是绝对路径 C:/Users/Administrator/Downl

  • Python使用pydub库对mp3与wav格式进行互转的方法

    我们需要用到一个叫pydub的类库, pydub是python的高级一个音频处理库,可以让你以一种不那么蠢的方法处理音频.---开发者原话 https://github.com/jiaaro/pydub 附上开发者的github地址 安装: pip install pydub 如果在pycharm中也可以这样安装: setting----Project Interpreter----右边绿色+号 点一下install package 依赖安装: 作者在github 上说,依赖可以安装libav

  • 使用Python实现文字转语音并生成wav文件的例子

    目前手边的一些工作,需要实现声音播放功能,而且仅支持wav声音格式. 现在,一些网站上支持文字转语音功能,但是生成的都是MP3文件,这样还需要额外的软件来转成wav文件,十分麻烦. 后来,研究Python,发现Python可以很容易的实现上面的功能. 步骤如下, 1.使用百度语音实现TTS(Text To Speech),生成mp3文件; 2. 使用pydub和ffmpeg实现mp3转wav格式. 下面,先上简单的示例代码,然后对代码作简单的分析. #!/usr/bin/python -tt #

  • Python实现批量将MP3音频转为WAV格式详解

    目录 前言 环境依赖 代码 验证一下 前言 本文提供批量将MP3格式的音频转为wav格式的工具代码,一如既往的实用主义. 环境依赖 ffmpeg环境安装,可以参考:windows ffmpeg安装部署 ffmpy安装: pip install ffmpy -i https://pypi.douban.com/simple 代码 不废话,上代码. #!/user/bin/env python # coding=utf-8 """ @project : batch-pro @au

  • R语言如何将大型Excel文件转为dta格式详解

    本文以2000年度我国工业企业数据库为例,该文件后缀名为xlsx,包含约16万条记录,文件有88M这么大.直接使用Excel打开都费劲:等待时间久,电脑风扇呼呼呼作响.如果尝试用Stata打开该xlsx文件,则会出现提示报错. 报错原因在于,Stata无法读取超过40M的Excel文件. 这就好比瓜迪奥拉的传控足球固然美丽,但是面对摆大巴的球队无能为力. 破大巴需要攻城锤,这把锤子的名字就是R语言.万事开头难啊,正憧憬着数据清洗和花式选取变量建模呢,可不能连数据们长啥模样都没见着啊.R语言适时挺

  • java 音频转换wav格式标准音频的操作

    目录 简述 环境依赖 maven依赖 ffmpeg依赖 工具类代码 总结 简述 该工具类主要是为了将各类音频转为wav标准格式,其中可以调节采样率.声道数等指标.主要是使用ffmpeg命令进行转换. 环境依赖 maven依赖 <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv-platform</artifactId> <version>1.5.5</v

  • Python用sndhdr模块识别音频格式详解

    本文主要介绍了Python编程中,用sndhdr模块识别音频格式的相关内容,具体如下. sndhdr模块 功能描述:sndhdr模块提供检测音频类型的接口. 唯一一个API sndhdr模块提供了sndhdr.what(filename)和sndhdr.whathdr(filename)两个函数.但实际上它们的功能是一样的.(不知道多写一个的意义何在,what函数在内部调用了whathdr函数并把数据完完整整地返回) 在之前的版本,whathdr函数返回元组类型的数据,在Python3.5版本之

  • python音频处理的示例详解

    准备工作: 首先,我们需要 import 几个工具包,一个是 python 标准库中的 wave 模块,用于音频处理操作,另外两个是 numpy 和 matplot,提供数据处理函数. 一:读取本地音频数据 处理音频第一步是需要从让计算机"听到"声音,这里我们使用 python 标准库中自带的 wave模块进行音频参数的获取. (1) 导入 wave 模块 (2) 使用 wave 中的函数 open 打开音频文件,wave.open(file,mode)函数带有两个参数, 第一个 fi

  • 基于python批量处理dat文件及科学计算方法详解

    摘要:主要介绍一些python的文件读取功能,文件内容修改,文件名后缀更改等操作. 批处理文件功能 import os path1 = 'C:\\Users\\awake_ljw\\Documents\\python for data analysis\\test1' path2 = 'C:\\Users\\awake_ljw\\Documents\\python for data analysis\\test2' filelist = os.listdir(path1) for files i

  • python 函数中的内置函数及用法详解

    今天来介绍一下Python解释器包含的一系列的内置函数,下面表格按字母顺序列出了内置函数: 下面就一一介绍一下内置函数的用法: 1.abs() 返回一个数值的绝对值,可以是整数或浮点数等. print(abs(-18)) print(abs(0.15)) result: 18 0.15 2.all(iterable) 如果iterable的所有元素不为0.''.False或者iterable为空,all(iterable)返回True,否则返回False. print(all(['a','b',

  • Python数据类型之列表和元组的方法实例详解

    引言 我们前面的文章介绍了数字和字符串,比如我计算今天一天的开销花了多少钱我可以用数字来表示,如果是整形用 int ,如果是小数用 float ,如果你想记录某件东西花了多少钱,应该使用 str 字符串型,如果你想记录表示所有开销的物品名称,你应该用什么表示呢? 可能有人会想到我可以用一个较长的字符串表示,把所有开销物品名称写进去,但是问题来了,如果你发现你记录错误了,想删除掉某件物品的名称,那你是不是要在这个长字符串中去查找到,然后删除,这样虽然可行,那是不是比较麻烦呢. 这种情况下,你是不是

  • Python 基于FIR实现Hilbert滤波器求信号包络详解

    在通信领域,可以通过希尔伯特变换求解解析信号,进而求解窄带信号的包络. 实现希尔伯特变换有两种方法,一种是对信号做FFT,单后只保留单边频谱,在做IFFT,我们称之为频域方法:另一种是基于FIR根据传递函数设计一个希尔伯特滤波器,我们称之为时域方法. # -*- coding:utf8 -*- # @TIME : 2019/4/11 18:30 # @Author : SuHao # @File : hilberfilter.py import scipy.signal as signal im

随机推荐