2014-01-25

Bash script for Livestreamer to record twitch stream by schedule

 Install livesreamer from:

http://livestreamer.tanuki.se/en/latest/

Very very thankful for this tool, I search for something like this sense I hooked on StarCraft II!

To use script, just edit configuration part...  It's pretty self explanatory.
In my time zone, all GSL battles going when I'm at work.
This script allows me to record all and watch it when I come back home after work! :) 

I use livesreamer, not only for recording. Sorry, but Twitch page is lagging and I don't need other stuff, only video. And the best experience, in sense of avoiding video lagging or some sort of interrupts, is to stream to file and then watch it!
Also you can start streaming to file and watch from the file right away. Perhaps you should wait 10 or 15 seconds, this way player not going to catch the end of the file!


^^^^^^^^^^^^^^^^^^^^^^^^^^
#! /bin/bash
STREAM_URL="http://www.twitch.tv/gsl"
STREAM_QUALITY="best"
STREAMS_DUMP_DIR="/media/xbmc/Streams/"
STREAM_FILE_NAME="gsl_code_s_"

#                        month day year hour minutes
START_TIMESTAMP=$(date -d '10/04/2014 11:45' +%s)
END_TIMESTAMP=$(date -d '10/04/2014 19:00' +%s)
KILL_TIMESTAMP=$(date -d '10/04/2014 19:01' +%s)

^^^^^^^^^^^^^^^^^^^^^^^^^^

Bash version1




Bash version 2


It runs as a daemon. Just drop a simple text file to "shed" directory.
Text file should look like this:

^^^^^^^^^^^^^^^^^^^^^^^^^^
# twitch link
URL=http://www.twitch.tv/nathanias
#    Year Month Day
DATE=2014.12.23
#    Hours minutes
TIME=07.45
^^^^^^^^^^^^^^^^^^^^^^^^^^


Also set global configurations before start ("bash.conf"):


^^^^^^^^^^^^^^^^^^^^^^^^^^
# stream quality
QUALITY=best
# directory where steams will be saved
DIR=/media/8b58Volume830GiB/Streams/
# stream offline timeout to consider that stream is ended.
# at least one stream file should be created 
# in minutes
OFFLINE_TIMEOUT=20
^^^^^^^^^^^^^^^^^^^^^^^^^^




Extract archive and open terminal in "livestreamer-deamon" directory

$ ./livestreamer-deamon start


I run this on Raspberry-Pi. I can leave it online 24for7 it consumes ~5W of power and make no noise! It's environment friendly and wife approved! :)))

I added this to /etc/rc.local

^^^^^^^^^^^^^^^^^^^^^^^^^^
# livestreamer-deamon
cd /home/pi/wcs/
su pi /home/pi/wcs/livestreamer-deamon.bash start >> /dev/null 2>> /dev/null &
^^^^^^^^^^^^^^^^^^^^^^^^^^



 

Bash version 3


^^^^^^^^^^^^^^^^^^^^^^^^^^
# twitch link
URL=http://www.twitch.tv/nathanias
#    Year Month Day
DATE=2015.04.04
#    Hours minutes
TIME=19.34

#    Continue for ? days and record everything...
DAYS=3
#    log file
LOG=
^^^^^^^^^^^^^^^^^^^^^^^^^^
If days set to zero it will work as before.


Download :

livestreamer-bash


Enjoy!



16 comments:

  1. Thank you. Could you make another version where user can schedule recording in cronjob with parameters such as URL and duration?

    ReplyDelete
    Replies
    1. Jes, schedule part of the script can be removed and than you can add script to cronjob. URL can be passed as parameter. It would look like this:
      livestreamer-deamon.bash "http://www.twitch.tv/nathanias"
      Duration parameter I want to avoid. I expect that the script will work while the stream is online.
      Also break should be added, to exit loop when the job is done.

      Delete
  2. Hi Rolas,

    is there any chance to start the script and let it record as soon as a channel comes live? I would like to record a three day live-stream event but I dont know when exactly the stream will be going live.

    Any help on this one?

    ReplyDelete
    Replies
    1. Good idea! It could be useful for me as well.
      Actually I find a bug in my script. I constantly use this script for GSL and some times it moves config file to expired and does nothing. It happens because cache in /tmp/livestreamer-deamon/ not removed after stream recording is complete. I fix this one and then think about your idea.

      Delete
  3. Thank you very much :) By the way - i forgot to thank you for all your work here - it works like a charm and helps me tremendously already :) so - THANKS a LOT !

    ReplyDelete
  4. Hi

    First thank you for your hard work !!! and please excuse my bad english -.-

    In order to use your script as root the following lines have to be edited

    Line 719:

    livestreamer ${TMP_URL} ${CONF_QUALITY} -o ${CACHE_DUMP_FILE_PATH} >> std_${THIS_BASH_PID}.log 2> /dev/null &

    in

    livestreamer ${TMP_URL} ${CONF_QUALITY} -o ${CACHE_DUMP_FILE_PATH} --yes-run-as-root >> std_${THIS_BASH_PID}.log 2> /dev/null &

    Line 1046:

    STREAM_STATUS_STD=`livestreamer -j ${TMP_URL} ${CONF_QUALITY}`

    in

    STREAM_STATUS_STD=`livestreamer -j ${TMP_URL} ${CONF_QUALITY} --yes-run-as-root`

    A Channel-alive function would be great !!!

    Greetings Itow

    ReplyDelete
    Replies
    1. I can't think any good reason for running this script as root. Why do you need that?
      "Channel-alive function" - there is function_is_stream_online() if that's what you had in mind.
      P.S. I'm still hunting a few bugs...

      Delete
    2. I use a banana pi Pro with Bananian
      By default, there is only the root user

      For testing, I was too lazy to create a new user ...

      and so, I noticed that the script did not work as root

      Greetings Itow

      Delete
  5. Anonymous7/07/2015

    Hi Roales,

    thank you for all your work. Is there a way to add a --http-header X-Forwarded-For= and various streaming protocols directly. For example: hlsvariant:// or akamaihd://. One last question. Please update the link to your Bash version1 of your script. Thank you and have a good day!!

    Chris

    ReplyDelete
    Replies
    1. Hi Chris,
      Sorry I don't know answer to your question.
      This script only babysits the livesreamer pid.
      Quote from livesreamer help:
      "Stream types that can be converted into a playable URL are:
      - hls
      - http
      - rtmp"

      Delete
  6. Thank you for your work. This is what I exactly need. I would appreciate if the very first variant of livestreamer.bash would be available again (current published link is broken) as I need those options as START_TIMESTAMP, END_TIMESTAMP, KILL_TIMESTAMP. Thank you in advance.
    Val

    ReplyDelete
  7. I fix the link for version 1. Hope it will be useful.

    ReplyDelete
  8. Rolas, thanks for your prompt reply.
    Val

    ReplyDelete
  9. Hello Rolas,

    the links are no longer available, do you think you can reupload them?

    thank you

    ReplyDelete
    Replies
    1. Sorry, my raspberry pi cloud right now under constructions. I try to make it available this weekend.

      Delete