一长一短音频波形转成二进制,进一步转成十六进制,再写入文件中
import wave, codecs import numpy as np wavfile = wave.open(u'music.wav',"rb") params = wavfile.getparams() nframes = params[3] # 采样点数 datawav = wavfile.readframes(nframes) # 读取音频,字符串格式 wavfile.close() datause = np.fromstring(datawav, dtype = np.short) # 将字符串转化为短整型 result_bin, result_hex = '', '' mx = 0 for i in range(len(datause) - 1): if datause[i] > mx: mx = datause[i] try: if(datause[i] < 0 and datause[i+1] >= 0): if (mx - 24000 > 0): result_bin += '1' mx = datause[i+1] else: result_bin += '0' mx = datause[i+1] except: break for i in range(0, len(result_bin), 4): result_hex += hex(int(result_bin[i : i + 4], 2))[2:] file_rar = open("result.txt","wb") file_rar.write(codecs.decode(result_hex, 'hex_codec')) file_rar.close()
标签:bin,文件,01,音频,hex,datause,result,mx,wavfile From: https://www.cnblogs.com/willingyut/p/17660271.html