import os import re import subprocess import requests from bs4 import BeautifulSoup from playwright.sync_api import sync_playwright
class YoukuVideoDownloader: def __init__(self): pass
def extract_id_from_url(self, url): """从优酷URL中提取视频ID""" pattern = r'id_([^\.]+)\.html' match = re.search(pattern, url) if match: return match.group(1) else: return None
def merge_videos(self, video_files, output_filename): """合并视频文件""" with open('input_files.txt', 'w') as f: for file in video_files: f.write("file '{}'\n".format(file))
print(output_filename) ffmpeg_command = [ 'ffmpeg.exe', '-y', '-f', 'concat', '-safe', '0', '-i', 'input_files.txt', '-c', 'copy', output_filename ]
result = subprocess.run(ffmpeg_command, shell=True)
os.remove('input_files.txt')
for file in video_files: os.remove(file)
def download_and_merge(self, url, title): video_id = self.extract_id_from_url(url) if not video_id: print("无法获取视频ID,请检查URL是否正确") return
download_dir = "download" if not os.path.exists(download_dir): os.makedirs(download_dir)
firefox_executable_path = "firefox-1440"
with sync_playwright() as p: browser = p.firefox.launch(headless=True) page = browser.new_page()
page.goto('https://player.youku.com/embed/' + video_id)
page.wait_for_timeout(5000)
html_content = page.content().encode('utf-8')
soup = BeautifulSoup(html_content, 'lxml')
video_divs = soup.find_all('video') if video_divs: video_files = [] for index, video_div in enumerate(video_divs): mp4_url = video_div.get('src') or video_div.get('data-orginal-src') if mp4_url: response = requests.get(mp4_url, stream=True) total_length = int(response.headers.get('content-length')) filename = os.path.join(download_dir, f"video_{video_id}_{index}.mp4") with open(filename, 'wb') as f: downloaded = 0 for chunk in response.iter_content(chunk_size=4096): if chunk: f.write(chunk) downloaded += len(chunk) print("Download", downloaded, total_length)
video_files.append(filename)
if video_files: output_filename = os.path.join(download_dir, f"video_{title}_merged.mp4") self.merge_videos(video_files, output_filename) print(f"合并后的视频已保存为 {output_filename}", "success") else: print("没有可用的视频链接,无法合并视频", "error") else: print("未找到可用的视频标签或此视频需要VIP", "error")
browser.close()
youku = YoukuVideoDownloader() info = "甜心格格" with open("1.txt", "r", encoding="utf-8") as f: for index,line in enumerate(f.readlines()): line = line.strip() if line: youku.download_and_merge(line, info+"_"+str(index))
|