Skip to content

Instantly share code, notes, and snippets.

@secemp9
Created July 13, 2024 16:29
Show Gist options
  • Save secemp9/380a2e3b9d5c873e5621d871418eeca1 to your computer and use it in GitHub Desktop.
Save secemp9/380a2e3b9d5c873e5621d871418eeca1 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set up variables
YOUTUBE_URL="rtmp://a.rtmp.youtube.com/live2/KEY"
TWITTER_URL="rtmp://va.pscp.tv:80/x/KEY"
TWITCH_URL="rtmp://live.twitch.tv/app/KEY"
YOUTUBE_BITRATE="9M"
TWITTER_BITRATE="9M"
TWITCH_BITRATE="7M"
LOCAL_RTMP="rtmp://0.0.0.0:1935"
# Set up named pipe
PIPE_PATH="/tmp/live_stream.pipe"
# Create named pipe
[ -e "$PIPE_PATH" ] && rm "$PIPE_PATH"
mkfifo "$PIPE_PATH"
# Start the input FFmpeg process
ffmpeg -listen 1 -i "$LOCAL_RTMP" -c copy -f mpegts "$PIPE_PATH" &
# Function to start an output FFmpeg process
start_output() {
local platform=$1
local url=$2
local bitrate=$3
while true; do
ffmpeg -i "$PIPE_PATH" \
-c:v libx264 -preset veryfast -tune zerolatency \
-b:v "$bitrate" -maxrate "$bitrate" -bufsize 3000k \
-g 60 -flags +global_header \
-c:a copy \
-f flv "$url"
echo "Restarting $platform stream..."
sleep 5
done
}
# Start output processes
start_output "YouTube" "$YOUTUBE_URL" "$YOUTUBE_BITRATE" &
start_output "Twitter" "$TWITTER_URL" "$TWITTER_BITRATE" &
start_output "Twitch" "$TWITCH_URL" "$TWITCH_BITRATE" &
# Wait for all background processes
wait
# Clean up the named pipe when the script exits
trap 'rm -f "$PIPE_PATH"' EXIT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment