moviepy:将MP4视频数据每隔10秒裁剪成一个新的视频,并保存在同一个文件夹下

将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)
相关推荐
承渊政道19 分钟前
【Python编程快速上手—让繁琐工作自动化】(列表:把数据排成队,从下标取值到深拷贝)
开发语言·python·pycharm·自动化·列表
大胖爱学习_MJ27 分钟前
Agent 如何安全地拿数据库密码?基于 HashiCorp Vault 的动态凭证下发
经验分享·python
tryCbest1 小时前
Python 爬虫从入门到实战(一):基础篇——从零写出第一个爬虫
开发语言·爬虫·python
无小道1 小时前
Python语句的特色
python·语句
2601_967980112 小时前
python pytest如何编写测试
python·测试用例·pytest·函数·assert
玖米GEO运营王总监2 小时前
成都AI数字人视频怎么批量生产?流程与合规标注
大数据·人工智能·音视频
2601_967980113 小时前
Python中heapq模块的各种用法实例
数据结构·python·算法·应用实例·heapq模块
大胖爱学习_MJ3 小时前
超越向量检索:用 Spring Boot + Neo4j 手搓 GraphRAG 知识图谱记忆
java·后端·python
RobinDevNotes3 小时前
24GB Mac 做数字人:声音放本地,画面租云端
人工智能·python·macos·开发工具
学无止境小袁同学5 小时前
LlamaIndex Workflows 学习
python·llm·tensorflow·llamaindex·agent开发·多agent·workflows