mirror of
https://github.com/linyqh/NarratoAI.git
synced 2025-12-11 18:42:49 +00:00
feat(test): 添加 moviepy 库视频剪辑测试脚本
- 新增 test_moviepy.py 文件,实现使用 moviepy 库剪辑指定时间戳视频的功能 - 添加时间字符串转换函数、格式化时长函数和视频剪辑函数- 编写主函数以测试视频剪辑功能
This commit is contained in:
parent
94b983a545
commit
177304aec0
63
app/test/test_moviepy.py
Normal file
63
app/test/test_moviepy.py
Normal file
@ -0,0 +1,63 @@
|
||||
"""
|
||||
使用 moviepy 库剪辑指定时间戳视频
|
||||
"""
|
||||
|
||||
from moviepy.editor import VideoFileClip
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def time_str_to_seconds(time_str: str) -> float:
|
||||
"""
|
||||
将时间字符串转换为秒数
|
||||
参数:
|
||||
time_str: 格式为"MM:SS"的时间字符串
|
||||
返回:
|
||||
转换后的秒数
|
||||
"""
|
||||
time_obj = datetime.strptime(time_str, "%M:%S")
|
||||
return time_obj.minute * 60 + time_obj.second
|
||||
|
||||
|
||||
def format_duration(seconds: float) -> str:
|
||||
"""
|
||||
将秒数转换为可读的时间格式
|
||||
参数:
|
||||
seconds: 秒数
|
||||
返回:
|
||||
格式化的时间字符串 (MM:SS)
|
||||
"""
|
||||
minutes = int(seconds // 60)
|
||||
remaining_seconds = int(seconds % 60)
|
||||
return f"{minutes:02d}:{remaining_seconds:02d}"
|
||||
|
||||
|
||||
def cut_video(video_path: str, start_time: str, end_time: str) -> None:
|
||||
"""
|
||||
剪辑视频
|
||||
参数:
|
||||
video_path: 视频文件路径
|
||||
start_time: 开始时间 (格式: "MM:SS")
|
||||
end_time: 结束时间 (格式: "MM:SS")
|
||||
"""
|
||||
# 转换时间字符串为秒数
|
||||
start_seconds = time_str_to_seconds(start_time)
|
||||
end_seconds = time_str_to_seconds(end_time)
|
||||
|
||||
# 加载视频文件
|
||||
video = VideoFileClip(video_path)
|
||||
|
||||
# 计算剪辑时长
|
||||
clip_duration = end_seconds - start_seconds
|
||||
print(f"原视频总长度: {format_duration(video.duration)}")
|
||||
print(f"剪辑时长: {format_duration(clip_duration)}")
|
||||
|
||||
# 剪辑视频
|
||||
video = video.subclip(start_seconds, end_seconds)
|
||||
video.write_videofile("../../resource/videos/cut_video2.mp4")
|
||||
|
||||
# 释放资源
|
||||
video.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
cut_video("../../resource/videos/best.mp4", "00:40", "02:40")
|
||||
Loading…
x
Reference in New Issue
Block a user