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 change how the sourcecode is displayed I recommend to check it here : )


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 :

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://

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(

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

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

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¬†I¬†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