Last active
July 19, 2021 12:41
-
-
Save jim60105/983bd6b7362bd35fbba6f71d7ac51721 to your computer and use it in GitHub Desktop.
test script for live-dl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.