RaspyStream
This is a simple-to-use tool to stream on Twitch or other streaming services from a Raspberry Pi and the new camera module v3. There are a lot of projects like this online the problem is that most of them use the old Raspivid which is not supported anymore, that's why I created this project adapting old programs to the new Libcam library.
It's composed of 3 shell scripts that can stream adding blank audio, with music, and with a microphone. if you have experience with the Raspberry Pi and streaming go to my GitHub there you will find all the code.
With this step-by-step guide, you don't need to know much to complete successfully even as a first raspberry project
You can use this in many situations like bird or plant watching, a podcast, or many things it can even be made portable for streaming outside.
Supplies
Materials:
- Raspberry pi 4b will work with older models but its recommended for the computing power
- Raspberry power supply
- Something to cool the pi like the case I use is recommended
- Pi camera v3 mine is the wide angle it comes with the cable
- SD card 16 Gb
- An Electrical box mine is 15 cm x 11 cm x 8 cm
- Plastic Flexible Coolant Pipe Hose
- Brass fittings 1/4" cap and female to female
- M4 screw washers and wingnut
- *Optional USB microphone
Tools:
- A computer I use a PC but mac or Linux will work to
- SD reader
- Drill and drill bits
- Screwdrivers
Hardware
First, we will attach the camera to the box to do this I use a Coolant pipe so I can position the camera freely.
a make a simple adapter out of steel but you can DIY something simple like gluing a camera case to the pipe.
The other end goes to a female-to-female 1/4" fitting and to a brass cap that I drill a hole to pass a screw that gets tightened through the plastic case with some washers and a wingnut.
Now the cable has to go thru the box this is done with a small slip made with a cuter and attaching the cable to both the raspberry and the camera.
Use the rubber grommets to pass the cables.
Software Get Your Raspberry Runing
I recommend using the Raspberry Pi Imager you can download it from the official website.
Once its download and installed insert your SD card and select the configuration of the picture
Then on the settings (gear icon) select a hostname, enable ssh, set a password, and set the wifi if you are going to connect it that way cable is preferred.
Once you set everything clicks on write and wait for it to finish and you will have your SD card ready.
Plug everything into your pi Camera, UBS mic, Ethernet, and lastly the power supply
Now its time to connect to the pi I use Putty to connect to the pi but you can use any SSH tool, under host put RaspyStream.local and open, it will ask for the user name and password use the one you write on the Pi imager before the defaults are user pi and password raspberry.
Last let's update everything so we don't have any problems later, write the command:
$ sudo apt update
$ sudo apt upgrade
Setup All the Packages
In this step, we will install all the necessary packages so everything runs smooth
First is FFmpeg which is used to mix the audio and video and stream it to twitch
$ sudo apt install -y ffmpeg
To test the installation write:
$ ffmpeg
And you should see something like this:
ffmpeg version 4.3.5-0+deb11u1+rpt3 Copyright (c) 2000-2022 the FFmpeg developers
You also need Screen, this will help us to run the streaming in the background, the next command will install it:
sudo apt install -y screen
First Stream
We will now prepare everything to make the first stream, since we will be sending a lot of files between the computer and the raspberry I recommend using WinSCP, which is a simple program that sends files over the internet you can download it here.
Now download the repository from GitHub, to do this directly to the pi and unzip it write the following command.
$ wget https://github.com/diegozalez/RaspyStream/archive/refs/heads/main.zip
$ unzip main.zip
Go inside the folder RaspyStream-main/
$ cd RaspyStream-main/
Here you will find the file keys.txt, and edit using nano.
$ nano keys.sh
There is a picture of what it should look like, moving with the arrows go to URL, and type your streaming URL, you can find it here. And for your twitch key go to your creator dashboard > Settings > Primary Stream Key, once you fill in the URL and key exit by pressing ctrl+X and Y to save the file.
For the .sh file to run you need to make it executable this can be done like this.
$ chmod u+x ScreenIt.sh
And we are ready to Stream!! just write.
$ ./ScreenIt.sh
After a few seconds, you will see the console telling you the frame rate and other information, go to your twitch channel and see you're self LIVEEEEEE!!!!!!
Tips
That was fun but at some point, you need to stop the stream, press ctrl + C to stop it and the screen will terminate too.
When you are running the stream you are using a screen session which means you can do other things while your stream is running, to exit the session pres ctrl+A+D, and you will now be able to see things like the pi Temperature with:
$ vcgencmd measure_temp
You can also see how the Pi's resources are with:
$ top
Exit with ctrl+C
To go back to your screen session type
$ screen -r RaspyStream
If you have any problem run directly
$ ./RaspyStreamMic.sh
or
$ ./RaspyStreamMusic.sh
or
$ ./RaspyStreamMuted.sh
This way you will see the error mesage
Turn Up the Music
Now that you have a stream running you might want to put music or a microphone. Let's start with the music.
The script RaspyStreamMusic.sh plays .wav files in the music folder to run it first upload all your wav files to the folder Music using WinSCP then modify the file Playlist.txt with nano and write the name of your files just like the example. It will be played in the order and you can repeat a file, once it finishes all it will start again, note that the default songs are blank.
Now just edit the ScreenIt.sh with nano:
$ nano ScreenIt.sh
Change where it says ./RaspyStreamMuted.sh to ./RaspyStreamMusic.sh and exit crtl+X and save.
You can now run
$ ./ScreenIt.sh
And your stream with music will start like before.
1, 2, Can You Hear Me?
You might want to stream a microphone too, for this first connect your microphone and verify the raspy detected as such. Write this command:
$ arecord -l
You will see something like this:
pi@RaspyStream:~/RaspyStream-main $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 3: U188 [U-188], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
Now edit the file RaspyStreamMic.sh with nano
$ nano RaspyStreamMic.sh
Go to the line where it says.
# Input_device is foud by typing $ arecord -l
# example: card 2: U188 [U-188], device 0: USB Audio [USB Audio]
InputA=3,0
And modify the InputA=<your card number>,<your device number>
Exit with crtl+X and save.
Edit the ./ScreenIt.sh script like in the music type but with ./RaspyStreamMic.sh
And run it with the same:
./ScreenIt.sh
That's it !!!
Improvements
Now it's your time to get creative, you can turn this into a movie stream by adding a power bank and a 4G modem or a light for dark environments, there is no limit, let me know if you come up with more ideas and I will love to see if you make this project.
Local RTMP Server
This is a great way to stream online but sometimes you want to test the stream locally or set it up privately, for this we can use MonaServer, which is a small rtmp server, this way we can stream from the Raspberry to a computer and from this computer or other access the stream. To do this we have to follow these steps:
- Prepare Monaserver:
- Download Monaserver2 Windows 64-bit (there are other versions for Mac or Linux here).
- Extract the "MonaServer_Win64.zip" on your desktop.
- Open the folder "MonaServer_Win64" We extract.
- Right-click on MonaServer.exe and execute as administrator.
- Allow access if Windows prompts a pop-up.
- Change the server on the Raspberry script
- Open the script RaspyStreamLocal.sh and on Ip write: "Ip=10.11.1.201" Remember to write the computer's IP address where you set up the MonaServer.
- Run the script:
$ ./RaspyStreamLocal.sh
- Connect the client to the server
- Download and install VLC on the computer you want to see the video feed, it can be the same as the Monaserver or other computers and phones with the VLC app in the same network.
- Once it's installed, opened it and go to Media>Open network stream.
- Write the server address on the network URL "rtmp://10.11.1.201/live" (Remember to change the IP address of your computer).
- And last press play.
Now you should see and hear your stream you can also do it on a cell phone with the VLC app the steps will be similar for Mac and Linux computers