I have the following server side route code for stream
movies_list = ['./mov1.mp4', './mov2.mp4', './mov3.mp4',]
@app.route("/video", methods=["GET"])
def video():
headers = request.headers
if not "range" in headers:
return current_app.response_class(status=400)
video_path = os.path.abspath(os.path.join("media", movies_list[0]))
size = os.stat(video_path)
size = size.st_size
chunk_size = (10 ** 6) * 3 #1000kb makes 1mb * 3 = 3mb (this is based on your choice)
start = int(re.sub("D", "", headers["range"]))
end = min(start + chunk_size, size - 1)
content_lenght = end - start + 1
def get_chunk(video_path, start, chunk_size):
with open(video_path, "rb") as f:
f.seek(start)
chunk = f.read(chunk_size)
return chunk
headers = {
"Content-Range": f"bytes {start}-{end}/{size}",
"Accept-Ranges": "bytes",
"Content-Length": content_lenght,
"Content-Type": "video/mp4",
}
return current_app.response_class(get_chunk(video_path, start,chunk_size), 206, headers)
Also client side html page
<video width="720" controls>
<source src="/video" type="video/mp4">
</video>
How can I send a request from the client side to the server so that it changes the source and starts playing?
2
Answers
and source change via JS by click button It works, thanks
If you use url
/video/<int:index>
inFlask
to select file byindex
then you can use
JavaScript
to replacesrc=
in<source>
with differentindex
and browser will load different stream.JavaScript with buttons can look like this:
Full working example:
I use
render_template_string
so everyone can easily copy and run code.