Bladeren bron

自动获取srt

zhangyong 10 maanden geleden
bovenliggende
commit
4562c3bac6
2 gewijzigde bestanden met toevoegingen van 82 en 2 verwijderingen
  1. 61 0
      common/srt.py
  2. 21 2
      video_agc/agc_video_method.py

+ 61 - 0
common/srt.py

@@ -0,0 +1,61 @@
+import re
+
+import requests
+import json
+
+class SRT():
+
+
+    @classmethod
+    # 定义函数去掉文字后的标点符号并添加换行符
+    def process_srt(cls, srt):
+        lines = srt.strip().split('\n')
+        processed_lines = []
+
+        for line in lines:
+            # 匹配字幕编号行
+            if re.match(r'^\d+$', line):
+                processed_lines.append(line)
+            # 匹配时间戳行
+            elif re.match(r'^\d{2}:\d{2}:\d{2}\.\d{1,3}-->\d{2}:\d{2}:\d{2}\.\d{1,3}$', line):
+                processed_lines.append(line.replace('-->', ' --> '))
+            # 处理字幕文本行
+            else:
+                # 去掉末尾的标点符号
+                line = re.sub(r'[,。!?;、]$', '', line)
+                # 添加换行符
+                processed_lines.append(line + '\n')
+
+        return '\n'.join(processed_lines)
+
+    @classmethod
+    def getSrt(cls, mp3_id):
+        url = "http://preapi-internal.piaoquantv.com/produce-center/srt/get/content"
+
+        payload = json.dumps({
+            "params": {
+                "resourceChannel": "inner",
+                "videoId": mp3_id
+            }
+        })
+        headers = {
+            'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
+            'Content-Type': 'application/json',
+            'Accept': '*/*',
+            'Host': 'preapi-internal.piaoquantv.com',
+            'Connection': 'keep-alive'
+        }
+
+        response = requests.request("POST", url, headers=headers, data=payload)
+        data_list = response.json()
+        code = data_list["code"]
+        if code == 0:
+            srt = data_list["data"]
+            srt = srt.replace("/n", "\n")
+            # srt = re.sub(r'(\w+)([,。!?])', r'\n\n', srt)
+            new_srt = cls.process_srt(srt)
+            return new_srt
+        else:
+            return ""
+
+

+ 21 - 2
video_agc/agc_video_method.py

@@ -13,10 +13,12 @@ import requests
 from datetime import datetime, timedelta
 from urllib.parse import urlencode
 
+
 sys.path.append(os.getcwd())
 from common.db import MysqlHelper
 from common.material import Material
 from common import Common, Oss, Feishu
+from common.srt import SRT
 
 config = configparser.ConfigParser()
 config.read('./config.ini')  # 替换为您的配置文件路径
@@ -604,7 +606,7 @@ class AgcVidoe():
         s_path, v_path, video_path_url, v_oss_path = cls.create_folders(mark)
 
         kb_count = int(result[1][1])
-        channel = ['douyin', 'kuaishou', 'koubo']
+        channel = ['kuaishou', 'douyin', 'koubo']
         try:
             for platform in channel:
                 limit_count = 35
@@ -631,11 +633,16 @@ class AgcVidoe():
                 videos = [list(item) for item in url_list]
                 # 下载视频
                 videos = Oss.get_oss_url(videos, video_path_url)
-
                 if srt:
                     # 创建临时字幕文件
                     cls.create_subtitle_file(srt, s_path)
                     Common.logger("video").info(f"S{mark}的{platform}渠道RT 文件目录创建成功")
+                else:
+                    srt_new = SRT.getSrt(int(uid))
+                    if srt_new:
+                        # 创建临时字幕文件
+                        cls.create_subtitle_file(srt_new, s_path)
+                        Common.logger("gs_video").info(f"S{mark}的{platform}渠道RT 文件目录创建成功")
                 # 获取音频
                 audio_video = cls.get_audio_url(uid, mark, mark_name)
                 Common.logger("video").info(f"{mark}的{platform}渠道获取需要拼接的音频成功")
@@ -753,6 +760,12 @@ class AgcVidoe():
                 # 创建临时字幕文件
                 cls.create_subtitle_file(srt, s_path)
                 Common.logger("gs_video").info(f"S{mark}的{platform}渠道RT 文件目录创建成功")
+            else:
+                srt_new = SRT.getSrt(int(uid))
+                if srt_new:
+                    # 创建临时字幕文件
+                    cls.create_subtitle_file(srt_new, s_path)
+                    Common.logger("gs_video").info(f"S{mark}的{platform}渠道RT 文件目录创建成功")
             # 获取音频
             audio_video = cls.get_audio_url(uid, mark, mark_name)
             Common.logger("gs_video").info(f"{mark}的{platform}渠道获取需要拼接的音频成功")
@@ -839,6 +852,12 @@ class AgcVidoe():
                     # 创建临时字幕文件
                     cls.create_subtitle_file(srt, s_path)
                     Common.logger("bk_video").info(f"S{mark} 文件目录创建成功")
+                else:
+                    srt_new = SRT.getSrt(int(uid))
+                    if srt_new:
+                        # 创建临时字幕文件
+                        cls.create_subtitle_file(srt_new, s_path)
+                        Common.logger("gs_video").info(f"S{mark}的{platform}渠道RT 文件目录创建成功")
                     # 获取音频
                 audio_video = cls.get_audio_url(uid, mark, mark_name)
                 Common.logger("bk_video").info(f"{mark}获取需要拼接的音频成功")