西瓜视频下载(支持单个视频以及合集)
1、配置python环境,安装相应的库2、安装ffmpeg,用于音频和视频合并(因为西瓜视频的音频和视频是分开的,需要各自下载,然后合并)
3、如有疑问,可以随意留言,一起讨论
import base64
import json
import os.path
import re
import subprocess
import requests
headers = {
"cookie": "ixigua-a-s=0; __ac_nonce=062711eef0074c53fd301; "
"__ac_signature=_02B4Z6wo00f01ixJMjwAAIDDB8Np-ahvLqYsaTaAAOlxwFWsX1Khk-KvFlM1N2JBoEUa-eVrLxEyF"
".bLdz61qjeQ-CTdvApqa7NKn7HTvbu1Nktww-QaXyO3gNQSkIdrfp3XdUkGrdCwrQKpea; "
"MONITOR_WEB_ID=6364cae8-2aa7-4531-9a30-1b98b0ee7596; "
"ttwid=1%7CTOw3V-1qPgQfcCT4S0Hhey26Q4PJq9J6nGaLq6GMe14%7C1651580656"
"%7C48653bc0c5afd31f1a919f9bd17a80173a1ea69d96b23359b103c4f078ef969b; support_webp=true; "
"support_avif=true; _tea_utm_cache_1300=undefined",
"Referer": "https://www.ixigua.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/86.0.4240.75 Safari/537.36"
}
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'):
downloadFile(video_url, title + '.mp4')
downloadFile(audio_url, title + '.mp3')
# 使用ffmpeg进行音频视频合并 替换自己电脑ffmpeg的安装地址
# D:/JetBrains/ffmpeg-5.0.1/ffmpeg-5.0.1-essentials_build/bin/ffmpeg
command = f'D:/JetBrains/ffmpeg-5.0.1/ffmpeg-5.0.1-essentials_build/bin/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')
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_url = \
json_data['anyVideo']['gidInformation']['packerData']['video']['videoResource']['dash'][
'dynamic_video'][
'dynamic_video_list'][-1]['main_url']
audio_url = \
json_data['anyVideo']['gidInformation']['packerData']['video']['videoResource']['dash'][
'dynamic_video'][
'dynamic_audio_list'][-1]['main_url']
title = json_data['anyVideo']['gidInformation']['packerData']['video']['title']
title = re.sub(r"[\/\\\:\*\?\"\<\>\|]", "_", title).replace(" ", "")
video_url = base64.b64decode(video_url).decode()
audio_url = base64.b64decode(audio_url).decode()
downloadAll(video_url, audio_url, title)
else:
json_data = json.loads(res_html)
for item in json_data['data']:
title = item['title']
# backup_url_1
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/6989911204554605064
main('https://www.ixigua.com/6989911204554605064')
# 合集视频下载
# 原链接: https://www.ixigua.com/6888602680952881676?id=6887944298654859780
# 将6888602680952881676 替换到下面链接中
main('https://www.ixigua.com/api/videov2/pseries_more_v2?pSeriesId=6888602680952881676&rank=0&tailCount=30')
用了you-get,会了。 视频地址:http://v9-xg-web-s.ixigua.com/9ed01176fe1ce4f5d4dfcc5dfc2fad4b/629405d4/video/tos/cn/tos-cn-o-0004/ac398b7972004d38a4f32bd929b0d4fb/media-video-avc1/?a=1768&ch=0&cr=0&dr=0&er=0&lr=default&cd=0%7C0%7C0%7C0&br=3787&bt=3787&cs=0&ds=4&mime_type=video_mp4&qs=0&rc=OThlNmY3ZzQ3ZGY8PDo8M0BpMzx2PDs6ZjU2OTMzNDczM0AzNV4uNS8wXmIxLTVjXmBjYSMtYGpmcjRncHBgLS1kLS9zcw%3D%3D&l=2022053006440501015902516206A0B30D
音频地址:http://v9-xg-web-s.ixigua.com/8423c209587cb22a163e45eee1132b58/629405d4/video/tos/cn/tos-cn-o-0004/ac398b7972004d38a4f32bd929b0d4fb/media-audio-und-mp4a/?a=1768&ch=0&cr=0&dr=0&er=0&lr=default&cd=0%7C0%7C0%7C0&br=0&bt=0&cs=0&mime_type=video_mp4&qs=0&rc=QGkzPHY8OzpmNTY5MzM0NzMzQGEjLWBqZnI0Z3BwYC0tZC0vc3M%3D&l=2022053006440501015902516206A0B30D
感谢分享 学习一下,感谢分享。 @封心棒棒糖 请问下载的是最高清晰度吗?
另外,我之前使用某解析网站解析西瓜视频,发现有视频和音频合在一起的下载地址的呀 guyinqian 发表于 2022-5-14 07:05
@封心棒棒糖 请问下载的是最高清晰度吗?
另外,我之前使用某解析网站解析西瓜视频,发现有视频和音频合在 ...
默认下载最高清晰度,视频和音频不在一起的 这个是真香,感谢 这个源码如何使用啊? 谢谢分享,已经用上了,待后续体验 有谁愿意详细教我一下啊,非电脑小白,一学就会。