优化剪辑方法,新增语调

This commit is contained in:
linyqh 2024-11-06 02:15:44 +08:00
parent e926e8676a
commit 242f8d5355
3 changed files with 32 additions and 18 deletions

View File

@ -275,10 +275,19 @@ def save_clip_video(timestamp: str, origin_video: str, save_dir: str = "") -> di
logger.info(f"video already exists: {video_path}") logger.info(f"video already exists: {video_path}")
return {timestamp: video_path} return {timestamp: video_path}
try:
# 剪辑视频 # 剪辑视频
start, end = utils.split_timestamp(timestamp) start, end = utils.split_timestamp(timestamp)
video = VideoFileClip(origin_video).subclip(start, end) video = VideoFileClip(origin_video).subclip(start, end)
video.write_videofile(video_path, logger=None) # 禁用 MoviePy 的内置日志
# 检查视频是否有音频轨道
if video.audio is not None:
video.write_videofile(video_path, logger=None) # 有音频时正常处理
else:
# 没有音频时使用不同的写入方式
video.write_videofile(video_path, audio=False, logger=None)
video.close() # 确保关闭视频文件
if os.path.getsize(video_path) > 0 and os.path.exists(video_path): if os.path.getsize(video_path) > 0 and os.path.exists(video_path):
try: try:
@ -289,11 +298,14 @@ def save_clip_video(timestamp: str, origin_video: str, save_dir: str = "") -> di
if duration > 0 and fps > 0: if duration > 0 and fps > 0:
return {timestamp: video_path} return {timestamp: video_path}
except Exception as e: except Exception as e:
try: logger.warning(f"视频文件验证失败: {video_path} => {str(e)}")
if os.path.exists(video_path):
os.remove(video_path) os.remove(video_path)
except Exception as e: except Exception as e:
logger.warning(str(e)) logger.warning(f"视频剪辑失败: {str(e)}")
logger.warning(f"无效的视频文件: {video_path}") if os.path.exists(video_path):
os.remove(video_path)
return {} return {}

View File

@ -372,6 +372,7 @@ def start_subclip(task_id: str, params: VideoClipParams, subclip_path_videos: li
list_script=list_script, list_script=list_script,
voice_name=voice_name, voice_name=voice_name,
voice_rate=params.voice_rate, voice_rate=params.voice_rate,
voice_pitch=params.voice_pitch,
force_regenerate=True force_regenerate=True
) )
if audio_files is None: if audio_files is None:

View File

@ -1403,7 +1403,7 @@ def get_audio_duration(sub_maker: submaker.SubMaker):
return sub_maker.offset[-1][1] / 10000000 return sub_maker.offset[-1][1] / 10000000
def tts_multiple(task_id: str, list_script: list, voice_name: str, voice_rate: float, force_regenerate: bool = True): def tts_multiple(task_id: str, list_script: list, voice_name: str, voice_rate: float, voice_pitch: float, force_regenerate: bool = True):
""" """
根据JSON文件中的多段文本进行TTS转换 根据JSON文件中的多段文本进行TTS转换
@ -1437,7 +1437,8 @@ def tts_multiple(task_id: str, list_script: list, voice_name: str, voice_rate: f
text=text, text=text,
voice_name=voice_name, voice_name=voice_name,
voice_rate=voice_rate, voice_rate=voice_rate,
voice_file=audio_file voice_pitch=voice_pitch,
voice_file=audio_file,
) )
if sub_maker is None: if sub_maker is None: