Skip to content

Scripting with Moonbeam

You can create shell scripts that use Moonbeam to automate various aspects of your streaming setup. Here are several creative examples:

Night Mode Streaming

night_mode__streaming.sh
#!/usr/bin/env -S nix shell nixpkgs#bash nixpkgs#coreutils nixpkgs#brightnessctl nixpkgs#redshift nixpkgs#moonbeam -c bash
current_hour=$(date +%H)
if (( current_hour >= 22 || current_hour < 6 )); then
# Night mode
# Lower brightness
brightnessctl set 50%
# Warmer colors
redshift -O 3500
moonbeam stream <HOST> <APP> --max-bandwidth 5000
else
moonbeam stream <HOST> <APP>
fi

This script adjusts streaming settings based on the time of day, potentially reducing eye strain during night hours and limiting the bitrate to 5000 kbps during nighttime streaming.

Multi-game launcher

multi_game_launcher.sh
#!/usr/bin/env -S nix shell nixpkgs#bash nixpkgs#coreutils nixpkgs#moonbeam -c bash
games=("Game 1" "Game 2" "Game 3" "Game 4")
echo "Select a game to launch:"
select game in "${games[@]}"; do
case $game in
"Game 1") moonbeam stream <HOST> "Game 1" --priority resolution ;;
"Game 2") moonbeam stream <HOST> "Game 2" --priority fps --max-fps 120 ;;
"Game 3") moonbeam stream <HOST> "Game 3" --max-resolution 4K --fps 60 ;;
"Game 4") moonbeam stream <HOST> "Game 4" ;;
*) echo "Invalid selection" ;;
esac
break
done

This script presents a menu of games, each with customized streaming settings.

Conditional Streaming Settings

adaptive-moonbeam.sh
#!/usr/bin/env -S nix shell nixpkgs#bash nixpkgs#coreutils nixpkgs#moonbeam -c bash
HOST="my-gaming-pc"
APP="$1"
# Check if we're on battery power
on_battery() {
cat /sys/class/power_supply/BAT0/status | grep -q "Discharging"
}
# Set streaming options based on power status
if on_battery; then
OPTIONS="--max-resolution 720p --max-fps 30 --max-bitrate 5000"
else
OPTIONS="--max-resolution 1080p --max-fps 60 --max-bitrate 20000"
fi
moonbeam stream "$HOST" "$APP" $OPTIONS

This script adjusts streaming quality based on whether the device is running on battery power.

Advanced Integration Examples

Here are some more advanced integration examples that showcase Moonbeam’s versatility:

Game Streaming Session Time Tracker and Limiter

This script uses the timewarrior time tracking tool to monitor your gaming sessions and limit them to a specified duration.

game_session_tracker.sh
#!/usr/bin/env -S nix shell nixpkgs#bash nixpkgs#coreutils nixpkgs#moonbeam nixpkgs#timewarrior -c bash
MAX_SESSION_TIME=120 # Maximum session time in minutes
start_time=$(date +%s)
# Start time tracking
timew start "Gaming Session"
# Start the game stream
moonbeam stream <HOST> <APP> &
pid=$!
while true; do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ $elapsed_time -ge $((MAX_SESSION_TIME * 60)) ]; then
echo "Session time limit reached. Stopping the stream."
kill $pid
break
fi
sleep 60 # Check every minute
done
# Stop time tracking
timew stop
echo "Gaming session ended. Total time: $(timew summary)"

This script will start a Moonbeam stream, track the session time using timewarrior, and automatically stop the stream after the specified time limit.

Moonbeam Integration with Home Automation Systems

This example integrates Moonbeam with Home Assistant, a popular home automation platform, to control your gaming environment.

gaming_environment.sh
#!/usr/bin/env -S nix shell nixpkgs#bash nixpkgs#coreutils nixpkgs#moonbeam -c bash
HASS_URL="http://homeassistant.local:8123"
HASS_TOKEN="your_long_lived_access_token"
# Function to call Home Assistant API
call_hass_service() {
curl -X POST -H "Authorization: Bearer $HASS_TOKEN" \
-H "Content-Type: application/json" \
-d "$2" \
"$HASS_URL/api/services/$1"
}
# Set up gaming environment
call_hass_service "light/turn_on" '{"entity_id": "light.gaming_room", "brightness": 128, "rgb_color": [255,0,0]}'
call_hass_service "climate/set_temperature" '{"entity_id": "climate.gaming_room", "temperature": 22}'
# Start the game stream
moonbeam stream <HOST> <APP>
# Reset environment after gaming
call_hass_service "light/turn_on" '{"entity_id": "light.gaming_room", "brightness": 255, "rgb_color": [255,255,255]}'
call_hass_service "climate/set_temperature" '{"entity_id": "climate.gaming_room", "temperature": 24}'

This script adjusts your room’s lighting and temperature before starting a game stream, and resets them after you’re done gaming.