Skip to content

How to do an endless video stream

I did an experiment for an idea I had for a side project.

My goal was to have YouTube live stream a video I already had recorded.

At the end of the video, the stream would start back from the start.

In case you’re wondering I just did the experiment and after seeing it work I said “works”, but I’m not actually using this. I also don’t guarantee it will work for you, just documenting what worked for me so next time I’ll reuse this code.

I guess this would work on Twitch as well but I haven’t tried so.. just a guess.

I started a live stream on YouTube and this gave me the stream key.

I had a video.mp4 video in a folder, and ran this shell script:

#!/bin/bash

KEY="the YT stream key" #add yours

ffmpeg -stream_loop -1 -i "video.mp4" -vcodec libx264 -pix_fmt yuv420p -preset medium -r 30-g $((30 * 2)) -b:v 2500k -acodec libmp3lame -ar 44100 -threads 6 -qscale 3 -b:a 712000 -bufsize 512k -f flv "rtmp://a.rtmp.youtube.com/live2/$KEY"

Those settings suppose the video is 720p, 30 frames per second.

Don’t ask me what all those settings do, I mixed some SO answers and tutorials to make it work.

Make sure you have the ffmpeg command line tool installed on your system.

This worked on my Mac, and also on a Ubuntu VPS I later used to test this so I could disconnect my Mac and let the VPS do the stream.


download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about :