Skip to content

Instantly share code, notes, and snippets.

@markusa380
Last active August 18, 2019 18:51
Show Gist options
  • Select an option

  • Save markusa380/c2ad7994ab999656604f5b31b5bb757b to your computer and use it in GitHub Desktop.

Select an option

Save markusa380/c2ad7994ab999656604f5b31b5bb757b to your computer and use it in GitHub Desktop.
Streaming a webcam on Linux to a YouTube livestream using a key stored in an AWS S3 bucket
#! /bin/bash
# This script requires the packages "aws-cli" and "ffmpeg".
# For Raspberry Pi, the package "rng-tools" should also be installed.
# The path of the webcam
webcam=/dev/video0
# The S3 bucket in which the key is stored
key_bucket=markus.appel.private
# The path of the key inside the S3 bucket
key_path=youtube_key
# The local path where the key is stored temporarily
local_key="~/youtube_key"
# Delete the old local key for good measure
rm -f $local_key
# Copy the key from S3
aws s3 cp s3://$key_bucket/$key_path $local_key
# Load the contents of the key file into a variable
key=$(cat $local_key)
# Run the command in a loop in case of network issues
while :
do
# Run ffmpeg to stream data from the configured webcam to the configured YouTube livestream
ffmpeg -thread_queue_size 512 -f v4l2 -i $webcam \
-ar 44100 -ac 2 -acodec pcm_s16le -f s16le -ac 2 -i /dev/zero -acodec aac -ab 128k -strict experimental \
-aspect 16:9 -vcodec h264 -preset veryfast -crf 25 -pix_fmt yuv420p -g 15 -vb 820k -maxrate 820k -bufsize 820k -profile:v baseline \
-r 15 -f flv "rtmp://a.rtmp.youtube.com/live2/$key"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment