Streaming From Raspberry Pi to YouTube

It means camera is on and streaming only if you've opened a page with stream preview. It saves to camera and reduces CPU usage a lot.

Streaming from Raspberry Pi to YouTube only when someone is watching

(because Hackster.io change how the sourcecode is displayed I recommend to check it here : https://remoteme.org/youtube-srteaming/ )

Introduction

I've used platform RemoteMe (I'm the author of this platform ) it's hosting your files for free, and helps to communicate with ESP. So first please create an account at : https://app.remoteMe.org.

In this project I will show how to stream an video from a USB camera connected to Raspberry Pi to YouTube. Streaming will not be continuous – the camera will be turned on and streamed to YouTube only when someone opens a page with a YouTube window.

What will be needed

Raspberry Pi – I use RPI4 with 4GB Ramu, I also tested on RPI3 (below screen with Rpi4 htop when streaming video with a resolution of 1280x720 as you can see processor cores are used in 36% at one core)

Heatsink for Raspberry Pi CPU – streaming can make Raspberry Pi hot, therefore heatsink is needed, I use Flirc Raspberry Pi 4 Case (Silver)

  • USB camera – I use the A4Tech PK-910H model – before buying the camera, please check if it will work with Rasbian
  • it is an aluminum box that also acts as a heat sink. When using it, the processor temperature is around 45C at an outside temperature of 24C. More about this box on the Mr. Andreas Spiess channel, the only caveat I have for this box is that there is no output for the belt from the camera connected directly to the RPI board, and to output PINS with a 40-pin belt you need to saw the sides of the tape plug

How it will work

To start stream the following command will be run:

ffmpeg -threads 0 -f v4l2 -i /dev/video0 -ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -acodec aac -ab 128k -strict experimental -s 1280x720 -b 6000000 -aspect 16:9 -vcodec h264_omx -vb 820k -pix_fmt yuv420p -g 60 -r 30 -f flv rtmp://a.rtmp.youtube.com/live2/SECRET_KEY

of course you must install ffmpeg before if you don’t have one(sudo apt-get install ffmpeg )

the script will be run when our website with a YouTube frame will be displayed by at least one person, when the page is closed and not re-opened within 10 seconds, the script will be ‘killed’ the webcam will turn off and will not stream to YouTube ‘ anymore.

Because we will actually create a new stream each time, iframe with the stream address must point not to a specific stream but to the active stream of the given user, i.e.

Because of that our streams has to be public

Let’s do this 🙂

in fact, it takes a few clicks to start a project – because this project is already in the quick projects tab(https://app.remoteme.org/en/#/app/quickstart)

click “Read More or Build It” on the project:

then in  Build It tab:

we filled YouTube stream name / key, which we download from YouTube at

it will have a form similar to: 7awp-w65c-zbas-61c9 - however, you can enter anything and then complete it with the correct secret key in the file youtuberun.sh

When we have not streamed anything before, we need to activate the service – activation takes up to 24h only then we will be able to start streaming

Channel id is taken from the address https://www.youtube.com/account_advanced

in my case it’s  ZCPuP7AKof_poZY664N4yJCA

after filled

then click Next step – choose or add Raspberry Pi (in case of problems please refer to the article about Raspberry Pi in RemoteMe python devcies in RemoteMe ), then next step and the green “build the project” button and after successfully creating the project we close the window.

What happened

A page has been created, a python script has been added:

we can open the page to see if everything works:

 

Source: Streaming From Raspberry Pi to YouTube


About The Author

Muhammad Bilal

I am highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top