将MP4视频数据每隔10秒裁剪成一个新的视频,并保存在同一个文件夹下。
输入数据,
裁剪结果:
python
import os
from moviepy.video.io.VideoFileClip import VideoFileClip
def split_video_into_segments(video_path, segment_duration=10):
# 获取视频的基本信息
video = VideoFileClip(video_path)
video_duration = video.duration
video_basename = os.path.basename(video_path)
video_name, video_ext = os.path.splitext(video_basename)
# 创建保存分割视频的文件夹
output_folder = f"{video_name}_segments"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 分割视频并保存
segment_count = int(video_duration // segment_duration) + 1
for i in range(segment_count):
start_time = i * segment_duration
end_time = min((i + 1) * segment_duration, video_duration)
segment = video.subclip(start_time, end_time)
output_path = os.path.join(output_folder, f"{video_name}_segment_{i+1}{video_ext}")
segment.write_videofile(output_path, codec="libx264")
print(f"Video split into {segment_count} segments and saved in '{output_folder}' folder.")
# 示例使用 第一步,将原始视频剪切为10秒的视频片段
video_path = "D:/AIAlgorithm/IntelligentDetectionVehicle/videos_0720/1/5.mp4" # 替换为你的MP4视频文件路径
split_video_into_segments(video_path)