Skip to content

Instantly share code, notes, and snippets.

@jim60105
Last active July 19, 2021 12:41
Show Gist options
  • Save jim60105/983bd6b7362bd35fbba6f71d7ac51721 to your computer and use it in GitHub Desktop.
Save jim60105/983bd6b7362bd35fbba6f71d7ac51721 to your computer and use it in GitHub Desktop.
test script for live-dl
#!/bin/bash
_url='https://www.youtube.com/channel/UCBC7vYFNQoGPupe5NxPG4Bw/live'
# _url='https://www.youtube.com/watch?v=Y-EVxJhfsfE'
USER_AGENT="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
_body=$(curl -s --compressed -H "User-Agent: $USER_AGENT" -b cookies.txt "$_url")
# Extract metadata from fetched page
METADATA_CURL_RAW=`echo "$_body" | grep -a 'ytInitialPlayerResponse' | grep -a '<script' | grep -a 'responseContext'`
# echo $METADATA_CURL_RAW
# Match only JSON part
# https://regex101.com/r/QiyZE2/3
#
# Debug commmand if YouTube changes its HTML structure in the future:
# $ curl -s --compressed -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" 'https://www.youtube.com/watch?v=j2z06YUbulk' | grep 'ytInitialPlayerResponse' | grep '<script' | grep 'responseContext' | perl -pe 's/^.*?ytInitialPlayerResponse = ({".*]});var .*/\1/g' | jq -r .
METADATA_CURL=`echo "$METADATA_CURL_RAW" | perl -pe 's/^.*?ytInitialPlayerResponse = (\{".*});var meta.*/\1/g' | grep -av '<script' | jq -r .`
echo $METADATA_CURL
# Get some metadata first, I will retrieve more later using youtube-dl
IS_LIVE=$(echo "$METADATA_CURL" | jq -r '.videoDetails.isLive | select(.!=null)')
VIDEO_ID=$(echo "$METADATA_CURL" | jq -r '.videoDetails.videoId')
FULLTITLE=$(echo "$METADATA_CURL" | jq -r '.videoDetails.title')
DESCRIPTION=$(echo "$METADATA_CURL" | jq -r '.videoDetails.shortDescription')
UPLOADER=$(echo "$METADATA_CURL" | jq -r '.videoDetails.author')
CHANNEL_ID=$(echo "$METADATA_CURL" | jq -r '.videoDetails.channelId')
echo $VIDEO_ID
echo $FULLTITLE
echo $CHANNEL_ID
@jim60105
Copy link
Author

@jim60105
Copy link
Author

jim60105 commented Jan 7, 2021

Update code from sparanoid/live-dl@d63f410

The origin code is not works with a cookies file. So I modify #L21 perl regex to let it works.
Please note that if no cookie file is passed in, it will not work properly.

@jim60105
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment