Python Youtube-dl提取无效的URL

时间:2016-03-29 09:01:50

标签: python youtube pythonanywhere youtube-dl

我正在编写简单的Python / Flask脚本,该脚本用于提取并重定向到所需Youtube视频的URL以供下载。为此,我使用youtube-dl模块。在代码中我创建了没有任何参数的YoutubeDL对象。

from youtube_dl import YoutubeDL
ydl = YoutubeDL()

我正在本地开发代码,并且我已将代码部署到PythonAnywhere(下面的相关代码段)......

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)

...它会返回第一眼看上去正确的网址,但每次都会显示空白页面。 相同的代码返回有效的URL,实际上在本地运行时会导致视频下载。

这就是看起来有效的网址:

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

...这是同一视频的提取网址,这会导致空白页:

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

我在本地和托管安装了相同版本的youtube-dl(2016.3.18)。

有没有办法控制输出上的URL格式(例如Youtube-dl选项)?我在文档中找不到它。或者重新格式化URL以使其有效的任何其他方式?

提前谢谢你。

1 个答案:

答案 0 :(得分:2)

我将部分回答自己 - 似乎提取视频网址以供下载并在2台不同的机器上实际下载视频无法可靠地工作(由于cookie,IP必须相同,......)记录here