zhangyong 4 月之前
父节点
当前提交
65e5cd3dc1
共有 2 个文件被更改,包括 30 次插入10 次删除
  1. 1 1
      carry_video/carry_video.py
  2. 29 9
      common/download_video.py

+ 1 - 1
carry_video/carry_video.py

@@ -290,7 +290,7 @@ class CarryViode:
             return
 
         logger.info(f"[+] {url}开始下载视频")
-        video_path = DownLoad.download_video(url, file_path)
+        video_path = DownLoad.download_video(url, file_path, tag_transport_channel)
         # video_path = Oss.download_video_oss(url, file_path)
         if not os.path.exists(video_path) or os.path.getsize(video_path) == 0:
             in_carry_video_data(REDIS_NAME, json.dumps(data, ensure_ascii=False, indent=4))

+ 29 - 9
common/download_video.py

@@ -1,20 +1,40 @@
 import html
 import os
+from urllib.parse import urlparse
+
 import requests
 
 
 class DownLoad:
     @classmethod
-    def download_video(cls, video_url, video_path_url):
+    def download_video(cls, video_url, video_path_url, tag_transport_channel):
         video = video_path_url + 'video.mp4'
-        url_type = html.unescape(video_url).split('?')[0]
-        if ".m3u8" in url_type:
-            ts_video = video_path_url + 'video.ts'
-            status_code = cls.download_m3u8_video(video_url, ts_video)
-            if status_code == False:
-                return video
-            cls.convert_ts_to_mp4(ts_video, video)
-            return video
+        if tag_transport_channel == "抖音":
+            headers = {
+                'accept': '*/*',
+                'accept-encoding': 'identity;q=1, *;q=0',
+                'accept-language': 'zh-CN,zh;q=0.9',
+                'cache-control': 'no-cache',
+                'connection': 'keep-alive',
+                'host': urlparse(video_url).netloc,
+                'pragma': 'no-cache',
+                'range': 'bytes=0-',
+                'referer': video_url,
+                'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
+            }
+            payload = {}
+            for i in range(3):
+                try:
+                    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)
+                        return video
+                except Exception:
+                    return video
         else:
             try:
                 for i in range(3):