Spaces:
Build error
Build error
| #### 视频重复代码 | |
| #### 视频长度超过音频 | |
| from moviepy.editor import VideoFileClip, concatenate_videoclips | |
| def repeat_video_to_duration(input_path, output_path, target_duration): | |
| # 加载视频 | |
| clip = VideoFileClip(input_path) | |
| # 获取视频的时长 | |
| clip_duration = clip.duration | |
| # 如果目标时长小于视频时长,则对视频进行切片 | |
| if target_duration < clip_duration: | |
| final_clip = clip.subclip(0, target_duration) | |
| else: | |
| # 计算需要重复的次数 | |
| repeat_times = int(target_duration // clip_duration) | |
| remaining_duration = target_duration % clip_duration | |
| # 重复视频 | |
| repeated_clips = [clip] * repeat_times | |
| # 如果有剩余的时长,则添加一部分视频 | |
| if remaining_duration > 0: | |
| remaining_clip = clip.subclip(0, remaining_duration) | |
| repeated_clips.append(remaining_clip) | |
| # 拼接所有视频片段 | |
| final_clip = concatenate_videoclips(repeated_clips) | |
| # 保存结果 | |
| final_clip.write_videofile(output_path, codec='libx264') | |
| # 示例使用 | |
| input_path = 'taizong.mp4' | |
| output_path = 'taizong_15.mp4' | |
| target_duration = 15 # 目标时长为30秒 | |
| repeat_video_to_duration(input_path, output_path, target_duration) | |
| from moviepy.editor import ImageClip | |
| def create_static_video_from_image(image_path, output_path, duration, fps=30): | |
| """ | |
| 将图片生成一个指定时长的静态视频 | |
| :param image_path: 图片路径 | |
| :param output_path: 输出视频路径 | |
| :param duration: 视频时长(秒) | |
| :param fps: 视频帧率(默认30帧/秒) | |
| """ | |
| # 加载图片并创建静态视频片段 | |
| clip = ImageClip(image_path, duration=duration) | |
| # 设置帧率 | |
| clip.fps = fps | |
| # 保存视频 | |
| clip.write_videofile(output_path, codec='libx264') | |
| # 示例使用 | |
| image_path = 'input_image.jpg' # 输入图片路径 | |
| output_path = 'output_video.mp4' # 输出视频路径 | |
| duration = 10 # 视频时长为10秒 | |
| create_static_video_from_image(image_path, output_path, duration) |