Skip to content

Instantly share code, notes, and snippets.

@jaekookang
Forked from hosackm/flaskaudiostream.py
Created November 24, 2021 06:04
Show Gist options
  • Save jaekookang/5020946f64d249b761b85e0cf64818f4 to your computer and use it in GitHub Desktop.
Save jaekookang/5020946f64d249b761b85e0cf64818f4 to your computer and use it in GitHub Desktop.
Flask streaming an audio file
from flask import Flask, Response
app = Flask(__name__)
@app.route("/wav")
def streamwav():
def generate():
with open("signals/song.wav", "rb") as fwav:
data = fwav.read(1024)
while data:
yield data
data = fwav.read(1024)
return Response(generate(), mimetype="audio/x-wav")
@app.route("/ogg")
def streamogg():
def generate():
with open("signals/song.ogg", "rb") as fogg:
data = fogg.read(1024)
while data:
yield data
data = fogg.read(1024)
return Response(generate(), mimetype="audio/ogg")
if __name__ == "__main__":
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment