簡體   English   中英

如何將 numpy 數組轉換為 mp3 文件

[英]How to convert a numpy array to a mp3 file

我正在使用聲卡庫來記錄我的麥克風輸入,它記錄在一個 NumPy 數組中,我想獲取該音頻並將其保存為 mp3 文件。

代碼:

import soundcard as sc
import numpy 
import threading


speakers = sc.all_speakers() # Gets a list of the systems speakers
default_speaker = sc.default_speaker() # Gets the default speaker
mics = sc.all_microphones() # Gets a list of all the microphones


default_mic = sc.get_microphone('Headset Microphone (Arctis 7 Chat)') # Gets the default microphone


# Records the default microphone
def record_mic():
  print('Recording...')
  with default_mic.recorder(samplerate=48000) as mic, default_speaker.player(samplerate=48000) as sp:
      for _ in range(1000000000000):
          data = mic.record(numframes=None) # 'None' creates zero latency
          sp.play(data) 
          
          # Save the mp3 file here 


recordThread = threading.Thread(target=record_mic)
recordThread.start()

帶 Scipy(到 wav 文件)

您可以輕松轉換為 wav,然后單獨將 wav 轉換為 mp3。 更多細節在這里

from scipy.io.wavfile import write

samplerate = 44100; fs = 100
t = np.linspace(0., 1., samplerate)

amplitude = np.iinfo(np.int16).max
data = amplitude * np.sin(2. * np.pi * fs * t)

write("example.wav", samplerate, data.astype(np.int16))

使用 pydub(轉 mp3)

從這個優秀的線程中試試這個 function -

import pydub 
import numpy as np

def write(f, sr, x, normalized=False):
    """numpy array to MP3"""
    channels = 2 if (x.ndim == 2 and x.shape[1] == 2) else 1
    if normalized:  # normalized array - each item should be a float in [-1, 1)
        y = np.int16(x * 2 ** 15)
    else:
        y = np.int16(x)
    song = pydub.AudioSegment(y.tobytes(), frame_rate=sr, sample_width=2, channels=channels)
    song.export(f, format="mp3", bitrate="320k")

#[[-225  707]
# [-234  782]
# [-205  755]
# ..., 
# [ 303   89]
# [ 337   69]
# [ 274   89]]

write('out2.mp3', sr, x)

注意:Output MP3 將是 16 位的,因為 MP3 總是 16 位的。 但是,您可以按照@Arty 的建議將sample_width=3設置為 24 位輸入。

截至目前,接受的答案至少在我的情況下會產生極度失真的聲音,因此這是改進后的版本:

#librosa read 
y,sr=librosa.load(dir+file,sr=None)
y=librosa.util.normalize(y)

#pydub read
sound=AudioSegment.from_file(dir+file)
channel_sounds = sound.split_to_mono()
samples = [s.get_array_of_samples() for s in channel_sounds]
fp_arr = np.array(samples).T.astype(np.float32)
fp_arr /= np.iinfo(samples[0].typecode).max

fp_arr=np.array([x[0] for x in fp_arr])
#i normalize the pydub waveform with librosa for comparison purposes
fp_arr=librosa.util.normalize(fp_arr)

所以你從任何庫中讀取音頻文件並且你有一個波形然后你可以使用下面的代碼將它導出到任何 pydub 支持的編解碼器,我也使用 librosa 讀取波形並且它工作完美。

wav_io = io.BytesIO()
scipy.io.wavfile.write(wav_io, sample_rate, waveform)
wav_io.seek(0)
sound = AudioSegment.from_wav(wav_io)

with open("file_exported_by_pydub.mp3",'wb') as af:
    sound.export(
        af,
        format='mp3',
        codec='mp3',
        bitrate='160000',
)  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM