Video Streaming with Flask

Posted by
on under

I'm sure by now you know that I have released a book and a couple of videos on Flask in cooperation with O'Reilly Media. While the coverage of the Flask framework in these is fairly complete, there are a small number of features that for one reason or another did not get mentioned much, so I thought it would be a good idea to write articles about them here.

This article is dedicated to streaming, an interesting feature that gives Flask applications the ability to provide large responses efficiently partitioned in small chunks, potentially over a long period of time. To illustrate the topic I'm going to show you how to build a live video streaming server!

NOTE: there is now a follow-up to this article, Flask Video Streaming Revisited, in which I describe some improvements to the streaming server introduced here.

What is Streaming?

Streaming is a technique in which the server provides the response to a request in chunks. I can think of a couple of reasons why this might be useful:

  • Very large responses. Having to assemble a response in memory only to return it to the client can be inefficient for very large responses. An alternative would be to write the response to disk and then return the file with flask.send_file(), but that adds I/O to the mix. Providing the response in small portions is a much better solution, assuming the data can be generated in chunks.
  • Real time data. For some applications a request may need to return data that comes from a real time source. A pretty good example of this is a real time video or audio feed. A lot of security cameras use this technique to stream video to web browsers.

Implementing Streaming With Flask

Flask provides native support for streaming responses through the use of generator functions. A generator is a special function that can be interrupted and resumed. Consider the following function:

def gen():
    yield 1
    yield 2
    yield 3

This is a function that runs in three steps, each returning a value. Describing how generator functions are implemented is outside the scope of this article, but if you are a bit curious the following shell session will give you an idea of how generators are used:

>>> x = gen()
>>> x
<generator object gen at 0x7f06f3059c30>
>>> next(x)
1
>>> next(x)
2
>>> next(x)
3
>>> next(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

You can see in this simple example that a generator function can return multiple results in sequence. Flask uses this characteristic of generator functions to implement streaming.

The example below shows how using streaming it is possible to generate a large data table, without having to assemble the entire table in memory:

from flask import Response, render_template
from app.models import Stock

def generate_stock_table():
    yield render_template('stock_header.html')
    for stock in Stock.query.all():
        yield render_template('stock_row.html', stock=stock)
    yield render_template('stock_footer.html')

@app.route('/stock-table')
def stock_table():
    return Response(generate_stock_table())

In this example you can see how Flask works with generator functions. A route that returns a streamed response needs to return a Response object that is initialized with the generator function. Flask then takes care of invoking the generator and sending all the partial results as chunks to the client.

For this particular example if you assume Stock.query.all() returns the result of a database query as an iterable, then you can generate a potentially large table one row at a time, so regardless of the number of elements in the query the memory consumption in the Python process will not grow larger and larger due to having to assemble a large response string.

Multipart Responses

The table example above generates a traditional page in small portions, with all the parts concatenated into the final document. This is a good example of how to generate large responses, but something a little bit more exciting is to work with real time data.

An interesting use of streaming is to have each chunk replace the previous one in the page, as this enables streams to "play" or animate in the browser window. With this technique you can have each chunk in the stream be an image, and that gives you a cool video feed that runs in the browser!

The secret to implement in-place updates is to use a multipart response. Multipart responses consist of a header that includes one of the multipart content types, followed by the parts, separated by a boundary marker and each having its own part specific content type.

There are several multipart content types for different needs. For the purpose of having a stream where each part replaces the previous part the multipart/x-mixed-replace content type must be used. To help you get an idea of how this looks, here is the structure of a multipart video stream:

HTTP/1.1 200 OK
Content-Type: multipart/x-mixed-replace; boundary=frame

--frame
Content-Type: image/jpeg

<jpeg data here>
--frame
Content-Type: image/jpeg

<jpeg data here>
...

As you see above, the structure is pretty simple. The main Content-Type header is set to multipart/x-mixed-replace and a boundary string is defined. Then each part is included, prefixed by two dashes and the part boundary string in their own line. The parts have their own Content-Type header, and each part can optionally include a Content-Length header with the length in bytes of the part payload, but at least for images browsers are able to deal with the stream without the length.

Building a Live Video Streaming Server

There's been enough theory in this article, now it is time to build a complete application that streams live video to web browsers.

There are many ways to stream video to browsers, and each method has its benefits and disadvantages. The method that works well with the streaming feature of Flask is to stream a sequence of independent JPEG pictures. This is called Motion JPEG, and is used by many IP security cameras. This method has low latency, but quality is not the best, since JPEG compression is not very efficient for motion video.

Below you can see a surprisingly simple, yet complete web application that can serve a Motion JPEG stream:

#!/usr/bin/env python
from flask import Flask, render_template, Response
from camera import Camera

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

def gen(camera):
    while True:
        frame = camera.get_frame()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen(Camera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

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

This application imports a Camera class that is in charge of providing the sequence of frames. Putting the camera control portion in a separate module is a good idea in this case, this way the web application remains clean, simple and generic.

The application has two routes. The / route serves the main page, which is defined in the index.html template. Below you can see the contents of this template file:

<html>
  <head>
    <title>Video Streaming Demonstration</title>
  </head>
  <body>
    <h1>Video Streaming Demonstration</h1>
    <img src="{{ url_for('video_feed') }}">
  </body>
</html>

This is a simple HTML page with just a heading and an image tag. Note that the image tag's src attribute points to the second route of this application, and this is where the magic happens.

The /video_feed route returns the streaming response. Because this stream returns the images that are to be displayed in the web page, the URL to this route is in the src attribute of the image tag. The browser will automatically keep the image element updated by displaying the stream of JPEG images in it, since multipart responses are supported in most/all browsers (let me know if you find a browser that doesn't like this).

The generator function used in the /video_feed route is called gen(), and takes as an argument an instance of the Camera class. The mimetype argument is set as shown above, with the multipart/x-mixed-replace content type and a boundary set to the string "frame".

The gen() function enters a loop where it continuously returns frames from the camera as response chunks. The function asks the camera to provide a frame by calling the camera.get_frame() method, and then it yields with this frame formatted as a response chunk with a content type of image/jpeg, as shown above.

Obtaining Frames from a Video Camera

Now all that is left is to implement the Camera class, which will have to connect to the camera hardware and download live video frames from it. The nice thing about encapsulating the hardware dependent part of this application in a class is that this class can have different implementations for different people, but the rest of the application remains the same. You can think of this class as a device driver, which provides a uniform implementation regardless of the actual hardware device in use.

The other advantage of having the Camera class separated from the rest of the application is that it is easy to fool the application into thinking there is a camera when in reality there is not, since the camera class can be implemented to emulate a camera without real hardware. In fact, while I was working on this application, the easiest way for me to test the streaming was to do that and not have to worry about the hardware until I had everything else running. Below you can see the simple emulated camera implementation that I used:

from time import time

class Camera(object):
    def __init__(self):
        self.frames = [open(f + '.jpg', 'rb').read() for f in ['1', '2', '3']]

    def get_frame(self):
        return self.frames[int(time()) % 3]

This implementation reads three images from disk called 1.jpg, 2.jpg and 3.jpg and then returns them one after another repeatedly, at a rate of one frame per second. The get_frame() method uses the current time in seconds to determine which of the three frames to return at any given moment. Pretty simple, right?

To run this emulated camera I needed to create the three frames. Using gimp I've made the following images:

Frame 1 Frame 2 Frame 3

Because the camera is emulated, this application runs on any environment, so you can run this right now! I have this application all ready to go on GitHub. If you are familiar with git you can clone it with the following command:

$ git clone https://github.com/miguelgrinberg/flask-video-streaming.git

If you prefer to download it, then you can get a zip file here.

Once you have the application installed, create a virtual environment and install Flask in it. Then you can run the application as follows:

$ python app.py

After you start the application enter http://localhost:5000 in your web browser and you will see the emulated video stream playing the 1, 2 and 3 images over and over. Pretty cool, right?

Once I had everything working I fired up my Raspberry Pi with its camera module and implemented a new Camera class that converts the Pi into a video streaming server, using the picamera package to control the hardware. I will not discuss this camera implementation here, but you can find it in the source code in file camera_pi.py.

If you have a Raspberry Pi and a camera module you can edit app.py to import the Camera class from this module and then you will be able to live stream the Pi camera, like I'm doing in the following screenshot:

Frame 1

If you want to make this streaming application work with a different camera, then all you need to do is write another implementation of the Camera class. If you end up writing one I would appreciate it if you contribute it to my GitHub project.

Limitations of Streaming

When the Flask application serves regular requests the request cycle is short. The web worker receives the request, invokes the handler function and finally returns the response. Once the response is sent back to the client the worker is free and ready to take on another request.

When a request that uses streaming is received, the worker remains attached to the client for the duration of the stream. When working with long, never ending streams such as a video stream from a camera, a worker will stay locked to the client until the client disconnects. This effectively means that unless specific measures are taken, the application can only serve as many clients as there are web workers. When working with the Flask application in debug mode that means just one, so you will not be able to connect a second browser window to watch the stream from two places at the same time.

There are ways to overcome this important limitation. The best solution in my opinion is to use a coroutine based web server such as gevent, which Flask fully supports. With the use of coroutines gevent is able to handle multiple clients on a single worker thread, as gevent modifies the Python I/O functions to issue context switches as necessary.

Conclusion

In case you missed it above, the code that supports this article is this GitHub repository: https://github.com/miguelgrinberg/flask-video-streaming/tree/v1. Here you can find a generic implementation of video streaming that does not require a camera, and also an implementation for the Raspberry Pi camera module. This follow-up article describes some improvements I made after this article was published originally.

I hope this article shed some light on the topic of streaming. I concentrated on video streaming because that is an area I have some experience, but streaming has many more uses besides video. For example, this technique can be used to keep a connection between the client and the server alive for a long time, allowing the server to push new information the moment it becomes available. These days the Web Socket protocol is a more efficient way to achieve this, but Web Socket is fairly new and works only in modern browsers, while streaming will work on pretty much any browser you can think of.

If you have any questions feel free to write them below. I plan to continue documenting more of the not well known Flask topics, so I hope you connect with me in some way to know when more articles are published. I hope to see you in the next one!

Miguel

Become a Patron!

Hello, and thank you for visiting my blog! If you enjoyed this article, please consider supporting my work on this blog on Patreon!

455 comments
  • #76 Gerry Wolfson said

    Miguel,
    I am running the piece of code below:

    <h1>!/usr/bin/env python</h1>

    from flask import Flask, render_template, Response

    <h1>emulated camera</h1> <h1>from camera import Camera</h1> <h1>Raspberry Pi camera module (requires picamera package)</h1>

    from camera_pi import Camera

    app = Flask(name)

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

    def gen(camera):
    """Video streaming generator function."""
    while True:
    frame = camera.get_frame()
    yield (b'--frame\r\n'
    b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

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

    if name == 'main':
    app.run(host='0.0.0.0', debug=True)

    It is working correctly except for the two lines:
    Camera.hflip = True
    Camera.vflip = True

    I have tried placing them in several different location throughout with and without the capital C as appropiate. I can not get the image to invert. What am I doing wrong or what else do I need to include?

    Thanks for the help and a great program,
    =Gerry=

  • #77 Miguel Grinberg said

    @Gerry: the flip controls should go in the camera driver file, not in the main file.

  • #78 Gary W said

    What would be the correct port(s) to forward on my router to view the stream from outside my LAN?

  • #79 Miguel Grinberg said

    @Gary: you need to open the port that you select for your web server. In the example above I used the Flask default, which is 5000. In a production deployment you would probably use port 80, the default for HTTP traffic.

  • #80 Tobu said

    Hi Miguel,

    I am trying to save the captured images before transmission. I want to collect those images and create a video out of it to save it as backup. Can you suggest a way to do that?

    Thanks in advance.

  • #81 Miguel Grinberg said

    @Tobu: encoding a video will slow down your Pi, so it will affect the streaming frame rate. Probably better to create a video on the other end, from a more powerful computer.

  • #82 dmitry said

    Hello. Please tell me how stim just two videos on a single page? I do route / video and / video2. But every time you request Flask touches only one route or / route or / route2. And the first frame has on both, but continues to stream only one

  • #83 Miguel Grinberg said

    @dmitry: I'm going to guess that this is because you have only only worker thread in your server. A streaming connection is long lived, so you need a multi-threaded/multi-processing web server to be able to handle multiple clients and/or multiple streams.

  • #84 Farzad said

    Hi Miguel! Great work, you are a Genius!
    In http://www.chioka.in/python-live-video-streaming-example/#comment-196623
    someone has modified your code to work with a webcam! I tried that code on my mac, and it did't work. Then I changed the cv2.VideoCapture argument with a file name, and it was working! Then I checked it on a Windows machine, and it is working with a webcam! I tried different things for mac, like creating a video thread instead of cv2.videoCapture, because there seems to be a problem here, as the webcam doesn't start (no light going on!) , but it didn't work! I have tried different cameras, but no success! I even tried the bad solution of saving the webcam frames from a different module as jpeg on disk, but this is causing parts of the images to be black, and delay! Do you have a solution for mac?
    Thanks in advance!

  • #85 Miguel Grinberg said

    @Farzad: I have not created a camera driver for Mac, but I'll take a look at this fork and see if it can be adapted. Thanks!

  • #86 Huidong Tian said

    Hi, great work! It works like a charm, this is the easiest method I have even seen.

    I have one question:
    when we start streaming (python app.py), we can see the stream video, but if we want to record the video, or take a picture meanwhile, I got this error:

    pi@raspberrypi /var/www $ raspivid -o vid.h264
    mmal: mmal_vc_component_enable: failed to enable component: ENOSPC
    mmal: camera component couldn't be enabled
    mmal: main: Failed to create camera component
    mmal: Failed to run camera app. Please check for firmware updates

    I have struggled several nights, could you tell me how to record or take a picture while streaming?

    Thanks! Huidong

  • #87 Miguel Grinberg said

    @Huidong: the camera is in use while streaming, so you will not be able to capture images or video from a separate process.

  • #88 Huidong Tian said

    OK, I see. But look at 7th minute of this video: https://www.youtube.com/watch?v=VTZMOHE8v-Q
    It seems that may can record video while streaming, right?

  • #89 Miguel Grinberg said

    @Huidong: it looks like the server application used in the video can do the recording + streaming using a single camera instance. If you look at the third comment in this page:

    https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=63276&sid=08636f781d69ab49223f8374f0522937

    There the author of that program is saying the same, you can't launch raspistill/vid while the program is running, the camera can only be used by one process at a time.

  • #90 Richard said

    Hi @Miguel Grinberg,
    Thanks for this wonderful tutorial. However, I encountered an issue.
    I can start the flask server and view the streaming, but when I refresh the page,
    the server crashes with the following error message:

    Is this due to doing:
    cam = picamera.PiCamera()
    twice whithout closing one first? Can you help?

    Thanks a lot!

    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in call
    return self.wsgi_app(environ, start_response)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functionsrule.endpoint
    File "/home/pi/bundle/monitor.py", line 54, in video_feed
    return Response(gen(cam_setup()),
    File "/home/pi/bundle/monitor.py", line 9, in cam_setup
    cam = picamera.PiCamera()
    File "/usr/share/pyshared/picamera/camera.py", line 488, in init
    self.STEREO_MODES[stereo_mode], stereo_decimate)
    File "/usr/share/pyshared/picamera/camera.py", line 620, in _init_camera
    prefix="Camera component couldn't be enabled")
    File "/usr/share/pyshared/picamera/exc.py", line 191, in mmal_check
    raise PiCameraMMALError(status, prefix)
    PiCameraMMALError: Camera component couldn't be enabled: Out of resources (other than memory)

  • #91 Phong said

    Greatly appreciate your tutorials! I did well the excersice of using Flask + gevent to stream Pi camera to several clients. In another situation, if the Flask server is not on the Pi, how can we push frames to the server, in order for it to re-distribute to clients? Flask has any specialized package(s) for dedicating streaming/pushing protocols such as RTSP and RTMP? If not, is Websocket helpful in such situations?

  • #92 Miguel Grinberg said

    @Phong: there are many ways to do this. One easy way is to make the web server a proxy for your camera host, so it just reads from the camera server and passes the stream through to the client. There are many other ways though, which method you use depends on the particular application.

  • #93 Fred Perkins said

    Miguel, Thanks.

    I'm having a problem. When I run the application I get 'jinja2.exceptions.TemplateNotFound: index.html' I created the html file, named it index.html, and stored it in the same folder as the Flask application. The Flask part seems to be running, since I got all of the debug messages in the browser. Do you have any idea what I might have missed?

    Fred

  • #94 Miguel Grinberg said

    @Fred: by default Flask looks for templates in a "templates" folder sitting next to the application.

  • #95 hi~~ said

    Hi Miguel!!
    I am Korean student. I am sorry for my English skill
    I read your article very well. I am using your source code and I want to add additional functions.
    I want to make web page that stream video and show data in same page.

    but, the function named gen() will never end. It take all resources.
    so I can't execute other functions.

    from flask import Flask, render_template, Response
    from camera import Camera
    import time

    app = Flask(name)

    @app.route('/')
    def index():
    return render_template('index.html')

    def gen(camera):
    while True:
    frame = camera.get_frame()
    yield (b'--frame\r\n'
    b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

    @app.route('/video_feed')
    def video_feed():
    return Response(gen(Camera()),
    mimetype='multipart/x-mixed-replace; boundary=frame')

    @app.route('/test')
    def test():
    return time.time() <<< Like this!! it dose not start. T.T

    if name == 'main':
    app.run(host='0.0.0.0', debug=True)

    I love you Miguel!!

  • #96 Miguel Grinberg said

    @hi~~: that's correct, a streaming route requires a dedicated connection. If you are using the development web server, which is single process and single threaded, then you can't issue any more requests while the streaming endpoint is being accessed. You can run Werkzeug in multi-threaded mode to enable more connections, or else you can go to a corroutine based web server, such as the one in gevent, which also allows multiple simultaneous connections.

  • #97 Rob K said

    Hi Miguel, Thanks for the great tutorial(s), it works great. While streaming, I am trying to change some of the camera properties and I am confused (I am not even sure it is possible) but I thought it would not hurt to ask. My thought was to create a method in the Camera class file that would change the brightness, then call that method and pass it the new value. I am a little confused on how the camera class is being used/created I think. I have tried a few things (lots) in the Camera class file as mentioned, but nothing so far (except disappointment). Any help or advice is appreciated.

    BTW, With your help, I used this with a Raspberry Pi to make a flask driven web app (with Jquery) that I can control from my phones browser to take time lapse photos, mostly of the night sky. I also incorporated your your web streaming example with OpenCV and managed to get the face detection working, and it works great!

    Thanks again,
    Rob

  • #98 Miguel Grinberg said

    @Rob: to change the brightness you don't have to call any functions, just do camera.brightness = X, with X a number between 0 and 100. Note that "camera" is the instance of the PiCamera class.

  • #99 Rob said

    Thanks for the fast response Miguel. I think my problem was that I was trying to set the brightness (camera.brightness = globalbright) using a global variable. At
    this time I was importing the camera class (from camera_pi import Camera). Once I moved the class into my main python program, I did get it to work. I was
    either doing something wrong initially (most likely) or it has something to do with the scope of the global variable. At any rate, I did get it to work
    (maybe not ideal), but it will work until I learn more. Thanks again for the fast response and your time posting this for everyone, it is the best streaming
    solution I found for the Pi. Since I am having so much fun with this project I think it is time to purchase one of your books...I think I ran across one available for Kindle on Amazon..

    Sorry if this is a duplicate, I had a little trouble with the captcha.

    Thanks again,
    Rob

  • #100 Sepp said

    Hi

    I get the following warning:
    /usr/lib/python2.7/dist-packages/picamera/camera.py:151: PiCameraDeprecated: Accessing framerate as a tuple is deprecated; this value is now a Fraction, so you can query the numerator and denominator properties directly, convert to an int or float, or perform arithmetic operations and comparisons directly 'Accessing framerate as a tuple is deprecated; this value is '

Leave a Comment