如何从无尽的音频流中获取和划分音频

How to get and divide audio from an endless stream of audio

首先,我有一个无尽的视频流,其中包括音频和视频。

如何在流式传输时根据RTSP流式传输的时间间隔获取分割的.wav文件。

我试过下面的代码,但在流结束前无法获取音频数据

command = ['ffmpeg.exe',
                   '-i', 'rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4',
                   '-f', 's16le',
                   '-acodec', 'libmp3lame',
                   '-ar', '44100',  
                   '-ac', '2', 
                   '-']
 
pipe = sp.Popen(command, stdout=sp.PIPE)

raw_audio = self.pipe.stdout.read()
print(raw_audio)

尝试-f segment输出容器。类似于:

command = ['ffmpeg.exe',
    "-i", r"rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4",
    '-vn', '-acodec', 'pcm_s16le', '-ar', '44100', '-ac', '2', 
    "-f", "segment", '-segment_time','3','out%03d.wav']
)

现在,如果您真正需要的是原始样本而不一定是 .wav 文件,则需要通过删除 '-acodec', 'libmp3lame' 选项并指定要读取的样本数来修复您的命令:

# how to read a block of audio data from stdout
n = 44100 * 3 # # of samples (sampling rate * duration)
nbytes = n * 2 * 2 # (#samples * #ch  * 2 bytes/sample)
while True:
    raw_audio = np.frombuffer(self.pipe.stdout.read(nread),shape=(n,2), dtype=np.int16)

    ... # do your thing