Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

11ib
Copy link

@11ib 11ib commented Dec 2, 2019

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.

Copy link
Owner

@Hakkin Hakkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request! This looks good, but a few things need fixing.

Mostly small changes needed, but sanitizing filenames is a big one. I'm not quite sure what the best way to do this in bash is, the simplest way would probably be to nuke all non-alphanumeric characters in the filename, but that also might be slightly overkill.

@@ -41,7 +47,7 @@ invalid_input() {

check_deps

while getopts ":ph" opt
while getopts "d:pht" opt
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while getopts "d:pht" opt
while getopts ":phtd:" opt

This should be ":phtd:", ":" at the start of a getopt argument prevents it from displaying debug errors.

get_stream_title () {
curl --silent --fail -H "Client-ID: $CLIENT_ID" "$API_URL$1" | jq -r .status
}

print_usage() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new switches should be added to the usage text.

if [ "$FILENAME_TITLE" == "1" ];
then
title=$(get_stream_title $username)
filename+="${title}_"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to sanitize the filename somehow, as it is, if a streamer has their title set to ../title, the file will be created up a directory. We also have to consider special characters that are not allowed in filenames, both on Windows and Linux.

then
filename+=$(date $DFORMAT)
else
filename+=$(date -u '+%Y-%m-%d-%H-%M-%S-(%Z)')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filename+=$(date -u '+%Y-%m-%d-%H-%M-%S-(%Z)')
filename+=$(date -u '+%Y_%m_%d_%H_%M_%S_(%Z)')

I would also prefer the date format stay the same as it was before, +%Y_%m_%d_%H_%M_%S_(%Z)

@camjac251
Copy link

camjac251 commented Jun 14, 2020

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
Copy link

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
Copy link
Owner

Hakkin commented Dec 2, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants