Skip to content

Instantly share code, notes, and snippets.

Last active October 15, 2019 18:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Quick and dirty script to get information about youtube videos and their URL (valid in october 2019)
!/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]))
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 = ''
with open('videinfo_page.txt') as cache:
page_txt =
except IOError:
print("Cache not found, download")
url = '' + 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 ="utf-8")
with open('videinfo_page.txt', 'w') as cache:
# 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])
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.
# 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('=' * len(name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment