Skip to content

Instantly share code, notes, and snippets.

@alsolovyev
Last active September 9, 2024 06:09
Show Gist options
  • Save alsolovyev/3ee1c8a62b224abe9874ef23865b169d to your computer and use it in GitHub Desktop.
Save alsolovyev/3ee1c8a62b224abe9874ef23865b169d to your computer and use it in GitHub Desktop.
This Fish shell script simplifies the process of recording Twitch streams by using Streamlink
function rt --description "Record a Twitch stream to a specified file."
set -l app "streamlink"
# Ensure the app is installed
if not command -v $app > /dev/null
echo (set_color red)"Error: '$app' is not installed."(set_color normal)
switch $app
case "streamlink"
if command -v brew > /dev/null
echo (set_color yellow)"You can install 'streamlink' using Homebrew by running:"(set_color normal)
echo "brew install streamlink"
else
echo (set_color yellow)"Please install 'streamlink' manually. Visit the official website for installation instructions:"(set_color normal)
echo "https://streamlink.github.io/install.html"
end
case "*"
echo (set_color yellow)"Please install '$app' manually"(set_color normal)
end
return 1
end
# Parse command-line arguments
argparse --name=rt "h/help" "o/output=" "d/delay=" "q/quality=" "D/debug" -- $argv
or return
# Display usage message if help flag is passed
if set -q _flag_help
echo "Usage: rt [options] <streamer_name>"
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -o, --output FILE Specify the output file or directory"
echo " -d, --delay SEC Retry stream for the specified delay in seconds"
echo " -q, --quality QUAL Specify the stream quality (default: best)"
echo " -D, --debug Show the streamlink command that would be executed, but don't run it"
return 0
end
# Set default options and variable
set -l streamlink_options "--twitch-disable-ads"
set -l streamer_name $argv[1]
set -l quality "best"
# Validate streamer name
if test -z "$streamer_name"
echo (set_color red)"Error: Streamer name is required."(set_color normal)
return 1
end
# Set default quality or use specified one
if set -q _flag_quality
set quality $_flag_quality
end
# Handle delay flag if specified
if set -q _flag_delay
set -a streamlink_options "--retry-streams=$_flag_delay"
end
# Handle output flag if specified, else use default naming convention
if set -q _flag_output
set -a streamlink_options "--output=$_flag_output"
else
set -a streamlink_options "--output=~/Movies/{author}-{time:%d%m%y}.mp4"
end
# Add any additional arguments to streamlink options
if test (count $argv) -gt 1
set -a streamlink_options $argv[2..-1]
end
# Execute the streamlink command
if set -q _flag_debug
echo "Executing: $app $streamlink_options twitch.tv/$streamer_name $quality"
else
$app $streamlink_options "twitch.tv/$streamer_name" $quality
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment