音、视频分开的情况,需要自己配置FFMPEG路径。没有弄合集下载,有兴趣的大佬,请帮弄好贴上来,谢谢。
[Python] 纯文本查看 复制代码'''
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=(.*?)', res_html)[0]
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/video ... =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')