Browse Source

读写文件优化

zhangyong 1 year ago
parent
commit
ee8d25f2f2
2 changed files with 22 additions and 30 deletions
  1. 9 13
      common/material.py
  2. 13 17
      video_stitching/video_stitching.py

+ 9 - 13
common/material.py

@@ -47,19 +47,15 @@ class Material():
         list = []
         for row in audio[1:]:
             account_name = row[0]
-            list.append(account_name)
-            srt = row[2]
-            if srt == None:
-                continue
-            file_path = f"./video_stitching/video/{account_name}.srt"
-            if os.path.isfile(file_path):
-                continue
-            else:
-                with open(file_path, 'w') as file:
-                    # 写入内容到文件中
-                    file.write(srt)
-
-        return list
+            text = row[2]
+            if text == None:
+                print(1)
+            number = {"audio_id": account_name,"text": text}
+            list.append(number)
+        list = random.choice(list)
+        audio_id = list['audio_id']
+        srt = list['text']
+        return audio_id, srt
 
     # 获取抖音 cookie
     @classmethod

+ 13 - 17
video_stitching/video_stitching.py

@@ -3,6 +3,7 @@
 import datetime
 import random
 import os
+import re
 import sys
 import time
 import resource
@@ -181,25 +182,21 @@ class VideoStitching():
         color_clip = editor.ColorClip(size=(final_width, 120),
                                       color=(255, 255, 0)).set_duration(duration_limit)
         final_clip = editor.CompositeVideoClip([final_clip, color_clip.set_position(("center", final_height - 100))])
-        # 处理SRT字幕文件
-        subtitle_file = f"./video_stitching/video/{srt}.srt"
-        if os.path.isfile(subtitle_file):
+        if srt != None:
             Common.logger().info(f"处理SRT字幕文件")
-            with open(subtitle_file, 'r') as file:
-                subtitles = file.read().strip().split('\n\n')
+            # 使用正则表达式提取时间码和字幕内容
+            pattern = r"(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})\n([\s\S]+?(?=\n\d|$))"
+            matches = re.findall(pattern, srt)
+            Common.logger().info(f"字幕{matches}")
             # 从SRT字幕文件中获取字幕
             subtitle_clips = []
-            for subtitle in subtitles:
-                # 按行分割字幕内容
-                subtitle_lines = subtitle.strip().split('\n')
-                # 提取时间轴信息和字幕文本
-                if len(subtitle_lines) >= 3:
-                    times, text = subtitle_lines[1], '\n'.join(subtitle_lines[2:])
-                    start, end = map(cls.srt_to_seconds, times.split(' --> '))
-                    start = editor.cvsecs(start)
-                    end = editor.cvsecs(end)
+            for match in matches:
+                start = match[0]
+                end = match[1]
+                text = match[2].strip()
                 text = cls.split_text(text, 10)
                 # /System/Library/Fonts/Hiragino Sans GB.ttc 本地字体
+                # /usr/share/fonts/truetype/wqy/wqy-zenhei.ttc 服务器地址
                 Common.logger().info(f"字幕:{text}")
                 sub = editor.TextClip(text, font="/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc",
                                       fontsize=30, color="black").set_duration(end - start).set_start(
@@ -274,10 +271,9 @@ class VideoStitching():
     def video_stitching(cls):
         cookie = Material.get_houtai_cookie()
         # 获取音频
-        audioid = Material.get_audio()
+        audio_id, srt = Material.get_audio()
         # 获取已入库的用户id
         account_id = cls.get_account_id()
-        audio_id = random.choice(audioid)
         account = random.choice(account_id)
         account = str(account).replace('(', '').replace(')', '').replace(',', '')
         Common.logger().info(f"获取用户ID:{account}")
@@ -293,7 +289,7 @@ class VideoStitching():
         videos = Oss.get_oss_url(videos)
         # 视频截取
         try:
-            audio_url, video_with_subtitles = cls.concatenate_videos(videos, str(audio), audio_id)
+            audio_url, video_with_subtitles = cls.concatenate_videos(videos, str(audio), srt)
             if len(audio_url) == 0:
                 Common.logger().info(f"视频生成失败")
             # 随机生成视频id