【问题标题】:Audio Livestreaming with Python & Flask使用 Python 和 Flask 进行音频直播
【发布时间】:2018-12-07 07:50:32
【问题描述】:

我目前正在努力使用 Python 和 Flask 实现一个简单的实时流媒体 Web 应用程序。 我似乎无法将我的现场录制音频从服务器麦克风输入流式传输到网页。

server.py

from flask import Flask, render_template, Response
import cv2
import framework
import pyaudio
import audio_processing as audioRec

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024

audio = pyaudio.PyAudio()


app = Flask(__name__)


@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


# Stream routing
@app.route('/video_feed')
def video_feed():
    """Video streaming route. Put this in the src attribute of an img tag."""
    return Response(generateVideo(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')


@app.route("/audio_feed")
def audio_feed():
    """Audio streaming route. Put this in the src attribute of an audio tag."""
    return Response(generateAudio(),
                    mimetype="audio/x-wav")


# Stream generating
def generateVideo():
    """Video streaming generator function."""
    cap = cv2.VideoCapture(0)
    while (cap.isOpened()):
        ret, frame = cap.read()
        output = framework.streamer(frame, 'final')
        cv2.imwrite('signals/currFrame.jpg', output)
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + open('signals/currFrame.jpg', 'rb').read() + b'\r\n')


def generateAudio():
    """Audio streaming generator function."""
    currChunk = audioRec.record()
    data_to_stream = genHeader(44100, 32, 1, 200000) + currChunk
    yield data_to_stream

    # with open("signals/audio.wav", "rb") as fwav:
    #     data = fwav.read(1024)
    #     while data:
    #         yield data
    #         data = fwav.read(1024)


def genHeader(sampleRate, bitsPerSample, channels, samples):
    datasize = samples * channels * bitsPerSample // 8
    o = bytes("RIFF",'ascii')                                               # (4byte) Marks file as RIFF
    o += (datasize + 36).to_bytes(4,'little')                               # (4byte) File size in bytes excluding this and RIFF marker
    o += bytes("WAVE",'ascii')                                              # (4byte) File type
    o += bytes("fmt ",'ascii')                                              # (4byte) Format Chunk Marker
    o += (16).to_bytes(4,'little')                                          # (4byte) Length of above format data
    o += (1).to_bytes(2,'little')                                           # (2byte) Format type (1 - PCM)
    o += (channels).to_bytes(2,'little')                                    # (2byte)
    o += (sampleRate).to_bytes(4,'little')                                  # (4byte)
    o += (sampleRate * channels * bitsPerSample // 8).to_bytes(4,'little')  # (4byte)
    o += (channels * bitsPerSample // 8).to_bytes(2,'little')               # (2byte)
    o += (bitsPerSample).to_bytes(2,'little')                               # (2byte)
    o += bytes("data",'ascii')                                              # (4byte) Data Chunk Marker
    o += (datasize).to_bytes(4,'little')                                    # (4byte) Data size in bytes
    return o




if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True, threaded=True)

audio_processing.py

import pyaudio

FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
CHUNK = 1024

audio = pyaudio.PyAudio()


def record():
    # start Recording
    stream = audio.open(format=FORMAT, channels=CHANNELS,
                    rate=RATE, input=True,
                    frames_per_buffer=CHUNK)
    # print "recording..."
    data = stream.read(CHUNK)
    return data

我正在尝试使用 audio_processing.py 获取麦克风的当前块,并使用 yield 将当前样本响应给用户。视频流效果很好。 任何人都知道,我在这里做错了什么?

亲切的问候, 费利克斯

【问题讨论】:

  • 请注意,您不应该持续打开 PyAudio 流;即在流式传输开始之前将其设置为 init 函数调用或类似的东西。然后在需要时使用 record() 方法访问流对象以从中读取。或者如果您想继续打开它,至少关闭它。
  • 是的,你是对的,我稍微修改了代码。我最近将 pyAudio 录制过程添加到 server.py 到 generateAudio() 中。该功能现在应该可以正常工作。唯一的问题是,我当前使用的 Python2.7 不支持 byte() 函数。您有什么想法吗,如何在 2.7 中编写类似的功能?找不到预制的解决方案:/
  • 你好 @F.Geißler ,我想看看你是如何在你的 html 中调用 @app.route("/audio_feed") 端点的。此设置的新手。会有很大的帮助。

标签: python flask live-streaming


【解决方案1】:

这是一个使用设备内置麦克风的工作示例: 抱歉无法解释太多,但这是我为我的应用找到的内容!

app.py

from flask import Flask, Response,render_template
import pyaudio

app = Flask(__name__)


FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5


audio1 = pyaudio.PyAudio()



def genHeader(sampleRate, bitsPerSample, channels):
    datasize = 2000*10**6
    o = bytes("RIFF",'ascii')                                               # (4byte) Marks file as RIFF
    o += (datasize + 36).to_bytes(4,'little')                               # (4byte) File size in bytes excluding this and RIFF marker
    o += bytes("WAVE",'ascii')                                              # (4byte) File type
    o += bytes("fmt ",'ascii')                                              # (4byte) Format Chunk Marker
    o += (16).to_bytes(4,'little')                                          # (4byte) Length of above format data
    o += (1).to_bytes(2,'little')                                           # (2byte) Format type (1 - PCM)
    o += (channels).to_bytes(2,'little')                                    # (2byte)
    o += (sampleRate).to_bytes(4,'little')                                  # (4byte)
    o += (sampleRate * channels * bitsPerSample // 8).to_bytes(4,'little')  # (4byte)
    o += (channels * bitsPerSample // 8).to_bytes(2,'little')               # (2byte)
    o += (bitsPerSample).to_bytes(2,'little')                               # (2byte)
    o += bytes("data",'ascii')                                              # (4byte) Data Chunk Marker
    o += (datasize).to_bytes(4,'little')                                    # (4byte) Data size in bytes
    return o

@app.route('/audio')
def audio():
    # start Recording
    def sound():

        CHUNK = 1024
        sampleRate = 44100
        bitsPerSample = 16
        channels = 2
        wav_header = genHeader(sampleRate, bitsPerSample, channels)

        stream = audio1.open(format=FORMAT, channels=CHANNELS,
                        rate=RATE, input=True,input_device_index=1,
                        frames_per_buffer=CHUNK)
        print("recording...")
        #frames = []
        first_run = True
        while True:
           if first_run:
               data = wav_header + stream.read(CHUNK)
               first_run = False
           else:
               data = stream.read(CHUNK)
           yield(data)

    return Response(sound())

@app.route('/')
def index():
    """Video streaming home page."""
    return render_template('index.html')


if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True, threaded=True,port=5000)

index.html - 在当前目录下的模板文件夹下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <audio controls>
        <source src="{{ url_for('audio') }}" type="audio/x-wav;codec=pcm">
        Your browser does not support the audio element.
    </audio>
</body>
</html>

【讨论】:

  • 尽量不要只发布代码答案,还要向 OP 解释他的代码出了什么问题
  • 确定!会记得哥们!稍微修改了一下
  • 嗨,我正在尝试设置它并且我已经能够让它播放,但是有一个响亮的方波声音覆盖在音频上。 Pyaudio录制到具有相同编码的文件似乎没有效果,所以我认为流式后期录制有问题。关于这可能是什么的任何输入都会有所帮助。我已尝试更改频道数量并尝试了我机器上的所有输入设备。
  • 您应该只发送一次标头;此代码正在发送每个块的标头。这导致了@Mr.Negi 注意到的“响亮的方波声音”。
  • 我应用了您建议的更改并验证它是否有效,我已经提交了一个编辑请求,其中包含对答案的更改,因为我没有足够的声誉在没有确认的情况下进行编辑。感谢您在这里提供见解,作为音频流领域的非专家,我不知道这是问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
  • 2020-06-02
  • 2019-11-23
  • 1970-01-01
  • 2015-03-05
  • 2013-06-03
  • 1970-01-01
相关资源
最近更新 更多