开发者

Python实现将wav转amr,并转换成hex数组

开发者 https://www.devze.com 2023-05-07 09:47 出处:网络 作者: eric_pyt@qq.com
目录将wav转amr,并转换成hex数组直接上代码执行generate_audio_file.py已经生成对应的.h文件总结将wav转amr,并转换成hex数组
目录
  • 将wav转amr,并转换成hex数组
  • 直接上代码
  • 执行generate_audio_file.py
  • 已经生成对应的.h文件
  • 总结

将wav转amr,并转换成hex数组

将wav文件快速转为amr,同时将arm文件转为16进制数组,保存在对应.h文件,供嵌入式设备使用(无文件系统产品(⊙o⊙)哦)

直接上代码

generate_audio_file.py

from __future__ import print_function
from builtins import range
import os
import wave
import struct
import time
import re 
import subprocess
import binascii
# eric 2020年7月10日
# wav文件转PCM数组
def get_wave_array_str(filename, target_bits):
    wave_read = wave.open(filename, "r")
    array_str = "// start\n\t"
    nchannels, sampwidth, framerate, nframes, comptype, compname = wave_read.getparams()
    sampwidth *= 8
    for i in range(wave_read.getnframes()):
        val, = struct.unpack("<H", wave_read.readframes(1))
        scale_val = (1 << target_bits) - 1
        cur_lim   = (1 << sampwidth) - 1
        # scale current data to 8-bit data
        val       = val * scale_val / cur_lim
        val       = int(val + ((scale_val + 1) // 2)) & scale_val
        array_str += "0x%02x, " % (val)
        if (i + 1) % 16 == 0:
            array_str += "\n\t"
    return array_str
# PCM 数组合并
def gen_wave_table(wav_file_list, target_file_name, scale_bits=8):
    with open(target_file_name, "w") as audio_table:
        print('#include <stdio.h>', file=audio_table)
        print("// time:{}".format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))), file=audio_table)
        print('const unsigned char audio_table[] = {', file=audio_table)
        for wav in wav_file_list:
            print("processing: {}".format(wav))
            print('//name:{}\n'.format(wav), file=audio_table)
            print(get_wave_array_str(filename=wav, target_bits=scale_bits), file=audio_table)
        print('};\n', file=audio_table)
    print("Done...")
# 获取wav文件信息
def get_wave_info(filename):
    wave_read = wave.open(filename, "r")
    nchannels, sampwidth, framerate, nframes, comptype, compname = wave_read.getparams()
    print('nchannels:{}'.format(nchannels))
    print('sampwidth:{}'.format(sampwidth))
    print(编程客栈'framerate:{}Hz'.format(framerate))
    print('nframes:{}'.format(nframes))
    print('comptype:{}'.format(comptype))
    print('compname:{}'.format(compname))
    return framerate
# 二进制文件转数组
def file2array(filename,h_filename=None):
    path, name = os.path.split(filename)
    if h_filename is None:
        h_filename = os.path.join(path, name.split('.')[0] +'.h')
    array_str = '\n//total len:{} bytes\n'.format(os.path.getsize(filename))
    array_str += 'const unsigned char '+name.split('.')[0]+'[]={\n\t'
    # print(array_str)
    h_file = open(h_filename, "w")
    file = open(filename, 'rb')
    try:
        while True:
            chunk = file.read(16*32)
            if not chunk:
                break
            for i in range(len(chunk)):
                array_str += "0x%02x, " % (chunk[i])
                if (i + 1) % 16 == 0:
                    array_str += "\n\t"
            # print('get:{}'.format(binascii.b2a_hex(chunk)))
    except:
        print('read ERR')
    finally:
        print('read file done!')
        array_str += '\n};\r\n'
        # print('array='+array_str)
        print(array_str, file=h_file)
    h_file.close()
    filewww.devze.com.close()
    return 0
# ffmpeg路径
ffpeg_path = 'D:/Program Files (x86)/QveAudio/'
# amr文件转MP3
def amr2mp3(amr_path,mp3_path=None):
    path, name = os.path.split(amr_path)
    if name.split('.')[-1]!='amr':
        print('not a amr file')
        return 0
    if mp3_path is None or mp3_path.split('.')[-1]!='mp3':
        mp3_path = os.path.join(path, name.split('.')[0] +'.mp3')
    error = subprocess.call(['ffmpeg/bin/ffmpeg','-i',amr_path,mp3_path])
    print('ERR:'+error)
    if error:
        return 0
    print ('success')
    return mp3_path
# wav文件转amr,wav必须为8000Hz
def wave2amr(wave_path,amr_path=None):
    path, name = os.path.split(wave_path)
    if name.split('.')[-1]!='wav':
        print ('not a wave file')
        return 0
    if amr_path is None or amr_path.split('.')[-1]!='amr':
        amr_path = os.path.join(path, name.split('.')[0] +'.amr')
    error = subprocess.call([ffpeg_path+'ffmpeg','-i',wave_path,'-y',amr_path])
    print('ERR:{}'.format(error))
    if error:
        return 0
    print ('amr success')
    return amr_path
# wav 格式化为8000Hz
def wave2Hz8000(wave_path,out_path=None):
    path, name = os.path.split(wave_path)
    if name.split('.')[-1]!='wav':
        print ('not a wave file')
        return 0
    if out_path is None or wave_path.split('.')[-1]!='wav':
        out_path = os.path.join(path, name.split('.')[0] +'_8k.wav')
    # error = subprocess.call([ffpeg_path+'ffmpeg','-i',wave_path,'-ar','8000','-y',out_path])
    error = subprocess.call(ffpeg_path+'ffmpeg'+' -i '+wave_path+' -ar 8000 -y '+out_path)
    print('ERR:{}'.format(error))
    if error:
        return 0
    print ('wav success')
    return out_path
# 将目录下所有wav文件转为amr,并转为对应的数组文件
if __name__ == '__main__':
    print("Generating audio array...")
    # wav_list = []
    for filename in os.listdir("./"):
        # print(os.getcwd())
        if filename.endswith(".wav"):
            print('src_name:',filename)
            framerate = get_wave_info(filename)
            if framerate != 8000:
                print('to 8000Hz')
                filename = wavejavascript2Hz8000(filename)
            amr_file = wave2amr(filename)
            if amr_file!=0:
                file2array(amr_file)
            # dst_name = "0_"+filename
            # print('dst_name:',filename)
            # os.rename(filename, dst_name)
            # wav_list.append(filename)
    # gen_wave_table(wav_file_list=wav_list, target_file_name="audio_example_file.h")

以上代码实现的功能:

  • 将当前目录下的所有wav文件,转为8kHz的wav
  • 将对应的8kHz wav,转为amr
  • 将对应的amr,转为hex数组,并保存在.h文件中

注意:音频的转换用的ffmpeg,使用前需确认对应的路径

ffpeg_path = 'D:/Program Files (x86)/QveAudio/'

转换前文件:

Python实现将wav转amr,并转换成hex数组

执行generate_audio_file.py

E:\Users\eric\Desktop\tools2>generate_audio_file.py
Generating audio array...
src_name: sample_00.wav
nchannels:1
sampwidth:2
framerate:16000Hz
nframes:11541
comptype:NONE
compname:not compressed
to 8000Hz
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libXML2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_00.wav':
  Duration: 00:00:00.72, bitrate: 256 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'sample_00_8k.wav':
  Metadata:
    ISFT            : Lavf58.3.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 pcm_s16le
size=      11kB time=00:00:00.72 bitrate= 128.9kbits/s speed= 103x
video:0kB audio:11kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.675793%
ERR:0
wav success
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_00_8k.wav':
  Metadata:
    encoder         : Lavf58.3.100
  Duration: 00:00:00.72, bitrate: 128 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> amr_nb (libopencore_amrnb))
Press [q] to stop, [?] for help
[libopencore_amrnb @ 043f2d80] bitrate not supported: use one of 4.75k, 5.15k, 5.90k, 6.70k, 7.40k, 7.95k, 10.20k, 12.20k, using 12.20k
Output #0, amr, to 'sample_00_8k.amr':
  Metadata:
    encoder         : Lavf58.3.100
    Stream #0:0: Audio: amr_nb (libopencore_amrnb), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 libopencore_amrnb
size=       1kB time=00:00:00.73 bitrate=  13.0kbits/s speed= 105x    
video:0kB audio:1kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.506757%
ERR:0
amr success
read file done!
src_name: sample_00_8k.wav
nchannels:1
sampwidth:2
framerate:8000Hz
nframes:5771
comptype:NONE
compname:not compressed
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_00_8k.wav':
  Metadata:
    encoder         : Lavf58.3.100
  Duration: 00:00:00.72, bitrate: 128 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> amr_nb (libopencore_amrnb))
Press [q] to stop, [?] for help
[libopencore_amrnb @ 03a02d80] bitrate not supported: use one of 4.75k, 5.15k, 5.90k, 6.70k, 7.40k, 7.95k, 10.20k, 12.20k, using 12.20k
Output #0, amr, to 'sample_00_8k.amr':
  Metadata:
    encoder         : Lavf58.3.100
    Stream #0:0: Audio: amr_nb (libopencore_amrnb), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 libopencore_amrnb
size=       1kB time=00:00:00.73 bitrate=  13.0kbits/s speed=16.4x
video:0kB audio:1kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.506757%
ERR:0
amr success
read file done!
src_name: sample_01.wav
nchannels:1
sampwidth:2
framerate:16000Hz
nframes:16904
comptype:NONE
compname:not compressed
to 8000Hz
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_01.wav':
  Duration: 00:00:01.06, bitrate: 256 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'sample_01_8k.wav':
  Metadata:
    ISFT            : Lavf58.3.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 pcm_s16le
size=      17kB time=00:00:01.05 bitrate= 128.6kbits/s speed=44.1x
video:0kB audio:17kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.461429%
ERR:0
wav success
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_01_8k.wav':
  Metadata:
    encoder         : Lavf58.3.100
  Duration: 00:00:01.06, bitrate: 128 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> amr_nb (libopencore_amrnb))
Press [q] to stop, [?] for help
[libopencore_amrnb @ 037c1ec0] bitrate not supported: use http://www.devze.comone of 4.75k, 5.15k, 5.90k, 6.70k, 7.40k, 7.95k, 10.20k, 12.20k, using 12.20k
Output #0, amr, to 'sample_01_8k.amr':
  Metadata:
    encoder         : Lavf58.3.100
    Stream #0:0: Audio: amr_nb (libopencore_amrnb), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 libopencore_amrnb
size=       2kB time=00:00:01.07 bitrate=  12.9kbits/s speed=  43x    
video:0kB audio:2kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.347222%
ERR:0
amr success
read file done!
src_name: sample_01_8k.wav
nchannels:1
sampwidth:2
framerate:8000Hz
nframes:8452
comptype:NONE
compname:not compressed
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_php01_8k.wav':
  Metadata:
    encoder         : Lavf58.3.100
  Duration: 00:00:01.06, bitrate: 128 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> amr_nb (libopencore_amrnb))
Press [q] to stop, [?] for help
[libopencore_amrnb @ 03b81ec0] bitrate not supported: use one of 4.75k, 5.15k, 5.90k, 6.70k, 7.40k, 7.95k, 10.20k, 12.20k, using 12.20k
Output #0, amr, to 'sample_01_8k.amr':
  Metadata:
    encoder         : Lavf58.3.100
    Stream #0:0: Audio: amr_nb (libopencore_amrnb), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 libopencore_amrnb
size=       2kB time=00:00:01.07 bitrate=  12.9kbits/s speed=38.5x    
video:0kB audio:2kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.347222%
ERR:0
amr success
read file done!
src_name: sample_02.wav
nchannels:1
sampwidth:2
framerate:16000Hz
nframes:51067
comptype:NONE
compname:not compressed
to 8000Hz
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_02.wav':
  Duration: 00:00:03.19, bitrate: 256 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'sample_02_8k.wav':
  Metadata:
    ISFT            : Lavf58.3.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 pcm_s16le
size=      50kB time=00:00:03.19 bitrate= 128.2kbits/s speed= 457x
video:0kB audio:50kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.152738%
ERR:0
wav success
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx开发者_Go培训265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_02_8k.wav':
  Metadata:
    encoder         : Lavf58.3.100
  Duration: 00:00:03.19, bitrate: 128 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> amr_nb (libopencore_amrnb))
Press [q] to stop, [?] for help
[libopencore_amrnb @ 03f50900] bitrate not supported: use one of 4.75k, 5.15k, 5.90k, 6.70k, 7.40k, 7.95k, 10.20k, 12.20k, using 12.20k
Output #0, amr, to 'sample_02_8k.amr':
  Metadata:
    encoder         : Lavf58.3.100
    Stream #0:0: Audio: amr_nb (libopencore_amrnb), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 libopencore_amrnb
size=       5kB time=00:00:03.19 bitrate=  12.8kbits/s speed= 160x    
video:0kB audio:5kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.117188%
ERR:0
amr success
read file done!
src_name: sample_02_8k.wav
nchannels:1
sampwidth:2
framerate:8000Hz
nframes:25534
comptype:NONE
compname:not compressed
ffmpeg version N-89672-g41e51fbcd9 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.2.0 (GCC)
  configuration: --disable-static --enable-shared --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-amf --enable-cuda --enable-cuvid --enable-d3d11va --enable-nvenc --enable-dxva2 --enable-avisynth --enable-libmfx
  libavutil      56.  7.100 / 56.  7.100
  libavcodec     58.  9.100 / 58.  9.100
  libavformat    58.  3.100 / 58.  3.100
  libavdevice    58.  0.100 / 58.  0.100
  libavfilter     7.  8.100 /  7.  8.100
  libswscale      5.  0.101 /  5.  0.101
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'sample_02_8k.wav':
  Metadata:
    encoder         : Lavf58.3.100
  Duration: 00:00:03.19, bitrate: 128 kb/s
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 8000 Hz, mono, s16, 128 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> amr_nb (libopencore_amrnb))
Press [q] to stop, [?] for help
[libopencore_amrnb @ 039b0900] bitrate not supported: use one of 4.75k, 5.15k, 5.90k, 6.70k, 7.40k, 7.95k, 10.20k, 12.20k, using 12.20k
Output #0, amr, to 'sample_02_8k.amr':
  Metadata:
    encoder         : Lavf58.3.100
    Stream #0:0: Audio: amr_nb (libopencore_amrnb), 8000 Hz, mono, s16, 128 kb/s
    Metadata:
      encoder         : Lavc58.9.100 libopencore_amrnb
size=       5kB time=00:00:03.19 bitrate=  12.8kbits/s speed= 146x
video:0kB audio:5kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.117188%
ERR:0
amr success
read file done!
E:\Users\eric\Desktop\tools2>

已经生成对应的.h文件

Python实现将wav转amr,并转换成hex数组

生成的格式如下:

//total len:1190 bytes
const unsigned char sample_00_8k[]={
	0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a, 0x3c, 0xb9, 0xd9, 0x71, 0x28, 0xb8, 0x63, 0xc1, 0xac, 0xec, 
	0xc9, 0xe1, 0xc6, 0x66, 0x47, 0x23, 0x00, 0x00, 0x4b, 0xf1, 0x20, 0xf1, 0x15, 0x5c, 0x00, 0x03, 
	0x6d, 0x50, 0x30, 0xb8, 0x36, 0x10, 0x3c, 0xbc, 0xed, 0x93, 0x5e, 0xb6, 0xc6, 0x8a, 0xfe, 0x87, 
	0x94, 0xfb, 0xd6, 0x6e, 0x86, 0x0c, 0x46, 0xe7, 0x4d, 0x98, 0x54, 0xf2, 0x1c, 0xd7, 0xe5, 0xbf, 
	0x93, 0x46, 0x87, 0x0e, 0x7f, 0x90, 0x3c, 0xbf, 0xd9, 0x67, 0x2a, 0xcb, 0x4d, 0x15, 0xbe, 0x1e, 
	0xdf, 0xc5, 0x96, 0xa9, 0xd5, 0x3b, 0x86, 0x44, 0xd5, 0x9d, 0xdd, 0xf5, 0x16, 0x3d, 0x1e, 0x29, 
	0x33, 0x97, 0xc5, 0x81, 0x0f, 0xe0, 0x3c, 0xb0, 0xf0, 0x1d, 0xe8, 0x4c, 0x78, 0x05, 0xde, 0x0d, 
	0x54, 0x41, 0x7d, 0xb1, 0x39, 0x04, 0x56, 0x22, 0xdf, 0x99, 0x98, 0x02, 0xe3, 0x7a, 0x0f, 0xf7, 
	0x96, 0x76, 0xf6, 0x0a, 0x81, 0x40, 0x3c, 0x78, 0xf0, 0x29, 0x0f, 0xf0, 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xb3, 0x69, 0x1f, 0xee, 0xe8, 0xf1, 0xd2, 0x9c, 
	0x96, 0x2d, 0x80, 0x3c, 0xa6, 0x20, 0x3c, 0x42, 0xf3, 0x97, 0x22, 0xaa, 0x72, 0x11, 0x10, 0xee, 
	0xd8, 0xb6, 0x3b, 0x0c, 0x72, 0xed, 0xf7, 0xc2, 0xbc, 0xbc, 0x3a, 0xbc, 0xe5, 0x64, 0x9b, 0xc6, 
	0x51, 0xe8, 0xba, 0x7b, 0xcc, 0xc0, 0x3c, 0x91, 0x32, 0x23, 0x08, 0x58, 0x06, 0x1c, 0xfc, 0x21, 
	0xad, 0xba, 0xa7, 0x00, 0x1f, 0xcf, 0xf2, 0x88, 0x21, 0xe7, 0xe9, 0x62, 0xe6, 0x5c, 0xcd, 0x78, 
	0xa7, 0x39, 0x83, 0x5d, 0xf8, 0xa0, 0x3c, 0x95, 0x28, 0x69, 0x5d, 0x37, 0xae, 0x16, 0x9e, 0x00, 
	0x3a, 0x82, 0x0a, 0x22, 0xcf, 0x19, 0xb1, 0x34, 0x32, 0x57, 0x06, 0xc6, 0x86, 0xb0, 0x83, 0x9f, 
	0x98, 0xb3, 0x55, 0xa0, 0xc6, 0x50, 0x3c, 0x55, 0x27, 0x7f, 0x10, 0x06, 0xd8, 0x00, 0x01, 0x10, 
	0xaf, 0x0b, 0xae, 0x64, 0x24, 0xc4, 0x49, 0x79, 0x69, 0xba, 0x05, 0x94, 0x20, 0x02, 0x46, 0x30, 
	0x0c, 0x30, 0x04, 0x3a, 0x49, 0x00, 
};

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

0

精彩评论

暂无评论...
验证码 换一张
取 消