芒果TV视频的地址解析下载

以芒果TV播放页地址http://www.mgtv.com/v/10/167938/c/2942052.html�为例,说说如何解析得到真实视频地址。

提取视频ID(即vid)

芒果TV的视频ID可以从播放页地址中得到。上述播放页地址中,2942052就是视频ID.

请求接口

以视频ID为参数请求接口 http://v.api.mgtv.com/player/video?retry=1&video_id=2942052 返回的JSON数据如下:

{
  "status": 200,
  "msg": "",
  "data": {
    "user": {
      "id": "",
      "uuid": "",
      "isvip": "0",
      "purview": "200",
      "ip": "1.95.84.161"
    },
    "info": {
      "video_id": "3467275",
      "root_id": "10",
      "root_name": "新闻",
      "collection_id": "298221",
      "collection_name": "午间新闻2016",
      "clip_type": "2",
      "title": "长沙着力解决房产证遗留问题",
      "sub_title": "长沙着力解决房产证遗留问题",
      "series": "",
      "url": "http://www.hunantv.com/v/10/298221/c/3467275.html",
      "thumb": "http://2img.mgtv.com/preview/sp_images/2016/xinwen/129747/2942052/20160106130629996.jpg_220x123.jpg",
      "desc": "长沙着力解决房产证遗留问题",
      "icon": "",
      "ispayment": "0",
      "isdrm": "0",
      "isiplimit": "0",
      "duration": "37",
      "trialtime": "300",
      "scale": "16:9",
      "istry": "0",
      "price_vip": "2.5",
      "price_novip": "5",
      "price": "0"
    },
    "stream": [
      {
        "url": "/vod.do?fmt=2&pno=1000&fid=AD299930AAB40177B5F10D4AF1471071&now=1478094895&gsid=a599287b6be0403988d92fae7a7f930a&file=%2Fmp4%2F2016%2Fxinwen%2Fwjxw_46493%2FAD299930AAB40177B5F10D4AF1471071_20160106_1_1_388_mp4%2F27647495F355811D49F4A26327EC35DC.m3u8",
        "name": "标清"
      },
      {
        "url": "/vod.do?fmt=2&pno=1000&fid=CBA97B6D9BA73EB84283817B1FE38FE8&now=1478094895&gsid=7f3a643f38dc469d81fd8bc942c6aa89&file=%2Fmp4%2F2016%2Fxinwen%2Fwjxw_46493%2FCBA97B6D9BA73EB84283817B1FE38FE8_20160106_1_1_719_mp4%2FB2B29E3A4D135CB9B86B85841D12139C.m3u8",
        "name": "高清"
      },
      {
        "url": "/vod.do?fmt=2&pno=1000&fid=593B5886239791678A15C4A872A60B08&now=1478094895&gsid=79b4eb17ce3b441084b4828230fdfdd7&file=%2Fmp4%2F2016%2Fxinwen%2Fwjxw_46493%2F593B5886239791678A15C4A872A60B08_20160106_1_1_1100_mp4%2FE2017FFD796C00BB185FBC30B6D7360B.m3u8",
        "name": "超清"
      }
    ],
    "stream_domain": [
      "http://disp.titan.mgtv.com",
      "http://disp1.titan.mgtv.com",
      "http://disp2.titan.mgtv.com"
    ],
    "share": {
      "weibo": "http://service.weibo.com/share/share.php?appkey=152400019&url=http%3A%2F%2Fwww.hunantv.com%2Fv%2F10%2F298221%2Fc%2F3467275.html%3Ffrom%3Dwb&title=%E9%95%BF%E6%B2%99%E7%9D%80%E5%8A%9B%E8%A7%A3%E5%86%B3%E6%88%BF%E4%BA%A7%E8%AF%81%E9%81%97%E7%95%99%E9%97%AE%E9%A2%98&content=utf-8&pic=http://2img.mgtv.com/preview/sp_images/2016/xinwen/129747/2942052/20160106130629996.jpg_220x123.jpg",
      "weixin": "http://www.hunantv.com/share/weixin/?url=http://www.hunantv.com/v/10/298221/c/3467275.html&title=%E9%95%BF%E6%B2%99%E7%9D%80%E5%8A%9B%E8%A7%A3%E5%86%B3%E6%88%BF%E4%BA%A7%E8%AF%81%E9%81%97%E7%95%99%E9%97%AE%E9%A2%98",
      "qq": "http://connect.qq.com/widget/shareqq/index.html?title=%E9%95%BF%E6%B2%99%E7%9D%80%E5%8A%9B%E8%A7%A3%E5%86%B3%E6%88%BF%E4%BA%A7%E8%AF%81%E9%81%97%E7%95%99%E9%97%AE%E9%A2%98&desc=%E5%9C%A8%E8%8A%92%E6%9E%9CTV%E4%B8%8A%E7%9C%8B%E8%A7%81%E8%BF%99%E6%9D%A1%E8%A7%86%E9%A2%91%E8%BF%98%E4%B8%8D%E9%94%99%E5%93%A6%EF%BC%9A%E9%95%BF%E6%B2%99%E7%9D%80%E5%8A%9B%E8%A7%A3%E5%86%B3%E6%88%BF%E4%BA%A7%E8%AF%81%E9%81%97%E7%95%99%E9%97%AE%E9%A2%98&site=%E8%8A%92%E6%9E%9CTV&url=http://www.hunantv.com/v/10/298221/c/3467275.html&tpa=dW5pb25faWQ9MTAzMjUyXzEwMDAwMV8wMV8wMQ",
      "qzone": "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=http://www.hunantv.com/v/10/298221/c/3467275.html&title=%E9%95%BF%E6%B2%99%E7%9D%80%E5%8A%9B%E8%A7%A3%E5%86%B3%E6%88%BF%E4%BA%A7%E8%AF%81%E9%81%97%E7%95%99%E9%97%AE%E9%A2%98"
    },
    "skipad": [
      "http://i1.hunantv.com/ui/2015/images/skipad.jpg",
      "http://order.mgtv.com/pay/pc/index.html"
    ],
    "tips": {
      "title": "会员跳广告(买12送3)",
      "url": "http://order.mgtv.com/pay/pc/index.html",
      "img": ""
    },
    "frame": {
      "second": [],
      "images": []
    },
    "points": {
      "start": [],
      "end": [],
      "content": []
    },
    "next": {
      "title": "长沙2016春运“两证”核发",
      "img": "http://0img.mgtv.com/preview/sp_images/2016/xinwen/129747/2942051/20160106125233250.jpg_220x123.jpg",
      "url": "/v/10/298221/c/3467257.html",
      "duration": "23"
    }
  }
}

我们可以看到,stream列表里面共有3个清晰度,stream_domain列表里面共有3个域名。假如选择超清清晰度对应的url,然后在url前面加上3个域名中的任一个,构成一个地址 http://disp.titan.mgtv.com/vod.do?fmt=2&pno=1000&fid=593B5886239791678A15C4A872A60B08&now=1478094895&gsid=79b4eb17ce3b441084b4828230fdfdd7&file=%2Fmp4%2F2016%2Fxinwen%2Fwjxw_46493%2F593B5886239791678A15C4A872A60B08_20160106_1_1_1100_mp4%2FE2017FFD796C00BB185FBC30B6D7360B.m3u8

请求这个地址得到的也是一个JSON格式数据:

{
  "ver": "2015-02-25 v1.0.1",
  "isothercdn": "1",
  "info": "http://pcvideoaliyun.titan.mgtv.com/mp4/2016/xinwen/wjxw_46493/593B5886239791678A15C4A872A60B08_20160106_1_1_1100_mp4/E2017FFD796C00BB185FBC30B6D7360B.m3u8?uuid=66e980da9e284bbe8222cae71fcc650a&t=581a6306&pno=1000&sign=ccedb70160e9136870fbd72864f11bb2&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken%3dhit%3d0%5eruip%3d23024801&rdur=21600&arange=0&limitrate=0&fid=593B5886239791678A15C4A872A60B08&ver=0x03",
  "status": "ok",
  "loc": "81-1.95.84.161",
  "t": "1478095494",
  "idc": "aliyun_cdn"
}

其中,info是一个m3u8地址,请求m3u8地址 http://pcvideoaliyun.titan.mgtv.com/mp4/2016/xinwen/wjxw_46493/593B5886239791678A15C4A872A60B08_20160106_1_1_1100_mp4/E2017FFD796C00BB185FBC30B6D7360B.m3u8?uuid=66e980da9e284bbe8222cae71fcc650a&t=581a6306&pno=1000&sign=ccedb70160e9136870fbd72864f11bb2&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken%3dhit%3d0%5eruip%3d23024801&rdur=21600&arange=0&limitrate=0&fid=593B5886239791678A15C4A872A60B08&ver=0x03 ,得到如下m3u8内容:

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:11
#EXT-X-MEDIA-SEQUENCE:0
#EXT-MGTV-VIDEO-WIDTH:1280
#EXT-MGTV-VIDEO-HEIGHT:720
#EXT-MGTV-File-SIZE:699360
#EXTINF:5.080000,
5976B00263913818AE86F0122D7B766F_0_5080_1101_v02_mp4.ts?uuid=66e980da9e284bbe8222cae71fcc650a&t=581a6306&pno=1000&sign=ccedb70160e9136870fbd72864f11bb2&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken%3dhit%3d0%5eruip%3d23024801&rdur=21600&arange=0&limitrate=0&fid=593B5886239791678A15C4A872A60B08&ver=0x03
#EXT-MGTV-File-SIZE:512488
#EXTINF:5.000000,
01FC81B6CE4051E6CA9D990A72FF9B61_5080_10080_819_v02_mp4.ts?uuid=66e980da9e284bbe8222cae71fcc650a&t=581a6306&pno=1000&sign=ccedb70160e9136870fbd72864f11bb2&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken%3dhit%3d0%5eruip%3d23024801&rdur=21600&arange=0&limitrate=0&fid=593B5886239791678A15C4A872A60B08&ver=0x03
#EXT-MGTV-File-SIZE:1392140
#EXTINF:10.000000,
E1A9A55392CA363DD79F9F34C5AB1D45_10080_20080_1113_v02_mp4.ts?uuid=66e980da9e284bbe8222cae71fcc650a&t=581a6306&pno=1000&sign=ccedb70160e9136870fbd72864f11bb2&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken%3dhit%3d0%5eruip%3d23024801&rdur=21600&arange=0&limitrate=0&fid=593B5886239791678A15C4A872A60B08&ver=0x03
#EXT-MGTV-File-SIZE:2260136
#EXTINF:10.000000,
645A04A7B53530E5FA5CB5FCCF772A35_20080_30080_1808_v02_mp4.ts?uuid=66e980da9e284bbe8222cae71fcc650a&t=581a6306&pno=1000&sign=ccedb70160e9136870fbd72864f11bb2&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken%3dhit%3d0%5eruip%3d23024801&rdur=21600&arange=0&limitrate=0&fid=593B5886239791678A15C4A872A60B08&ver=0x03
#EXT-MGTV-File-SIZE:926652
#EXTINF:7.080000,
B7D675030CAB254C04330380F8A8A2AF_30080_37160_1047_v02_mp4.ts?uuid=66e980da9e284bbe8222cae71fcc650a&t=581a6306&pno=1000&sign=ccedb70160e9136870fbd72864f11bb2&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken%3dhit%3d0%5eruip%3d23024801&rdur=21600&arange=0&limitrate=0&fid=593B5886239791678A15C4A872A60B08&ver=0x03
#EXT-X-ENDLIST

那些不以#号开头的行就是视频分段地址的一部分。只不过,完整的视频分段地址需要构造出来。如何构造呢?我们用浏览器开发者工具观察到最终的视频地址是这样的:

http://pcvideoaliyun.titan.mgtv.com/mp4/2016/xinwen/wjxw_46493/CBA97B6D9BA73EB84283817B1FE38FE8_20160106_1_1_719_mp4/B4FBE7EEE4C4E58313038787AFA1CBF7_30080_37160_773_v02_mp4.ts?uuid=b6e729c483cb4ebfa2ca6cccc08bacff&t=581a661e&pno=1000&sign=b454c0a00e9240c96c877662329699cb&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken%3dhit%3d0%5eruip%3d23024801&rdur=21600&arange=0&limitrate=0&fid=CBA97B6D9BA73EB84283817B1FE38FE8&ver=0x03&r=309659237103548&tt=3167&yfmode=3

而m3u8地址是这样的:

http://pcvideoaliyun.titan.mgtv.com/mp4/2016/xinwen/wjxw_46493/593B5886239791678A15C4A872A60B08_20160106_1_1_1100_mp4/E2017FFD796C00BB185FBC30B6D7360B.m3u8?uuid=66e980da9e284bbe8222cae71fcc650a&t=581a6306&pno=1000&sign=ccedb70160e9136870fbd72864f11bb2&win=3600&srgid=25021&urgid=81&srgids=25021&nid=25021&payload=usertoken=hit=0%5Eruip=23024801&rdur=21600&arange=0&limitrate=0&fid=593B5886239791678A15C4A872A60B08&ver=0x03

这两个地址的前面一部分是一样的,有木有?这下该知道如何构造完整的视频分段地址了吧。

Python代码示例

import requests
import json
import re
import os
import urlparse
import random

vid = re.search(r'/(\d+).html', 'http://www.mgtv.com/v/10/167938/c/2942052.html').group(1)
r = requests.get('http://v.api.mgtv.com/player/video?retry=1&video_id=%s' % vid)
data = json.loads(r.content)
streams = data['data']['stream']
stream_domains = data['data']['stream_domain']
for stream in streams:
    print stream['name']
    stream_domain = random.choice(stream_domains)
    url = urlparse.urljoin(stream_domain, stream['url'])
    r = requests.get(url)
    data = json.loads(r.content)
    m3u8_url = data['info']
    parse_result = urlparse.urlparse(m3u8_url)
    r = requests.get(m3u8_url)
    segments = re.findall(r'^[^#][^\r][^\n]*', r.content, re.MULTILINE)
    for s in segments:
        print '%s://%s%s/%s' % (parse_result.scheme, parse_result.netloc, os.path.dirname(parse_result.path), s)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,236评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,867评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,715评论 0 340
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,899评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,895评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,733评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,085评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,722评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,025评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,696评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,816评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,447评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,057评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,009评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,254评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,204评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,561评论 2 343

推荐阅读更多精彩内容