Merge pull request #252 from harry0703/dev

fix some bugs
This commit is contained in:
Harry
2024-04-13 20:26:52 +08:00
committed by GitHub
2 changed files with 29 additions and 12 deletions

View File

@@ -5,6 +5,7 @@ from urllib.parse import urlencode
import requests import requests
from typing import List from typing import List
from loguru import logger from loguru import logger
from moviepy.video.io.VideoFileClip import VideoFileClip
from app.config import config from app.config import config
from app.models.schema import VideoAspect, VideoConcatMode, MaterialInfo 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) 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: 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 "" return ""

View File

@@ -13,15 +13,16 @@ from app.utils import utils
def get_bgm_file(bgm_type: str = "random", bgm_file: str = ""): def get_bgm_file(bgm_type: str = "random", bgm_file: str = ""):
if not bgm_type: if not bgm_type:
return "" return ""
if bgm_file and os.path.exists(bgm_file):
return bgm_file
if bgm_type == "random": if bgm_type == "random":
suffix = "*.mp3" suffix = "*.mp3"
song_dir = utils.song_dir() song_dir = utils.song_dir()
files = glob.glob(os.path.join(song_dir, suffix)) files = glob.glob(os.path.join(song_dir, suffix))
return random.choice(files) return random.choice(files)
if os.path.exists(bgm_file):
return bgm_file
return "" return ""
@@ -127,7 +128,7 @@ def wrap_text(text, max_width, font='Arial', fontsize=60):
if width <= max_width: if width <= max_width:
return text, height 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 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_] _wrapped_lines_ = [line.strip() for line in _wrapped_lines_]
result = '\n'.join(_wrapped_lines_).strip() result = '\n'.join(_wrapped_lines_).strip()
height = len(_wrapped_lines_) * height height = len(_wrapped_lines_) * height
logger.warning(f"wrapped text: {result}") # logger.warning(f"wrapped text: {result}")
return result, height return result, height
_wrapped_lines_ = [] _wrapped_lines_ = []
@@ -168,7 +169,7 @@ def wrap_text(text, max_width, font='Arial', fontsize=60):
_wrapped_lines_.append(_txt_) _wrapped_lines_.append(_txt_)
result = '\n'.join(_wrapped_lines_).strip() result = '\n'.join(_wrapped_lines_).strip()
height = len(_wrapped_lines_) * height height = len(_wrapped_lines_) * height
logger.warning(f"wrapped text: {result}") # logger.warning(f"wrapped text: {result}")
return result, height 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) bgm_file = get_bgm_file(bgm_type=params.bgm_type, bgm_file=params.bgm_file)
if bgm_file: if bgm_file:
bgm_clip = (AudioFileClip(bgm_file) try:
.set_duration(video_clip.duration) bgm_clip = (AudioFileClip(bgm_file)
.volumex(params.bgm_volume) .volumex(params.bgm_volume)
.audio_fadeout(3)) .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 = video_clip.set_audio(audio_clip)
video_clip.write_videofile(output_file, video_clip.write_videofile(output_file,
audio_codec="aac", audio_codec="aac",