某瓜视频下载python脚本
感谢@LoveCode大佬的 https://www.52pojie.cn/thread-1917707-1-1.html 分析,弄了个脚本出来,大神们请略过音、视频分开的情况,需要自己配置FFMPEG路径。没有弄合集下载,有兴趣的大佬,请帮弄好贴上来,谢谢。
'''
by 52pojie-xinxiu
2024.4.27一种情况是有dynamic_video的,一种情况是没有dynamic_video的情况视频和音频分开,下载后需要合并
另一种是video_list只有视频包含音频,代码自动判断可能出现的情况,并下载
'''
import base64
import json
import os.path
import re
import subprocess
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
import requests
from tqdm import tqdm
headers = {
"cookie": "填入你自己的cookie,不然运行代码会出错",
"Referer": "https://www.ixigua.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36"
}
def aes_decrypt(data: str, key: str) -> str:
data = base64.b64decode(data)
key = key.encode()
iv = key[:16]
# mode 为 CBC、pad 为 PKcs7
cipher = AES.new(key, AES.MODE_CBC, iv)
res = cipher.decrypt(data)
res = unpad(res, AES.block_size)
res = base64.b64decode(res).decode()
return res
def downloadFile(url, name):
print(f'开始下载: {name}')
resp = requests.get(url, headers=headers)
if not os.path.exists('movie'):
os.mkdir('movie')
with open(f'movie/{name}', mode='wb') as f:
f.write(resp.content)
print(f'完成下载: {name}')
def downloadAll(video_url, audio_url, title):
if not os.path.exists('movie'):
os.mkdir('movie')
if not os.path.exists(f'movie/{title}-merger.mp4'):
if audio_url:# 如果音频链接存在,说明需要合并
downloadFile(video_url, title + '.mp4')
downloadFile(audio_url, title + '.mp3')
command = f'ffmpeg -i movie/{title}.mp4 -i movie/{title}.mp3 -c:v copy -c:a aac -strict experimental movie/{title}-merger.mp4'
print('开始合并音频视频......')
subprocess.run(command, shell=True)
os.remove(f'movie/{title}.mp4')
os.remove(f'movie/{title}.mp3')
print(f'完成合并,见目录: movie/{title}-merger.mp4')
else:# 如果音频链接不存在,直接下载视频文件
downloadFile(video_url, title + '.mp4')
print(f'完成下载: {title}.mp4')
def main(url):
resp = requests.get(url=url, headers=headers)
resp.encoding = 'utf-8'
if resp.status_code == 200:
res_html = resp.text
if 'pseries_more_v2' not in url:
json_str = re.findall('window._SSR_HYDRATED_DATA=(.*?)</script>', res_html)
json_str = json_str.replace('undefined', 'null')
json_data = json.loads(json_str)
# video_resource = json_data['anyVideo']['gidInformation']['packerData']['video']['videoResource']['dash']
video_resource = json_data['anyVideo']['gidInformation']['packerData']['video']['videoResource']['normal']
# 初始化视频链接和音频链接为 None
video_url = None
audio_url = None
# 检查是否存在 dynamic_video,如果存在则获取视频链接
if 'dynamic_video' in video_resource:
video_url = video_resource['dynamic_video']['dynamic_video_list'][-1]['main_url']
audio_url = video_resource['dynamic_video']['dynamic_audio_list'][-1]['main_url']
ptk = video_resource['ptk']
# 如果不存在 dynamic_video,则尝试获取 video_list 中的视频链接
elif 'video_list' in video_resource:
video_url = next(iter(video_resource['video_list'].values()))['main_url']
ptk = video_resource['ptk']
# 如果 video_url 不为空,则进行解密处理
if video_url:
title = json_data['anyVideo']['gidInformation']['packerData']['video']['title']
title = re.sub(r"[\/\\\:\*\?\"\<\>\|]", "_", title).replace(" ", "")
video_url = aes_decrypt(data=video_url, key=ptk)
print(video_url)
if audio_url:
audio_url = aes_decrypt(data=audio_url, key=ptk)
downloadAll(video_url, audio_url, title)
else:
print("无法获取视频链接")
else:
json_data = json.loads(res_html)
for item in json_data['data']:
title = item['title']
video_url = item['preloadVideoResource']['dynamic_video']['dynamic_video_list'][-1]['backup_url_1']
audio_url = item['preloadVideoResource']['dynamic_video']['dynamic_audio_list'][-1]['backup_url_1']
video_url = base64.b64decode(video_url).decode()
audio_url = base64.b64decode(audio_url).decode()
title = re.sub(r"[\/\\\:\*\?\"\<\>\|]", "_", title).replace(" ", "")
downloadAll(video_url, audio_url, title)
if __name__ == '__main__':
# 单个视频 https://www.ixigua.com/6895255008762135051
# 视频合集 https://www.ixigua.com/api/videov2/pseries_more_v2?pSeriesId=7069375938756411911&rank=0&tailCount=30
# 关于下载视频合集的几个参数 rank:每页的起始数字tailCount: 一页展示多少个
# 比如第一页: rank=0&tailCount=30 第二页: rank=30&tailCount=30 第三页: rank=60&tailCount=30
# 直接复制地址栏中长数字(类似:6895255008762135051)替换到链接中即可
# 单个视频下载
# 原链接:https://www.ixigua.com/7360279127813063202,https://www.ixigua.com/7354353866864853515
main('https://www.ixigua.com/7343202047816434211?logTag=1f46f01173fd5dcf7d14')
# 合集视频下载
# 原链接: https://www.ixigua.com/6888602680952881676?id=6887944298654859780
# 将6888602680952881676 替换到下面链接中
# main('https://www.ixigua.com/api/videov2/pseries_more_v2?pSeriesId=6888602680952881676&rank=0&tailCount=30')
西瓜视频中的电影好像解密方式是不一样的,"cenc-aes-ctr",能否分析一下。比如杀手之五:https://www.ixigua.com/6551332086433710600
"quality_type": 1,
"definition": "1080p",
"quality": "normal",
"vtype": "dash",
"vwidth": 1920,
"vheight": 1080,
"bitrate": 4371594,
"codec_type": "h264",
"size": 2120385323,
"file_id": "b06cb89abb644b0ea4b8e108c70e1381",
"fps": 25,
"file_hash": "a553e72dbe363f1c2988f90b6cad2afa",
"real_bitrate": 2883067,
"avg_bitrate": 2755245,
"init_range": "0-1280",
"index_range": "1281-16096",
"first_moof_range": "16097-17200",
"encrypt": true,
"kid": "634a6edb6c39fca369f6f8230003043b",
"spade_a": "orws9Hy8NMFluDX0TLkA9Uy4NfR5jADESbwzwU6\u002FBfaFgc19fQ==",
"encryption_method": "cenc-aes-ctr",
"backup_url_1": "NQbjT+dkrjwfEvWHEqGDEaWYi7jl498c2+dGkhejtQy1QZLW03+RIf+Dc+TBH4J8Etv2j4FvoS78wTwS1x1WCNqdXMuai+Eqd62c\u002F5lhD7j2hfGJ8pYeGCVcWJUaJYkX9nyt52a8dWvYcd+qfsem\u002F4UUyLESnFUZ9XT27lD9bX2swnuv4R\u002FG9KuPHP3eibFEK6QY5K5K5QAxZ\u002FYKVfOFBFJlFX7o68kXNPH6vDSWTHAAlcMnExX2hrvBDpkOMoTY3eEiTi3iGiAkYD79m5aaud3HRgj5xeT5hrKdEYqEMkK24HPRHOMBLD0R\u002FqSJlkmB3RAQ93+hNDcpLMeNaekf8LRLIb1IMkCIpZh12zkOKoU1CVPLCyRB7bHhN9hv6+XgqmU0gArGODZmNb28cVIMzZVaq\u002FiV4Jv8xEbpOeywHx1MyKtJLLtekVrDjQffaTz495LZHB4nlp2vw5G+Qx7MxO+V\u002FfpmkEuX+iA2t9sxXQZuX7kNLzPTlPAusoaSZ8LklBjBaXgohjowcqQq2H7afk\u002FGbYbTyYqq4zYXpbhdQNDTz9M5DWov2nRT\u002FvN8uyru1QHknNAaUajjR5JPxNDIxw=="
}], gxr2010 发表于 2024-4-30 17:11
西瓜视频中的电影好像解密方式是不一样的,"cenc-aes-ctr",能否分析一下。比如杀手之五:https://www.ixig ...
视频在这里:['anyVideo']['gidInformation']['packerData']['videoResource']['dash']['dynamic_video']['dynamic_video_list']
音频改为['dynamic_audio_list']
PTK在dash下面
一样有video_list,你搜索一下 牛逼。你也是大佬了 感谢分享值得学习学习 感谢分享,牛掰 感谢楼主分享干货 厉害了,真是技术流啊 感谢大佬的分享!先复制下来,去PYTHON上面研究学习一下! 感谢分享 感谢分享 感谢分享,太厉害了!