diff --git a/app/services/material.py b/app/services/material.py index 0f16a2e..e2a65cc 100644 --- a/app/services/material.py +++ b/app/services/material.py @@ -5,6 +5,7 @@ from urllib.parse import urlencode import requests from typing import List from loguru import logger +from moviepy.video.io.VideoFileClip import VideoFileClip from app.config import config from app.models.schema import VideoAspect, VideoConcatMode, MaterialInfo @@ -105,7 +106,19 @@ def save_video(video_url: str, save_dir: str = "") -> str: f.write(requests.get(video_url, proxies=proxies, verify=False, timeout=(60, 240)).content) if os.path.exists(video_path) and os.path.getsize(video_path) > 0: - return video_path + try: + clip = VideoFileClip(video_path) + duration = clip.duration + fps = clip.fps + clip.close() + if duration > 0 and fps > 0: + return video_path + except Exception as e: + try: + os.remove(video_path) + except Exception as e: + pass + logger.warning(f"invalid video file: {video_path} => {str(e)}") return "" diff --git a/app/services/video.py b/app/services/video.py index 8a5ccc2..3a6a61b 100644 --- a/app/services/video.py +++ b/app/services/video.py @@ -13,15 +13,16 @@ from app.utils import utils def get_bgm_file(bgm_type: str = "random", bgm_file: str = ""): if not bgm_type: return "" + + if bgm_file and os.path.exists(bgm_file): + return bgm_file + if bgm_type == "random": suffix = "*.mp3" song_dir = utils.song_dir() files = glob.glob(os.path.join(song_dir, suffix)) return random.choice(files) - if os.path.exists(bgm_file): - return bgm_file - return "" @@ -127,7 +128,7 @@ def wrap_text(text, max_width, font='Arial', fontsize=60): if width <= max_width: return text, height - logger.warning(f"wrapping text, max_width: {max_width}, text_width: {width}, text: {text}") + # logger.warning(f"wrapping text, max_width: {max_width}, text_width: {width}, text: {text}") processed = True @@ -151,7 +152,7 @@ def wrap_text(text, max_width, font='Arial', fontsize=60): _wrapped_lines_ = [line.strip() for line in _wrapped_lines_] result = '\n'.join(_wrapped_lines_).strip() height = len(_wrapped_lines_) * height - logger.warning(f"wrapped text: {result}") + # logger.warning(f"wrapped text: {result}") return result, height _wrapped_lines_ = [] @@ -168,7 +169,7 @@ def wrap_text(text, max_width, font='Arial', fontsize=60): _wrapped_lines_.append(_txt_) result = '\n'.join(_wrapped_lines_).strip() height = len(_wrapped_lines_) * height - logger.warning(f"wrapped text: {result}") + # logger.warning(f"wrapped text: {result}") return result, height @@ -245,12 +246,15 @@ def generate_video(video_path: str, bgm_file = get_bgm_file(bgm_type=params.bgm_type, bgm_file=params.bgm_file) if bgm_file: - bgm_clip = (AudioFileClip(bgm_file) - .set_duration(video_clip.duration) - .volumex(params.bgm_volume) - .audio_fadeout(3)) + try: + bgm_clip = (AudioFileClip(bgm_file) + .volumex(params.bgm_volume) + .audio_fadeout(3)) + bgm_clip = afx.audio_loop(bgm_clip, duration=video_clip.duration) + audio_clip = CompositeAudioClip([audio_clip, bgm_clip]) + except Exception as e: + logger.error(f"failed to add bgm: {str(e)}") - audio_clip = CompositeAudioClip([audio_clip, bgm_clip]) video_clip = video_clip.set_audio(audio_clip) video_clip.write_videofile(output_file, audio_codec="aac",