How to stream video on Raspberry Pi

A Raspberry Pi can have a myriad of specialized uses, and streaming live video is one of them. There are multiple camera models sold for the Raspberry Pi, which you can browse over at the official website. These cameras plug in via ribbon cable directly to the board of the Raspberry Pi, and then it is an easy set up to stream the video or take still pictures from the camera.

This is useful if you want to set it up as a typical webcam, or go one step further and turn your Raspberry Pi into some kind of video surveillance system. There are entire operating systems dedicated to turning a Raspberry Pi device into a security camera, so it just depends on how far you want to take your setup. In this tutorial, we will go through the step by step instructions of configuring a camera on the Raspberry Pi and using it to stream live video or take pictures.

In this tutorial you will learn:

  • How to plug the camera into Raspberry Pi
  • How to take pictures and videos from Raspberry Pi camera
  • How to share out video stream from Raspberry Pi camera
  • How to access the video stream with VLC media player
How to stream video on Raspberry Pi
How to stream video on Raspberry Pi
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to Stream Video on Raspberry Pi – step by step instuctions



DID YOU KNOW?
There are many different ways that this project can be approached. We will be keeping it simple by sticking with the default Raspberry Pi OS, and only using the command line terminal and default applications to access our video stream.

There are many other apps or even operating systems geared towards this purpose, but that will only be relevant for users that wish to fully dedicate their Raspberry Pi device to streaming video full time.

  1. The first step will be to plug in your camera to the Raspberry Pi board. Make sure your Raspberry Pi is off before doing this step. You can locate the ribbon plug on the Raspberry Pi, which is indicated in the screenshot below. If you look closely, you will see the word “Camera” written right above where you need to insert the pins of the ribbon. The blue tab on the ribbon needs to be facing the direction of your USB and Ethernet plugs, then you have it oriented correctly. Then, turn the Raspberry Pi on.
    Finding the ribbon plug for the camera on the Raspberry Pi board
    Finding the ribbon plug for the camera on the Raspberry Pi board
  2. The camera should already be enabled by default. You can check this by going to Preferences > Raspberry Pi configuration and checking that camera is enabled under the interfaces tab.
    Access Raspberry Pi configuration to turn the camera on
    Access Raspberry Pi configuration to turn the camera on
  3. Check that the camera is working as expected by running the following test command:
    $ rpicam-hello
    

    This will open a camera window for a few seconds. Alternatively, supply the -t 0 option with the command to make the window remain open until it is manually closed:

    $ rpicam-hello -t 0
    
  4. The rpi-jpeg command is used to take a still picture from the camera:
    $ rpicam-jpeg -o image.jpg
    



  5. To record a video with the camera, we will utilize the rpicam-vid tool. The -t option lets us select the duration of the video, given in milliseconds. The -o options allows us to specify the file to where the video stream should be saved. The following command will record a 10 second video to output file video.h264:
    $ rpicam-vid -t 10000 -o video.h264
    
  6. The above commands will come in handy, but what if we want to continually stream video from the Raspberry Pi and access the stream? To do this, we will once again use the rpicam-vid command, but specify an indefinite amount of time with -t, along with the --inline option, and an IP address and port number to output the stream to with -o.
    $ rpicam-vid -t 0 --inline -o udp://<ip-addr>:<port>
    

    For the IP address, you could substitute localhost or the Raspberry Pi’s IP address.

  7. To access the stream, we can open it in VLC (installed by default on Raspberry Pi).
    $ vlc udp://<ip-addr>:<port> :demux=h264
    
  8. The alternative is to use the GUI to access the network stream instead of opening it from command line. Open up VLC and access the stream by going to Media > Open Network Stream.
    Opening network stream with VLC application
    Opening network stream with VLC application
  9. Then, enter the UDP address specified above to access the stream.
    Accessing network stream with VLC media player
    Accessing network stream with VLC media player

    This should work from the Raspberry Pi itself as well as from any other device that tries to access the Raspberry Pi from over the network, assuming that you have the appropriate permissions and firewall exceptions configured for other clients to access the port number.

NOTE
For an exhaustive list of options and configurable stream settings, please check the official camera software documentation on the Raspberry Pi website. What has been covered here will get you up and running, but those looking for more granular settings will need to do some extra research through the manual page.


Closing Thoughts

In this tutorial, we saw how to stream video on a Raspberry Pi system. This involved getting the camera plugged into the Raspberry Pi board, sharing out the stream via UDP, facilitated by the default rpicam-vid tool. Then, we used VLC from command line and GUI to open up the stream. We can now access the stream from the local device or from over the network when proper firewall exceptions have been configured.



Comments and Discussions
Linux Forum