Skip to content

Instantly share code, notes, and snippets.

@squaresmile
Last active October 3, 2021 15:34
Show Gist options
  • Select an option

  • Save squaresmile/7395dec4cbcaec55b939464f9e2d8df2 to your computer and use it in GitHub Desktop.

Select an option

Save squaresmile/7395dec4cbcaec55b939464f9e2d8df2 to your computer and use it in GitHub Desktop.

Revisions

  1. squaresmile renamed this gist Oct 3, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. squaresmile created this gist Oct 3, 2021.
    11 changes: 11 additions & 0 deletions save_stream.service
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    [Unit]
    Description=Save Twitch stream
    After=network.target

    [Service]
    WorkingDirectory=/home/squaresmile/twitch_streams
    ExecStart=/bin/bash /home/squaresmile/twitch_streams/save_stream.sh
    User=cereal

    [Install]
    WantedBy=multi-user.target
    10 changes: 10 additions & 0 deletions save_stream.timer
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    [Unit]
    Description=Check and save stream

    [Timer]
    OnCalendar=*-*-* *:00/15:00
    RandomizedDelaySec=60
    AccuracySec=1us

    [Install]
    WantedBy=timers.target
    22 changes: 22 additions & 0 deletions streamlink_save.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    #!/usr/bin/env bash

    set -e

    if [[ $(find . -name "*_.mp4" -printf '.' | wc -m) -ne "0" ]]; then
    exit 1
    fi

    STREAM=$(date +"%Y_%m_%d_%H_%M_%S")
    echo "Saving stream $STREAM"
    streamlink https://www.twitch.tv/<twitch_username> -o "$STREAM"_.mp4 --twitch-disable-hosting \
    --twitch-disable-ads --twitch-disable-reruns --retry-streams 60 --retry-max 5 || true

    if [ -f "$STREAM"_.mp4 ]; then
    # Some players (Chrome) can't play the original mp4 file from streamlink (probably because when the stream ends, strealink doesn't finish writing cleanly)
    # so we remux the file
    ffmpeg -i "$STREAM"_.mp4 -c: copy "$STREAM".mp4
    rm "$STREAM"_.mp4

    # Uncomment if you want to send a pushbullet notification on finish
    # curl -u <pushbullet_api_token>: https://api.pushbullet.com/v2/pushes -d type=note --data-urlencode title="$STREAM stream finished"
    fi