123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import html
- import subprocess
- import time
- import requests
- class DownLoad:
- @classmethod
- def download_video(cls, video_url, video_path_url):
- video = video_path_url + 'video.mp4'
- url_type = html.unescape(video_url).split('?')[0]
- if ".m3u8" in url_type:
- ffmpeg_cmd_oss = [
- "ffmpeg",
- "-y", # 覆盖输出文件
- "-i", video_url, # 输入文件
- "-c", "copy", # 复制视频流
- "-bsf:a", "aac_adtstoasc", # 转换 AAC 音频格式
- video # 输出文件
- ]
- # 使用 subprocess 运行命令
- subprocess.run(ffmpeg_cmd_oss, capture_output=True, text=True)
- return video
- else:
- try:
- for i in range(3):
- payload = {}
- headers = {}
- response = requests.request("GET", video_url, headers=headers, data=payload, timeout=240)
- if response.status_code == 200:
- # 以二进制写入模式打开文件
- with open(f"{video}", "wb") as file:
- # 将响应内容写入文件
- file.write(response.content)
- time.sleep(5)
- return video
- return None
- except Exception:
- return None
|