簡體   English   中英

當我使用 ffmpeg 將 numpy 數組轉換為音頻文件 (python) 時,為什么 mp3/wav 持續時間不同?

[英]Why is mp3/wav duration different when I convert a numpy array with ffmpeg into audiofile (python)?

我想將一個包含 60 秒原始音頻的 numpy 數組轉換為 .wav 和 .mp3 文件。 使用 ffmpeg(版本 3.4.6),我嘗試將數組轉換為所需的格式。 為了進行比較,我還使用了模塊聲音文件。 只有 soundfile 創建的 .wav 文件的預期長度為 60 秒。 由 ffmpeg 創建的 .wav 文件有點短,而 .mp3 文件則是 ca。 32 秒長。

我希望所有出口的長度相同。我做錯了什么?

這是一個示例代碼:

import subprocess as sp
import numpy as np
import soundfile as sf

def data2audiofile(filename,data):
    out_cmds = ['ffmpeg',
                '-f', 'f64le', # input 64bit float little endian 
                '-ar', '44100', # inpt samplerate 44100 Hz
                '-ac','1', # input 1 channel (mono)
                '-i', '-', # inputfile via pipe
                '-y', #  overwrite outputfile if it already exists
                filename]
    pipe = sp.Popen(out_cmds, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE) 
    pipe.stdin.write(data)


data = (np.random.randint(low=-32000, high=32000, size=44100*60)/32678).astype('<f8')

data2audiofile('ffmpeg_mp3.mp3',data)
data2audiofile('ffmpeg_wav.wav',data)
sf.write('sf_wav.wav',data,44100)

這里以audacity顯示的結果文件:

您需要關閉pipe.stdin並等待子進程結束。

關閉pipe.stdin刷新stdin管道。
此處解釋了該主題:寫入 python 子進程管道

在調用wait之前關閉stdin(刷新並發送EOF)的關鍵

pipe.stdin.write(data)之后添加以下代碼行:

pipe.stdin.close()
pipe.wait()

您還可以嘗試在sp.Popen設置大緩沖區大小:

pipe = sp.Popen(out_cmds, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, bufsize=10**8)

暫無
暫無

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

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