Quick and dirty script to get information about youtube videos and their URL (valid in october 2019)
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
!/usr/bin/env python3 | |
# Libraries | |
import re | |
import sys | |
import json | |
import urllib.request | |
from urllib.parse import parse_qs | |
# Get video id from command line | |
if len(sys.argv) != 2: | |
print("Usage: {} <video_id>".format(sys.argv[0])) | |
exit(1) | |
vid = sys.argv[1] | |
# Format number of bytes in human-readable form | |
def sizeof_fmt(num, suffix='B'): | |
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: | |
if abs(num) < 1024.0: | |
return "%3.1f%s%s" % (num, unit, suffix) | |
num /= 1024.0 | |
return "%.1f%s%s" % (num, 'Yi', suffix) | |
# Load video infos from server (use cache for tests) | |
page_txt = '' | |
try: | |
with open('videinfo_page.txt') as cache: | |
page_txt = cache.read() | |
except IOError: | |
print("Cache not found, download") | |
url = 'https://youtube.com/get_video_info?video_id=' + vid | |
user_agent = 'Mozilla/5.0 (X11; FreeBSD amd64; rv:40.0) Gecko/20100101 Firefox/40.0' | |
req = urllib.request.Request(url, None, {'User-Agent': user_agent}) | |
with urllib.request.urlopen(req) as response: | |
page_txt = response.read().decode("utf-8") | |
with open('videinfo_page.txt', 'w') as cache: | |
cache.write(page_txt) | |
# Parse interesting parts | |
pr = json.loads(parse_qs(page_txt)['player_response'][0]) | |
formats = pr['streamingData']['adaptiveFormats'] + pr['streamingData']['formats'] | |
# Sort formats | |
def fmt_sortkey(s): | |
k = re.split('(\d+)', s['qualityLabel'] if 'qualityLabel' in s else s['quality']) | |
if k[0] == '': | |
k[0] = 1 | |
k[1] = int(k[1]) | |
else: | |
k = [0, k[0]] | |
return k | |
formats = sorted(formats, key=fmt_sortkey, reverse=True) | |
# There are a lot of useful information that may be shown in pr['videoDetails'] Let's show the title. | |
print(pr['videoDetails']['title']) | |
# Display found videos and set useful name | |
fmtlst = [] | |
for f in formats: | |
name = f['quality'] | |
if 'qualityLabel' in f: | |
name = f['qualityLabel'] | |
if 'mimeType' in f: | |
name += ' ' + f['mimeType'].split(';')[0] | |
if 'contentLength' in f: | |
name += ' ' + sizeof_fmt(int(f['contentLength']), 'B') | |
print(name) | |
print('=' * len(name)) | |
print(f['url']) | |
print('') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment