Skip to content

Instantly share code, notes, and snippets.

@xorspark
Last active September 18, 2021 16:14
Show Gist options
  • Save xorspark/60e94d1baa860081734b4036bf582393 to your computer and use it in GitHub Desktop.
Save xorspark/60e94d1baa860081734b4036bf582393 to your computer and use it in GitHub Desktop.
Bash script to allow viewing twitch streams with mpv at the desired video encoding. Inspired by the twitch-view/twitch-format scripts but in pure bash instead of having a perl dependency.
#!/usr/bin/env bash
# Allows you to watch a twitch stream at the desired encoding rate
# using mpv + youtube-dl
twitch_name="$1"
if [[ -z "$twitch_name" ]]; then
printf "Error: twitch username required.\n"
exit 1
fi
declare -a formats
while read -r yt_output_line; do
read -d' ' -ra line_parts <<< "$yt_output_line"
if [[ "${line_parts[0]}" =~ ^[0-9] || "${line_parts[0]}" =~ ^audio ]]; then
formats+=( "${line_parts[0]}" )
fi
done < <(youtube-dl -F "https://twitch.tv/$twitch_name")
# twitch account does not exist/offline/banned? Bail.
if [[ "${#formats[@]}" == 0 ]]; then
exit 0
fi
formats+=("quit")
select format in "${formats[@]}"; do
if [[ -n "$format" ]]; then
if [[ "$format" == "quit" ]]; then
exit 0
else
break
fi
fi
done
mpv --ytdl-format="$format" "https://twitch.tv/$twitch_name"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment