How to Stream Live Video from Your Raspberry Pi Camera

0

The official Raspberry Pi camera comes in many forms. From the original v1.3 to the latest Raspberry Pi HQ Camera, all use the same software. Using the raspistill and raspivid commands, we can take still images and videos directly from the terminal.

More advanced uses included manually controlling the white balance, color, saturation, and brightness of an image and video. We can also create a simple video streaming service with just one line of code.

In this tutorial, we’ll learn how to set up the camera, take a few test shots, and then create a test stream to make sure everything is working before we jump into a project that creates video art using effects. of live video image that we can save on our desktop computer with very little effort.

Configure a Raspberry Pi camera

If you already know how to set up a Pi camera module, you can move on. These steps will work for all Raspberry Pi camera modules (including third-party ones).

With the Raspberry Pi turned off.

1. Open the camera port by gently lifting the plastic lock upwards.

(Image credit: Tom’s Hardware)

2. Insert the ribbon connector with the blue tab facing the USB/Ethernet ports.

(Image credit: Tom’s Hardware)

3. Close the connector lock and pull it very gently to make sure it is in place.

4. Turn on your Raspberry Pi then go to Preferences >> Raspberry Pi Configuration.

(Image credit: Tom’s Hardware)

5. Click the Enable button for the camera found in the Interfaces tab.

(Image credit: Tom’s Hardware)

6. Click OK and to restart ft.

seven. Open a terminal and type the following command take a quick photo to test the camera.

$ raspistill -o test.jpg

After five seconds, an image will be taken and saved as test.jpg. Using the file manager, verify that the image is correct before continuing.

Test a flow

To start a stream, we need to open a terminal and enter a rather long command. Make sure your Raspberry Pi is connected to the network. For best performance use an ethernet cable, Wi-Fi will work, but you may see dropouts.

1. Obtain the hostname of your Raspberry Pi. Open a terminal and type this command for your hostname. Note the host name. You can need to add “.local” at the end, depending on your network.

$ hostname

2. Run the streaming command. The one line command to run a live video stream from the camera is quite long, so let’s review the command before running it.

-o is our output, in this case set to none.

-t is the length of the video clip, using zero will set it to infinite.

-w and -h are the width and height of the video, in this case 800 x 600.

-fps is the frames per second for the video stream, a lower value should minimize dropouts.

| cvlc is a pipe that takes the output of the raspivid command, our video stream, and streams the video using an h264 codec over the real-time streaming protocol (rtsp) over our network.

Run this command in a terminal on your Raspberry Pi.

raspivid -o - -t 0 -w 800 -h 600 -fps 12  | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8080/}' :demux=h264

3. On your Windows/Mac/Linux computer install VLC then open VLC.

4. Go to Media >> Open Network Streamor press CTRL+N.

(Image credit: Tom’s Hardware)

5. Enter rtsp address and port of your Raspberry Pi. The rtsp address is the hostname of your Raspberry Pi, preceded by rtsp://, and the port has been set to 8080. Click Play to start the stream. Here is our example rtsp address.

rtsp://raspberrypi:8080/

(Image credit: Tom’s Hardware)

It will take a few seconds for VLC to catch up with the stream, please be patient. Soon you will see the video streaming from your Raspberry Pi, with a delay of about 5-10 seconds.

6. To save a stream, click Play >> Record then specify a file name. The recording will end when you press the stop button.

(Image credit: Tom’s Hardware)

Go arty: stream with filters

The last part of this project is where we get creative. We will create a script that will first create an array (a list) of all possible image effects. Next, we create a variable to store the length of the array, before randomly picking a number from the array that will control what effect is used when our stream goes live. We will write the code on the Raspberry Pi using a graphical text editor.

1. Launch Geany (from the menu) and create a new file called random_stream.sh and remember to save often.

2. Enter the first line of code, which will tell the code where to find the Bash interpreter.

#!/bin/bash

3. Create an array to store all possible image effects in this project. There are 20 effects in total, and each has its own place in the array, allowing our code to choose a specific effect based on a random number.

array[0]="none"
array[1]="negative"
array[2]="solarise"
array[3]="sketch"
array[4]="denoise"
array[5]="emboss"
array[6]="oilpant"
array[7]="hatch"
array[8]="gpen"
array[9]="pastel"
array[10]="watercolour"
array[11]="film"
array[12]="blur"
array[13]="saturation"
array[14]="colourswap"
array[15]="washedout"
array[16]="posterise"
array[17]="colourpoint"
array[18]="colourbalance"
array[19]="cartoon"

4. Create a variable called size to store the number of effects in the array. This variable will store the output of a command (via {}) that checks the length of the array.

size=${#array[@]}

5. Create another variable, index, which will store a random number between zero and the length of the array, in this case 20.

index=$(($RANDOM % $size))

6. Print the chosen filter to the terminal, then wait a second.

echo ${array[$index]}
sleep 1

seven. Use raspivid to create an infinite stream at 800 x 600 resolution at 15 fps. The -ifx switch will be populated with a random effect from the array. To stream live video, this time we use a standard tcp stream. It works a bit faster than rstp but your stream may have artifacts. The Pi listens for (-l) connections from any local IP address.

raspivid -t 0 -w 800 -h 600 -ifx ${array[$index]} -fps 15 -l -o tcp://0.0.0.0:5000

8. Save the code in the /home/pi/ directory and quit the editor.

9. Open a terminal and use this command to make the code executable.

$ chmod +x random_stream.sh

ten. Run the code.

$ ./random_stream.sh

11. On your Windows/Mac/Linux computer open VLC.

12. Go to Media >> Open Network Streamor press CTRL+N.

(Image credit: Tom’s Hardware)

13. Enter the tcp address and the port of your Raspberry Pi. The tcp address is the hostname of the Raspberry Pi, prefixed with tcp/h264://, and the port set to 5000. Click Play to start the stream. Here is our example tcp address.

tcp/h264://raspberrypi.local:5000

(Image credit: Tom’s Hardware)

14. To save a stream, click Play >> Record then specify a file name. The recording will end when you press the stop button.

(Image credit: Tom’s Hardware)

15. To change effect, press CTRL+C in the Raspberry Pi terminal, then hit the UP key and enter to run the command again, hopefully with a different filter.

Complete list of codes

#!/bin/bash
array[0]="none"
array[1]="negative"
array[2]="solarise"
array[3]="sketch"
array[4]="denoise"
array[5]="emboss"
array[6]="oilpant"
array[7]="hatch"
array[8]="gpen"
array[9]="pastel"
array[10]="watercolour"
array[11]="film"
array[12]="blur"
array[13]="saturation"
array[14]="colourswap"
array[15]="washedout"
array[16]="posterise"
array[17]="colourpoint"
array[18]="colourbalance"
array[19]="cartoon"
size=${#array[@]}
index=$(($RANDOM % $size))
echo ${array[$index]}
sleep 1
raspivid -t 0 -w 800 -h 600 -ifx ${array[$index]} -fps 15 -l -o tcp://0.0.0.0:5000
Share.

Comments are closed.