我正在使用 python 和 pyav、ffmpeg 来解码内存中的 mp3。我知道还有其他方法可以做到这一点,比如 pipe ffmpeg 命令。但是,我想探索 pyav 和 ffmpeg API。所以我有以下代码。它可以工作,但声音非常嘈杂,虽然可以听到:
import numpy as np
import av # to convert mp3 to wav using ffmpeg
import pyaudio # to play music
mp3_path = 'D:/MyProg/python/SauTimThiepHong.mp3'
def decodeStream(mp3_path):
# Run NOT OK
container = av.open(mp3_path)
stream = next(s for s in container.streams if s.type == 'audio')
frame_count = 0
data = bytearray()
for packet in container.demux(stream):
# <class 'av.packet.Packet'>
# We need to skip the "flushing" packets that `demux` generates.
#if frame_count == 5000 : break
if packet.dts is None:
continue
for frame in packet.decode():
#<av.AudioFrame 0, pts=1843200, 1152 samples at 44100Hz, stereo, fltp at 0x1d074bd2e48>
# type(frame) : <class 'av.audio.frame.AudioFrame'>
#frame.samples = 1152 : 1152 diem du lieu : Number of audio samples (per channel)
# moi frame co size = 1152 (diem) * 2 (channels) * 4 (bytes / diem) = 9216 bytes
# 11021 frames
#arr = frame.to_ndarray() # arr.nbytes = 9216
#channels = []
channels = frame.to_ndarray().astype("float16")
#for plane in frame.planes:
#channels.append(plane.to_bytes()) #plane has 4 bytes / sample, but audio has only 2 bytes
# channels.append(np.frombuffer(plane, dtype=np.single).astype("float16"))
#channels.append(np.frombuffer(plane, dtype=np.single)) # kieu np.single co 4 bytes
if not frame.is_corrupt:
#data.extend(np.frombuffer(frame.planes[0], dtype=np.single).astype("float16")) # 1 channel: noisy
# type(planes) : <class 'av.audio.plane.AudioPlane'>
frame_count += 1
#print( '>>>> %04d' % frame_count, frame)
#if frame_count == 5000 : break
# mix channels:
for i in range(frame.samples):
for ch in channels: # dec_ctx->channels
data.extend(ch[i]) #noisy
#fwrite(frame->data[ch] + data_size*i, 1, data_size, outfile)
return bytes(data)
我使用管道 ffmpeg 获取解码数据以进行比较并发现它们不同:
def RunFFMPEG(mp3_path, target_fs = "44100"):
# Run OK
import subprocess
# init command
ffmpeg_command = ["ffmpeg", "-i", mp3_path,
"-ab", "128k", "-acodec", "pcm_s16le", "-ac", "0", "-ar", target_fs, "-map",
"0:a", "-map_metadata", "-1", "-sn", "-vn", "-y",
"-f", "wav", "pipe:1"]
# excute ffmpeg command
pipe = subprocess.run(ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize= 10**8)
# debug
#print(pipe.stdout, pipe.stderr)
# read signal as numpy array and assign sampling rate
#audio_np = np.frombuffer(buffer=pipe.stdout, dtype=np.uint16, offset=44)
#audio_np = np.frombuffer(buffer=pipe.stdout, dtype=np.uint16)
#sig, fs = audio_np, target_fs
#return audio_np
return pipe.stdout[78:]
然后我用pyaudio播放数据,发现很吵
p = pyaudio.PyAudio()
streamOut = p.open(format=pyaudio.paInt16, channels=2, rate= 44100, output=True)
#streamOut = p.open(format=pyaudio.paInt16, channels=1, rate= 44100, output=True)
mydata = decodeStream(mp3_path)
print("bytes of mydata = ", len(mydata))
#print("bytes of mydata = ", mydata.nbytes)
ffMpegdata = RunFFMPEG(mp3_path)
print("bytes of ffMpegdata = ", len(ffMpegdata))
#print("bytes of ffMpegdata = ", ffMpegdata.nbytes)
minlen = min(len(mydata), len(ffMpegdata))
print("mydata == ffMpegdata", mydata[:minlen] == ffMpegdata[:minlen]) # ffMpegdata.tobytes()[:minlen] )
#bytes of mydata = 50784768
#bytes of ffMpegdata = 50784768
#mydata == ffMpegdata False
streamOut.write(mydata)
streamOut.write(ffMpegdata)
streamOut.stop_stream()
streamOut.close()
p.terminate()
请帮助我理解 pyav api 的解码帧(在for frame inpacket.decode()之后)。是不是应该多处理?还是我有一些错误?