Hakkin / twitchpipe

Pipe your favorite Twitch streams to the media player of your choice, or a file to save them for later. Supports low-latency playback.
MIT License
35 stars 5 forks source link

Added support for stream titles/custom date format in filename #6

Open 11ib opened 4 years ago

11ib commented 4 years ago

I've added the -d and -t flags for inserting a custom date format (using the Bash Date Format) and grabbing the title of a stream when recording a stream.

camjac251 commented 4 years ago

I've seen another go project deal with filenames by converting it to utf-8 It might be useful here https://github.com/uhthomas/kipp/blob/e8189beaa3e4ae20366890cfb45e0f57bf86678b/server.go#L132

camjac251 commented 4 years ago

Having the filename include the title of the stream might not be ideal for archiving. A lot of times, a streamer will set their title after going live so you'd have some duplicates. I've opened an issue with a feature request for a logging format and think logging the titles separately could be better than filenames. #12

Hakkin commented 3 years ago

Hi @11ib, would you mind checking to see if the new record.sh -f option in the master branch does what you want? You can define any program to run when the stream starts, the output of that program (standard output) will be used as the output filename. You can use the variables $username and $id in the command and they will be expanded and passed to the script. To avoid your shell trying to expand the variables itself, you should make sure to put the -f argument in single quotes (') and not double quotes ("). If you want some examples to test, I've tried these and confirmed they're working:

./record.sh -f 'until youtube-dl --get-filename -o "%(uploader)s (%(uploader_id)s) - %(upload_date)s (%(timestamp)d) - %(id)s - %(description)s" "https://twitch.tv/${username}"; do continue; done' username1 username2

this is using a bash until loop to call youtube-dl and request a formatted filename, it will usually give a few "stream is offline" errors until it works. This will get you a filename formatted like displayName (username) - 20201202 (1606896000) - 40000000000 - This is a title.ts You can use whatever youtube-dl template options you want.

You can also of course write custom bash scripts to fetch whatever info you want, or any other commands for that matter, I wrote a simple bash script that does similar to the above without using youtube-dl.

#!/bin/bash

id="${1}"
json="$(curl --silent --fail -H "Accept: application/vnd.twitchtv.v5+json" -H "Client-ID: jzkbprff40iqj646a697cyrvl0zt2m6" "https://api.twitch.tv/kraken/streams/${id}")"
stream="$(jq .stream <<< "${json}")"
if [ "${stream}" == "null" ]
then
    sleep 1
    exit 1
fi

display_name="$(jq -r .channel.display_name <<< "${stream}")"
user_id="$(jq -r .channel._id <<< "${stream}")"
created_at="$(jq -r .created_at <<< "${stream}")"
broadcast_id="$(jq -r ._id <<< "${stream}")"
status="$(jq -r .channel.status <<< "${stream}")"

printf "%s - %s (%d) - %s (%d)" "$(date -u -d "${created_at}" "+%Y%m%d%H%M%S")" "${display_name}" "${user_id}" "${status}" "${broadcast_id}"

put this in a file called filename.sh and chmod +x it, then it can be used like so: ./record.sh -f 'until ./filename.sh "${id}"; do continue; done' username1 username2 it will output to a filename like this: 20201202000000 - displayName (userID) - This is a title (broadcastID).ts