Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

Reduce repeated video material usage

harry07031e56568bc203
diff --git a/app/services/video.py b/app/services/video.py
index a6bd23e2..43b32610 100644
--- a/app/services/video.py
+++ b/app/services/video.py
@@ -35,12 +35,22 @@ from app.services.utils import video_effects
from app.utils import file_security, utils
class SubClippedVideoClip:
- def __init__(self, file_path, start_time=None, end_time=None, width=None, height=None, duration=None):
+ def __init__(
+ self,
+ file_path,
+ start_time=None,
+ end_time=None,
+ width=None,
+ height=None,
+ duration=None,
+ source_file_path=None,
+ ):
self.file_path = file_path
self.start_time = start_time
self.end_time = end_time
self.width = width
self.height = height
+ self.source_file_path = source_file_path or file_path
if duration is None:
self.duration = end_time - start_time
else:
@@ -59,6 +69,49 @@ fps = 30
_BGM_EXTENSIONS = (".mp3",)
+def _prioritize_unique_source_clips(
+ subclipped_items: List[SubClippedVideoClip],
+ concat_mode: VideoConcatMode,
+) -> List[SubClippedVideoClip]:
+ """
+ 优先让每个源素材只出现一次,降低成片里同一素材反复出现的概率。
+
+ 线上素材经常会遇到“一个长视频被切成多个短片段”的情况。旧逻辑在
+ random 模式下直接打乱所有短片段,导致同一个源视频的多个切片可能
+ 分布在开头和中间,用户会感知为素材重复。本函数只调整片段顺序:
+ 先放每个源文件里最长的一个片段,剩余片段作为兜底;当素材总时长不足时,
+ 仍然允许后续片段补齐音频长度,避免破坏视频生成成功率。优先选择最长
+ 片段是为了避免随机选中视频尾部的零碎短片段,导致明明有足够素材却过早复用。
+ """
+ if not subclipped_items:
+ return []
+
+ concat_mode_value = getattr(concat_mode, "value", concat_mode)
+ if concat_mode_value != VideoConcatMode.random.value:
+ return subclipped_items
+
+ grouped_items: dict[str, list[SubClippedVideoClip]] = {}
+ for item in subclipped_items:
+ grouped_items.setdefault(item.source_file_path, []).append(item)
+
+ primary_items = []
+ overflow_items = []
+ for items in grouped_items.values():
+ primary_item = max(items, key=lambda item: item.duration)
+ primary_items.append(primary_item)
+ overflow_items.extend(item for item in items if item is not primary_item)
+
+ random.shuffle(primary_items)
+ random.shuffle(overflow_items)
+ logger.info(
+ "prioritized unique video materials, "
+ f"sources: {len(grouped_items)}, "
+ f"primary clips: {len(primary_items)}, "
+ f"fallback clips: {len(overflow_items)}"
+ )
+ return primary_items + overflow_items
+
+
def get_ffmpeg_binary():
# 优先复用用户在 config.toml / 环境变量里显式指定的 ffmpeg,可避免
# Windows 便携包、Docker、自定义安装目录等场景下 PATH 不一致。
@@ -345,6 +398,7 @@ def combine_videos(
end_time=end_time,
width=clip_w,
height=clip_h,
+ source_file_path=video_path,
)
)
@@ -352,18 +406,24 @@ def combine_videos(
if video_concat_mode.value == VideoConcatMode.sequential.value:
break
- # random subclipped_items order
- if video_concat_mode.value == VideoConcatMode.random.value:
- random.shuffle(subclipped_items)
+ subclipped_items = _prioritize_unique_source_clips(
+ subclipped_items=subclipped_items,
+ concat_mode=video_concat_mode,
+ )
logger.debug(f"total subclipped items: {len(subclipped_items)}")
# Add downloaded clips over and over until the duration of the audio (max_duration) has been reached
for i, subclipped_item in enumerate(subclipped_items):
- if video_duration > audio_duration:
+ if video_duration >= audio_duration:
break
- logger.debug(f"processing clip {i+1}: {subclipped_item.width}x{subclipped_item.height}, current duration: {video_duration:.2f}s, remaining: {audio_duration - video_duration:.2f}s")
+ logger.debug(
+ f"processing clip {i+1}: {subclipped_item.width}x{subclipped_item.height}, "
+ f"source: {os.path.basename(subclipped_item.source_file_path)}, "
+ f"current duration: {video_duration:.2f}s, "
+ f"remaining: {audio_duration - video_duration:.2f}s"
+ )
try:
clip = _open_video_clip_quietly(subclipped_item.file_path).subclipped(
@@ -424,7 +484,15 @@ def combine_videos(
clip_duration_saved = clip.duration
close_clip(clip)
- processed_clips.append(SubClippedVideoClip(file_path=clip_file, duration=clip_duration_saved, width=clip_w, height=clip_h))
+ processed_clips.append(
+ SubClippedVideoClip(
+ file_path=clip_file,
+ duration=clip_duration_saved,
+ width=clip_w,
+ height=clip_h,
+ source_file_path=subclipped_item.source_file_path,
+ )
+ )
video_duration += clip_duration_saved
except Exception as e:
diff --git a/test/services/test_video.py b/test/services/test_video.py
index 225e39d1..0bf558f4 100644
--- a/test/services/test_video.py
+++ b/test/services/test_video.py
@@ -276,6 +276,70 @@ class TestVideoService(unittest.TestCase):
video_transition_mode=None,
)
self.assertEqual(result, combined_video_path)
+
+ def test_prioritize_unique_source_clips_uses_each_source_before_reuse(self):
+ """
+ 随机模式下,一个长素材会被拆成多个片段。调度层应先让每个源素材
+ 至少出现一次,再使用同一源素材的其他切片,降低用户感知到的重复。
+ """
+ clips = [
+ vd.SubClippedVideoClip("a.mp4", 0, 4, source_file_path="a.mp4"),
+ vd.SubClippedVideoClip("a.mp4", 4, 8, source_file_path="a.mp4"),
+ vd.SubClippedVideoClip("b.mp4", 0, 4, source_file_path="b.mp4"),
+ vd.SubClippedVideoClip("b.mp4", 4, 8, source_file_path="b.mp4"),
+ vd.SubClippedVideoClip("c.mp4", 0, 4, source_file_path="c.mp4"),
+ ]
+
+ ordered_clips = vd._prioritize_unique_source_clips(
+ subclipped_items=clips,
+ concat_mode=vd.VideoConcatMode.random,
+ )
+
+ self.assertCountEqual(ordered_clips, clips)
+ first_round_sources = [clip.source_file_path for clip in ordered_clips[:3]]
+ self.assertCountEqual(first_round_sources, ["a.mp4", "b.mp4", "c.mp4"])
+
+ def test_prioritize_unique_source_clips_keeps_sequential_order(self):
+ """
+ 顺序模式本身只取每个素材的首段,不应被随机调度逻辑改变顺序。
+ """
+ clips = [
+ vd.SubClippedVideoClip("a.mp4", 0, 4, source_file_path="a.mp4"),
+ vd.SubClippedVideoClip("b.mp4", 0, 4, source_file_path="b.mp4"),
+ vd.SubClippedVideoClip("c.mp4", 0, 4, source_file_path="c.mp4"),
+ ]
+
+ ordered_clips = vd._prioritize_unique_source_clips(
+ subclipped_items=clips,
+ concat_mode=vd.VideoConcatMode.sequential,
+ )
+
+ self.assertEqual(ordered_clips, clips)
+
+ def test_prioritize_unique_source_clips_prefers_long_primary_clip(self):
+ """
+ 同一个源素材的最后一个切片可能短于目标片段时长。首轮去重时应优先
+ 选择较长片段,否则会因为累计时长不足而提前复用素材。
+ """
+ short_tail = vd.SubClippedVideoClip(
+ "a.mp4", 6, 6.5, source_file_path="a.mp4"
+ )
+ full_clip = vd.SubClippedVideoClip(
+ "a.mp4", 0, 3, source_file_path="a.mp4"
+ )
+ other_source = vd.SubClippedVideoClip(
+ "b.mp4", 0, 3, source_file_path="b.mp4"
+ )
+
+ ordered_clips = vd._prioritize_unique_source_clips(
+ subclipped_items=[short_tail, full_clip, other_source],
+ concat_mode=vd.VideoConcatMode.random,
+ )
+
+ first_a_clip = next(
+ clip for clip in ordered_clips if clip.source_file_path == "a.mp4"
+ )
+ self.assertEqual(first_a_clip, full_clip)
def test_wrap_text(self):
"""test text wrapping function"""