Skip to content

Instantly share code, notes, and snippets.

@szepeviktor
Last active November 5, 2023 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szepeviktor/a09ebd63d465a5502460e0e2904e9562 to your computer and use it in GitHub Desktop.
Save szepeviktor/a09ebd63d465a5502460e0e2904e9562 to your computer and use it in GitHub Desktop.
Get YouTube download URL-s of video and audio
#!/bin/bash
# Save HTML
wget -O watch.html "https://www.youtube.com/watch?v=VIDEO_ID"
# Extract player data
sed -n -e 's#.*var ytInitialPlayerResponse = \({[^<]\+}\);</script>.*#\1#p' <watch.html >video.json
# For HTML from the browser
#sed -n -e 's#.*var ytInitialPlayerResponse = \({[^<]\+}\);var meta = .*#\1#p' <watch.html >video.json
# Get URL for best video format
jq -r '."streamingData"."adaptiveFormats"[] | select(."itag" == 298) | ."url"' video.json
#jq -r '."streamingData"."adaptiveFormats"[] | select(."itag" == 137) | ."url"' video.json
# Get URL for best audio format
jq -r '."streamingData"."adaptiveFormats"[] | select(."itag" == 140) | ."url"' video.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment