以虎牙视频播放页地址 http://v.huya.com/play/3474405.html 为例,说说如何得到视频的真实地址。
提取视频ID
视频ID(vid)可以从播放页地址中得到,上述播放页地址对应的视频ID就是3474405.
请求接口
通过浏览器开发者工具的网络监测,我们发现如下接口:
http://playapi.v.duowan.com/index.php?r=play%2Fvideo&vid=3474405&partner=
请求该接口返回的是JSON格式数据:
{
"code": 1,
"message": "success",
"result": {
"cover": "http://vimg.dwstatic.com/1644/3474405/3.jpg",
"vr": false,
"channel": "df",
"items": [
{
"transcode_id": "5897886",
"vid": "3474405",
"task_id": "5790225",
"video_name": "3474405-102-1478215562.mp4",
"task_name": "原画(不转码)",
"status": "1",
"can_play": "1",
"format": "",
"definition": "yuanhua",
"size": "62741992",
"width": "1280",
"height": "720",
"duration": "229.016000",
"bitrate": "2191706",
"cover": "",
"md5": "",
"path": "1644/",
"transcode": {
"transcode_id": "5897886",
"video_name": "3474405-102-1478215562.mp4",
"size": "62741992",
"width": "1280",
"height": "720",
"duration": "229.016000",
"path": "1644/",
"urls": [
"http://w5.dwstatic.com/54/8/1644/3474405-102-1478215562.mp4",
"http://w5.dwstatic.com/54/8/1644/3474405-102-1478215562.mp4",
"http://w5.dwstatic.com/54/8/1644/3474405-102-1478215562.mp4",
"http://w5.dwstatic.com/54/8/1644/3474405-102-1478215562.mp4"
]
}
},
{
"transcode_id": "5897889",
"vid": "3474405",
"task_id": "5790226",
"video_name": "3474405-98-1478215563.mp4",
"task_name": "流畅",
"status": "1",
"can_play": "1",
"format": "",
"definition": "350",
"size": "8393817",
"width": "640",
"height": "360",
"duration": "229.085000",
"bitrate": "293124",
"cover": "",
"md5": "",
"path": "1644/",
"transcode": {
"transcode_id": "5897889",
"video_name": "3474405-98-1478215563.mp4",
"size": "8393817",
"width": "640",
"height": "360",
"duration": "229.085000",
"path": "1644/",
"urls": [
"http://w5.dwstatic.com/52/11/1644/3474405-98-1478215563.mp4",
"http://w5.dwstatic.com/52/11/1644/3474405-98-1478215563.mp4",
"http://w5.dwstatic.com/52/11/1644/3474405-98-1478215563.mp4",
"http://w5.dwstatic.com/52/11/1644/3474405-98-1478215563.mp4"
]
}
},
{
"transcode_id": "5897890",
"vid": "3474405",
"task_id": "5790227",
"video_name": "3474405-99-1478215563.mp4",
"task_name": "高清",
"status": "1",
"can_play": "1",
"format": "",
"definition": "1000",
"size": "15304327",
"width": "960",
"height": "540",
"duration": "229.085000",
"bitrate": "534450",
"cover": "",
"md5": "",
"path": "1644/",
"transcode": {
"transcode_id": "5897890",
"video_name": "3474405-99-1478215563.mp4",
"size": "15304327",
"width": "960",
"height": "540",
"duration": "229.085000",
"path": "1644/",
"urls": [
"http://w5.dwstatic.com/53/11/1644/3474405-99-1478215563.mp4",
"http://w5.dwstatic.com/53/11/1644/3474405-99-1478215563.mp4",
"http://w5.dwstatic.com/53/11/1644/3474405-99-1478215563.mp4",
"http://w5.dwstatic.com/53/11/1644/3474405-99-1478215563.mp4"
]
}
},
{
"transcode_id": "5897891",
"vid": "3474405",
"task_id": "5790228",
"video_name": "3474405-100-1478215563.mp4",
"task_name": "超清",
"status": "1",
"can_play": "1",
"format": "",
"definition": "1300",
"size": "27177273",
"width": "1280",
"height": "720",
"duration": "229.085000",
"bitrate": "949072",
"cover": "",
"md5": "",
"path": "1644/",
"transcode": {
"transcode_id": "5897891",
"video_name": "3474405-100-1478215563.mp4",
"size": "27177273",
"width": "1280",
"height": "720",
"duration": "229.085000",
"path": "1644/",
"urls": [
"http://w5.dwstatic.com/55/8/1644/3474405-100-1478215563.mp4",
"http://w5.dwstatic.com/55/8/1644/3474405-100-1478215563.mp4",
"http://w5.dwstatic.com/55/8/1644/3474405-100-1478215563.mp4",
"http://w5.dwstatic.com/55/8/1644/3474405-100-1478215563.mp4"
]
}
}
],
"code": {
"swf": "http://assets.dwstatic.com/video/vppp.swf?vid=3474405&auto_play=1&channelId=df&logo=huya",
"html": "<embed id=\"3474405\" name=\"3474405\" src=\"http://assets.dwstatic.com/video/vppp.swf\" wmode=\"transparent\" allowFullScreen=\"true\" quality=\"high\" height=\"498\" width=\"610\" align=\"middle\" allowScriptAccess=\"always\" flashvars=\"auto_play=1&vid=3474405&channelId=df&logo=huya\" type=\"application/x-shockwave-flash\">\t</embed> ",
"url": "http://v.huya.com/play/3474405.html"
}
}
}
items列表中包含了各个清晰度视频的相关信息。可以看到,共有原画、流畅、高清和超清这4种码流。每种码流中的urls就是视频的真实地址。
Python代码示例
import requests
import json
import re
import random
vid = re.search(r'/play/(.*).html', 'http://v.huya.com/play/3474405.html').group(1)
play_api = 'http://playapi.v.duowan.com/index.php?partner=&r=play/video&vid=%s' % vid
r = requests.get(play_api)
data = json.loads(r.content)
for item in data['result']['items']:
definition = item['task_name']
urls = item['transcode']['urls']
print definition
print random.choice(urls)
运行结果:
原画(不转码)
http://w5.dwstatic.com/54/8/1644/3474405-102-1478215562.mp4
流畅
http://w5.dwstatic.com/52/11/1644/3474405-98-1478215563.mp4
高清
http://w5.dwstatic.com/53/11/1644/3474405-99-1478215563.mp4
超清
http://w5.dwstatic.com/55/8/1644/3474405-100-1478215563.mp4