【问题标题】:Python - Reading a large audio file to a stream?Python - 将大型音频文件读取到流中?
【发布时间】:2022-07-19 04:39:00
【问题描述】:

问题

我想加载任何类型(mp3、m4a、flac 等)的音频文件并将其写入输出流。

我尝试使用pydub,但它一次加载整个文件,这需要很长时间并且很容易耗尽内存。 我也尝试过使用python-vlc,但它不可靠而且太黑了。

那么,如何逐块打开大型音频文件进行流式传输?

编辑#1

我找到了 一半的解决方案 here,但我需要对另一半做更多的研究。

TL;DR: 使用subprocessffmpeg 将文件转换为wav 数据,并将该数据通过管道传输到np.frombuffer。问题是,在使用frombuffer 之前,子进程仍然必须完成。

...除非可以将管道写入 1 个线程,而 np 从另一个线程读取它,我还没有测试过。目前,这个问题没有解决

【问题讨论】:

标签: python python-3.x audio pydub


【解决方案1】:

我认为 python 包https://github.com/irmen/pyminiaudio 可能会有所帮助。您可以像这样流式传输音频文件

import miniaudio


audio_path = "my_audio_file.mp3"
target_sampling_rate = 44100  #the input audio will be resampled a this sampling rate
n_channels = 1  #either 1 or 2
waveform_duration = 30 #in seconds
offset = 15 #this means that we read only in the interval [15s, duration of file]

waveform_generator = miniaudio.stream_file(
     filename = audio_path,
     sample_rate = target_sampling_rate,
     seek_frame = int(offset * target_sampling_rate),
     frames_to_read = int(waveform_duration * target_sampling_rate),
     output_format = miniaudio.SampleFormat.FLOAT32,
     nchannels = n_channels)


for waveform in waveform_generator:
    #do something with the waveform....

我确定这适用于 mp3、ogg、wav、flac,但由于某种原因它不适用于 mp4/acc,我实际上正在寻找一种阅读 mp4/acc 的方法

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多