某讯视频用python解析数据并下载视频mp4格式
国庆祝福祖国生日快乐。偶尔间,想做做视频剪辑,需要下载视频素材,但是在平台上的视频似乎不支持手动下载。自然学了python,可以试着用代码去下载视频,在这块找到相应的教学内容,本次内容主要是分享用python如何去获取下载某讯视频。下面为大家讲解一下。
就拿最近比较火的昆仑神宫来做下视频解析下载,首先是打开网页版的腾讯视频。通过测试发现这个ts文件就是视频片段。就是我们需要下载的视频数据
不过这个地址是一个拼接地址,我们要先获取视频的拼接字段。这个就是需要获取的拼接字段。
可以发现这个拼接字段就是视频的片段,那么只要找到这个字段获取接口就行了。这就是视频的接口了。
在代码中我们进行模拟请求
def spiderIndex(): url = 'https://vd6.l.qq.com/proxyhttp' headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36', 'Cookie': 'RK=2YvZ9H2FTa; ptcz=1b68ca958b61f0640e1514e58e4bfa3b737a4bc3270f19eaa18d830752000e87; ptui_loginuin=649307714@qq.com; pgv_pvid=1666049471; _clck=3944290737|1|f4o|0; tvfe_boss_uuid=e776055103157d8d; appuser=9C1476C01B8D3CAC; lv_play_index=4; o_minduid=pFcsCshCv2RhT6TqeBACWGaGa2DNlzs0; vversion_name=8.2.95; www_omgid=8e51da136961c52f; pgv_info=ssid=s9431282696; LPPBturn=142; LZTturn=480; ufc=r24_1_1664723257_1664637037; LPVLturn=449; LPSJturn=408; LCZCturn=692; LVINturn=182; LPHLSturn=284; LDERturn=160; LPDFturn=22' } data = {"adparam":"pf=in&pf_ex=pc&pu=1&pt=0&platform=10901&from=0&flowid=10d3a06bd590587626c4a14d675c462d&guid=8e51da136961c52f&coverid=mzc002000pcdwox&vid=u00445z1gcx&chid=0&tpid=2&refer=&url=https%3A%2F%2Fv.qq.com%2Fx%2Fcover%2Fmzc002000pcdwox%2Fu00445z1gcx.html<=qq&opid=B1691D752D68898B6CB03F1C37943B28&atkn=819BE5322FB8FA8BBD7FC926CD0E883C&appid=101483052&uid=565401408&tkn=cOp6IaTofj-_d322aK7gKA.N&rfid=933adbce14d7b65786633cf13150dd01_1664522169&v=1.7.0&vptag=&ad_type=LD%7CKB%7CPVL&live=0&appversion=1.5.4&ty=web&adaptor=1&dtype=1&resp_type=json&s_img=webp", "buid":"vinfoad", "vinfoparam":"charge=0&otype=ojson&defnpayver=7&spau=1&spaudio=64&spwm=1&sphls=2&host=v.qq.com&refer=https%3A%2F%2Fv.qq.com%2Fx%2Fcover%2Fmzc002000pcdwox%2Fu00445z1gcx.html&ehost=https%3A%2F%2Fv.qq.com%2Fx%2Fcover%2Fmzc002000pcdwox%2Fu00445z1gcx.html&sphttps=1&encryptVer=8.1&cKey=802F458EC052379B56152C578E14E490FC2FD062DD0AB2FCA43AF8C40027308B4629214745732BD518AA0875C9569B6B84BC97DB4513041EB03BCFCC6D14771065273D54F7E2EF9334CEABBECA32A4D351719DD450611949B0C342570B2163872AB8D049A9EE75355F511569A6838296CD24F3DE3FA6C4A0D5E938802656F54ED2A69A4AAD2EA0B720E391E876BF02E3EB6BEB2D6C5362B702C7E4F962882F0CDBEFA8B56DD28047AD56D29799010A2955AEB4122ABB79465ABB601D5E948F3D9CEFF2687DD620982EEEC5F0CB72A57D&clip=4&guid=8e51da136961c52f&flowid=0522a8876f3d5fa03be5a175b4c0e1a3&platform=10901&sdtfrom=v1010&appVer=3.5.57&unid=&auth_from=&auth_ext=&vid=u00445z1gcx&defn=&fhdswitch=0&dtype=3&spsrt=2&tm=1664638869&lang_code=0&logintoken=%7B%22access_token%22%3A%22819BE5322FB8FA8BBD7FC926CD0E883C%22%2C%22appid%22%3A%22101483052%22%2C%22vusession%22%3A%22Tfr_izUS-a3jBXGcQrO5-g.N%22%2C%22openid%22%3A%22B1691D752D68898B6CB03F1C37943B28%22%2C%22vuserid%22%3A%22565401408%22%2C%22www_guid%22%3A%228e51da136961c52f%22%2C%22main_login%22%3A%22qq%22%7D&spvvpay=1&spadseg=3&spav1=15&hevclv=28&spsfrhdr=100&spwww=4&spm3u8tag=67&spmasterm3u8=3&drm=40" } resp = requests.post(url=url,headers=headers,json=data) # print(resp.json()['vinfo']) vinfo = resp.json()['vinfo'] return eval(vinfo)
经过响应后获取到的json数据
接下来我们对json数据进行解析获取视频地址字段。
def parseJson(info): m3u8_data = info['vl'] ['vi'][0]['ul']['m3u8'] m3u8_data = re.sub(r'#E.*', '', m3u8_data).split() print(m3u8_data) return m3u8_data
最后对图片1的地址进行拼接成完整地址,然后进行请求获取字节流数据并保存为视频mp4格式
def main(): info = spiderIndex() items = parseJson(info) # print(items) for ts in tqdm(items): ts_url = 'https://apd-af0ad8ebcf60efe082459590668c79cd20857437bcbe6769.v.smtcdns.com/moviets.tc.qq.com/AQxYvBGijPcvDFbmnZR-mW3FBthNmbw5Tsj7QIVYfTT0/uwMROfz2r55ggaQXGdGnC2de5GryTJ3wTQg5cEoxzm7irTT1/svp_50112/89pevnZgGuf9qsaNm-ZpTCoRipcLQNBi8jJFH0jfbATCVkALe0kjzufGlzZQOJxxvuwbUIZV6R-yoX5KWn5Q2vf1sN7ZeWFpCCbpdKisCkfUt3qcgky8X9Oj56e2WA9OzrfWuygKCBDAnYGAVqo8x6SdWygvKED-im-7B-tacSo/' + ts # print(ts_url) ts_content = requests.get(url=ts_url).content with open('昆仑神宫.mp4', mode='ab') as f: f.write(ts_content) print('下载完成')
然后下载完成后,我们找到这个视频进行播放测试
附加,国庆期间,快排源码和网页系统以及其他软件源码都8折优惠,有需要的可以私信我。
声明:所有文章只做编程技术知识分享,不可非法用途使用,任何非法用途与本人无关。