Created
September 2, 2015 22:30
-
-
Save hosackm/289814198f43976aff9b to your computer and use it in GitHub Desktop.
Flask streaming an audio file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Thank you! Worked like a charm. If someone runs into can't decode byte error, please make sure you're opening the file in 'rb' mode. I missed that the first time.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to delete that mp3 file as soon as it gets streamed.
I noticed that how streaming works :~
So it doesn't matter that file still remains on server or not .
So I want to delete that music file to avoid server jamming and causing
507
errors.What is the best option I can use ?
after_this_request
it is causingPermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
return
statements.crontab
would be last option available.What should I choose ?
If I'm wrong somewhere, Please correct me. 👀