优化剪辑方法,新增语调

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,25 +275,37 @@ def save_clip_video(timestamp: str, origin_video: str, save_dir: str = "") -> di
logger.info(f"video already exists: {video_path}")
return {timestamp: video_path}
# 剪辑视频
start, end = utils.split_timestamp(timestamp)
video = VideoFileClip(origin_video).subclip(start, end)
video.write_videofile(video_path, logger=None) # 禁用 MoviePy 的内置日志
try:
# 剪辑视频
start, end = utils.split_timestamp(timestamp)
video = VideoFileClip(origin_video).subclip(start, end)
# 检查视频是否有音频轨道
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):
try:
clip = VideoFileClip(video_path)
duration = clip.duration
fps = clip.fps
clip.close()
if duration > 0 and fps > 0:
return {timestamp: video_path}
except Exception as e:
if os.path.getsize(video_path) > 0 and os.path.exists(video_path):
try:
os.remove(video_path)
clip = VideoFileClip(video_path)
duration = clip.duration
fps = clip.fps
clip.close()
if duration > 0 and fps > 0:
return {timestamp: video_path}
except Exception as e:
logger.warning(str(e))
logger.warning(f"无效的视频文件: {video_path}")
logger.warning(f"视频文件验证失败: {video_path} => {str(e)}")
if os.path.exists(video_path):
os.remove(video_path)
except Exception as e:
logger.warning(f"视频剪辑失败: {str(e)}")
if os.path.exists(video_path):
os.remove(video_path)
return {}

View File

@ -372,6 +372,7 @@ def start_subclip(task_id: str, params: VideoClipParams, subclip_path_videos: li
list_script=list_script,
voice_name=voice_name,
voice_rate=params.voice_rate,
voice_pitch=params.voice_pitch,
force_regenerate=True
)
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
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转换
@ -1437,7 +1437,8 @@ def tts_multiple(task_id: str, list_script: list, voice_name: str, voice_rate: f
text=text,
voice_name=voice_name,
voice_rate=voice_rate,
voice_file=audio_file
voice_pitch=voice_pitch,
voice_file=audio_file,
)
if sub_maker is None: