This commit is contained in:
yumoqing 2024-09-15 00:42:20 +08:00
parent 5d0298ef06
commit d74b26a7e6

View File

@ -139,11 +139,24 @@ class AudioTrackVad(MediaStreamTrack):
return fs return fs
def frames_write_wave(self, frames): def frames_write_wave(self, frames):
"""
fb = [ self.frame2bytes(f) for f in frames ] fb = [ self.frame2bytes(f) for f in frames ]
ndarr = np.frombuffer(b''.join(fb), dtype=np.int16) ndarr = np.frombuffer(b''.join(fb), dtype=np.int16)
fn = temp_file(suffix='.wav') fn = temp_file(suffix='.wav')
write(fn, frames[0].sample_rate, ndarr) write(fn, frames[0].sample_rate, ndarr)
return fn return fn
"""
path = temp_file(suffix='.wav')
output_container = av.open(path, 'w')
out_stream = output_container.add_stream('pcm_s16le')
for frame in frames:
for packet in out_stream.encode(frame):
output_container.mux(packet)
for packet in out_stream.encode(None):
output_container.mux(packet)
output_container.close()
return path
async def write_wave(self): async def write_wave(self):
"""Writes a .wav file. """Writes a .wav file.
@ -179,7 +192,7 @@ class AudioTrackVad(MediaStreamTrack):
resampler = AudioResampler(format=out_stream.format, layout=out_stream.layout, rate=out_stream.rate) resampler = AudioResampler(format=out_stream.format, layout=out_stream.layout, rate=out_stream.rate)
for frame in self.voiced_frames: for frame in self.voiced_frames:
for f in resampler.resample(frame): for f in resampler.resample(frame):
output_container.mux(out_stream.encode(frame)) output_container.mux(out_stream.encode(f))
output_container.mux(out_stream.encode()) output_container.mux(out_stream.encode())
output_container.close() output_container.close()
return path return path