I want to use a looping video on a site made powered by Flask. Apparently, Chrome will not loop the video, unless it was streamed with an HTTP 206 code being returned. Flask, however, always returns this static file with an HTTP 200. How do I stream static content from my Flask project (hosted on Heroku, for the record) to make the video correctly loop in Chrome?
Asked
Active
Viewed 5,060 times
5
-
You probably want to [a custom buildpack with nginx](https://github.com/ryandotsmith/nginx-buildpack) or some other fully featured server. That said, Werkzeug *does* support [`Range`](http://werkzeug.pocoo.org/docs/http/#werkzeug.http.parse_range_header) so it may be possible to implement it in Flask. – Sean Vieira May 30 '14 at 14:19
-
I ultimately did not end up implementing the answer below, I just moved these static files to a CDN. – Underyx Jul 07 '14 at 12:21
2 Answers
9
I had the same problem when serving my video files and I found the solution by digging into the source code of Werkzeug
. I solved it by adding the flag conditional=True
in the send_from_directory
function as follows:
@app.route('/uploads/<filename>')
def uploaded_file(filename):
"""Endpoint to serve uploaded videos
Use `conditional=True` in order to support range requests necessary for
seeking videos.
"""
return send_from_directory(app.config['UPLOAD_FOLDER'], filename,
conditional=True)

se7entyse7en
- 4,310
- 7
- 33
- 50
-
this is a much better (and more specific) solution than my original answer. – tsalaroth Sep 08 '18 at 16:11
-
Is there anything else you changed to make this work? I am still getting response 200. – WolfLink Oct 13 '20 at 03:52
1
Reponse objects in Flask have a "status_code" parameter you can pass. See this documentation for more details, but essentially, you may want to subclass the Response object.
Also take a look at make_response() - it may reveal a simpler way, depending on your application structure.
Take a look at the streaming pattern for more details, but it's geared towards generated content as opposed to static.

tsalaroth
- 112
- 9