Optimized YouTube 24/7 streaming script that reduces costs from ~$2 to ~$0.20 by eliminating unnecessary video processing.
-
Smart Download (
yt-dlp): Forces YouTube to provide a 1080p H.264 file specifically, ensuring the video is already in the perfect format for streaming. -
Direct Stream Copy (
ffmpeg): Uses-c copyto remove all video processing. Instead of "reading, decoding, resizing, re-encoding, and sending," it now just "reads and sends," reducing CPU usage by ~99%.
- Python 3.7+
- FFmpeg installed and available in your PATH
- YouTube stream key
- YouTube video ID to stream
-
Install Python dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the project root with your credentials:STREAM_KEY=your_youtube_stream_key_here YouTube_ID=your_youtube_video_id_here -
Run the stream:
python stream.py
-
Build the Docker image:
docker build -t youtube-stream . -
Run the container with environment variables:
docker run -d --name youtube-stream \ -e STREAM_KEY=your_youtube_stream_key_here \ -e YouTube_ID=your_youtube_video_id_here \ youtube-stream
Or use a
.envfile (make sure it's in the same directory):docker run -d --name youtube-stream \ --env-file .env \ youtube-stream
-
View logs:
docker logs -f youtube-stream
-
Stop the container:
docker stop youtube-stream docker rm youtube-stream
The script downloads a YouTube video in the exact format needed (1080p H.264 MP4) and streams it directly to YouTube's RTMP server using stream copy mode, which requires minimal CPU resources since no re-encoding occurs.
- The video will loop infinitely
- Press
Ctrl+Cto stop the stream (ordocker stopif using Docker) - Ensure FFmpeg is installed:
ffmpeg -version(included in Docker image)


