I'm writing simple Python/Flask script, which is suppposed to extract and redirect to URL of desired Youtube video for download. For this purpose I use youtube-dl module. In the code I create YoutubeDL object without any parameters.
from youtube_dl import YoutubeDL
ydl = YoutubeDL()
I'm developing the code locally and also I've deployed my code to PythonAnywhere (relevant snippet below) ...
def video(id):
url = 'https://www.youtube.com/watch?v=' + id
r = ydl.extract_info(url, download=False)
if 'entries' in r:
video = r['entries'][0]
else:
video = r
video_url = video['url']
return flask.redirect(video_url)
... and it returns URL that looks correct at the first sight but it leads to blank page everytime. The same code returns valid URL, that actually leads to video download when running locally.
This is how looks URL that works:
https://r11---sn-2gb7ln7k.googlevideo.com/videoplayback?mm=31&ip=82.117.130.94&mn=sn-2gb7ln7k&pl=21&itag=22&signature=D73CFDE63BCE4359D57535A1478D1D9D65DA52AA.2C1AB3C70A9C95685DAFF5742D2D3ACE69306914&id=o-ALXi8_5fzzqW3bvJ-B9Yl2-hO927Vbfgx-ufVjNys1OI&mt=1459240812&upn=PhqlLYrRU3I&mv=m&ms=au&key=yt6&expire=1459262524&mime=video%2Fmp4&dur=4209.986&sver=3&ratebypass=yes&fexp=9408209%2C9416126%2C9416916%2C9417701%2C9420452%2C9422546%2C9422596%2C9423661%2C9423662%2C9424580%2C9427320%2C9427902%2C9429118%2C9429314%2C9431086%2C9431173%2C9432034&nh=IgphcjAxLnByZzAyKgkxMjcuMC4wLjE&sparams=dur%2Cgcr%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cnh%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&lmt=1458929174531688&ipbits=0&gcr=cz&requiressl=yes&source=youtube&initcwndbps=1297500
... and this is extracted URL of the same video, which leads to blank page:
https://r11---sn-p5qlsn7s.googlevideo.com/videoplayback?lmt=1458929174531688&ip=54.147.140.181&gcr=us&expire=1459262525&id=o-AH6rUZjQmWtR55nCdUd2VKG8QrZxZIgZ1aVaKp7Chc3s&upn=DDLzYOc4HBg&nh=IgpwcjAyLmlhZDI2KgkxMjcuMC4wLjE&pl=22&source=youtube&sparams=dur%2Cgcr%2Cid%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cnh%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&requiressl=yes&ratebypass=yes&key=yt6&mime=video%2Fmp4&fexp=9408506%2C9416126%2C9417368%2C9419452%2C9420452%2C9422596%2C9423661%2C9423662%2C9424132%2C9427143%2C9427364%2C9427902%2C9428422%2C9428710%2C9431439%2C9431464%2C9431860%2C9431952&ipbits=0&mn=sn-p5qlsn7s&signature=B71BF75C6BF798D49720F960F9AFF11E946382F0.8A17B35804BCBA5B0D0D2CA3C889B529A17AC9D9&mm=31&itag=22&sver=3&dur=4209.986&mv=u&mt=1459240330&ms=au
I have the same version of youtube-dl installed locally and hosted (2016.3.18).
Is there a way to control the format of URL on output (e.g. Youtube-dl options)? I could not find it in documentation. Or any other way to reformat URL to be valid?
Thank you, in advance.
I will partly answer myself - It seems that extracting video URL for download and actually downloading the video on 2 different machines won't reliably work ever (due to cookies, IP must be the same, ...) Documented here.