﻿#20240208
#20250513

import requests,bs4,sys,subprocess,os
from bs4 import BeautifulSoup


url = input('url:')


r = requests.get(url)

soup = BeautifulSoup(r.text, 'html.parser')
altName = name = soup.find_all('h1')[0].text
name += '.mp4'
print(name)

try:
    open(name).close()
    print("文件存在")
    exit()
except IOError:pass

Label = soup.find_all("img", attrs={"alt": altName})

for i in Label:
    ImgUrl = i.attrs['src']
    if 'preview.jpg' in ImgUrl:    
        downUrl = 'https://91rbnet.gslb-al.com/hls/contents/videos/{0}/{1}/{1}_1080p.mp4/index.m3u8'.format(ImgUrl.split('/')[-3], ImgUrl.split('/')[-2])

        r = requests.get(downUrl,  headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'})
        if r.status_code != 200:
            downUrl = 'https://91rbnet.gslb-al.com/hls/contents/videos/{0}/{1}/{1}_720p.mp4/index.m3u8'.format(ImgUrl.split('/')[-3], ImgUrl.split('/')[-2])
            r = requests.get(downUrl, headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36'})
            if r.status_code != 200:
                downUrl = 'https://91rbnet.gslb-al.com/hls/contents/videos/{0}/{1}/{1}.mp4/index.m3u8'.format(ImgUrl.split('/')[-3], ImgUrl.split('/')[-2])
                
        print(downUrl)
        break
    else:
        print(ImgUrl)
        print("退出")
        sys.exit()

env = os.environ.copy()
print('开始下载')

#env["PATH"] += r";D:\python\m3u8\ffmpeg-2021-01-12-git-ca21cb1e36-full_build\bin"
p = subprocess.Popen(['yt-dlp', '--no-check-certificate', '--concurrent-fragments', '32', downUrl, '-o', name], stdout=subprocess.PIPE, env=env, text=True)  

for line in p.stdout:
    print(line.strip())
        