Last active
April 6, 2026 23:17
-
-
Save AfroThundr3007730/d933ad982036aae9239e553eca5e46ef to your computer and use it in GitHub Desktop.
Save Twitch live streams via streamlink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Save Twitch live streams via streamlink | |
| set -euo pipefail | |
| shopt -s extdebug | |
| declare -gr \ | |
| BASEDIR='/mnt/pool0/media/archive/twitch' \ | |
| BASEURL='https://api.twitch.tv/helix/streams' \ | |
| CLIENT="${CLIENT:-}" \ | |
| TOKEN="${TOKEN:-}" \ | |
| SLEEP=20 \ | |
| USER="${1}" \ | |
| FILTER='.data[] | { | |
| id: .id, | |
| author: .user_name, | |
| category: .game_name, | |
| title: .title | |
| }' | |
| utils.sleep() { | |
| (($# == 1)) || return | |
| [[ ${_sleep_fd:-} ]] || { exec {_sleep_fd}<> <(:) && utils.sleep 0; } | |
| read -r -t "${1#*-}" -u "$_sleep_fd" || : | |
| } | |
| streamlink_save() { | |
| (($# == 2)) || return | |
| systemd-run --user --scope --slice-inherit -u streamlink-$$ \ | |
| nice -n 19 ionice -c 3 /usr/bin/streamlink -o "${1}" "${2}" best & | |
| sudo -i systemd-inhibit --who=streamlink-$$ --why="VOD recording: ${2}" \ | |
| pidwait -ns $$ streamlink | |
| } | |
| get_stream_info() { | |
| (($# == 1)) || return | |
| curl -fsS \ | |
| -H 'Accept: application/vnd.twitchtv.v5+json' \ | |
| -H "Authorization: Bearer ${TOKEN}" \ | |
| -H "Client-ID: ${CLIENT}" \ | |
| "${BASEURL}?user_login=${USER}" | jq "${FILTER}" >"${1}" | |
| } | |
| main() { | |
| local stamp file json | |
| [[ ${CLIENT:-} && ${TOKEN:-} ]] || | |
| { echo 'Missing client credentials.' && exit 1; } | |
| while utils.sleep "${SLEEP}"; do | |
| [[ -d ${BASEDIR} ]] || continue | |
| streamlink "twitch.tv/${USER}" &>/dev/null && { | |
| printf -v stamp '%(%Y-%m-%d_%H-%M-%S)T' | |
| json=${BASEDIR}/${USER}/${USER}_${stamp}r.json | |
| file=${BASEDIR}/${USER}/${USER}_${stamp}r.m2t | |
| [[ -d ${file%/*} ]] || mkdir -p "${file%/*}" | |
| get_stream_info "${json}" | |
| streamlink_save "${file}" "twitch.tv/${USER}" | |
| } | |
| done | |
| } | |
| [[ ${BASH_SOURCE[0]} == "$0" ]] && main "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ~/.config/systemd/user/streamlink-save@.service | |
| [Unit] | |
| Description=Auto start Streamlink for Twitch channel %I | |
| RequiresMountsFor=/mnt/pool0/media/archive/twitch | |
| [Service] | |
| Type=simple | |
| EnvironmentFile=-%h/.config/systemd/user/streamlink-save.env | |
| ExecStart=%h/.local/bin/streamlink-save %I | |
| Restart=on-failure | |
| [Install] | |
| WantedBy=default.target |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
user:read:followsscope (try this site).CLIENTandTOKENvariables via the environment file listed in the service.systemctl --user enable --now streamlink-save@USER