代码:

import os
from moviepy.editor import VideoFileClip

def split_video(video_path, output_dir):
    # 加载视频文件
    clip = VideoFileClip(video_path)
    duration = clip.duration

    # 计算中间时间点
    midpoint = duration / 2

    # 创建输出目录
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # 切分视频
    part1 = clip.subclip(0, midpoint)
    part1_filename = os.path.join(output_dir, os.path.basename(video_path).split('.')[0] + '_part1.mp4')
    part1.write_videofile(part1_filename, codec='libx264', audio_codec='aac')

    part2 = clip.subclip(midpoint, duration)
    part2_filename = os.path.join(output_dir, os.path.basename(video_path).split('.')[0] + '_part2.mp4')
    part2.write_videofile(part2_filename, codec='libx264', audio_codec='aac')

    print(f"视频 {video_path} 已切分为两部分: {part1_filename} 和 {part2_filename}")

def split_videos_in_directory(directory, output_dir):
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):
                video_path = os.path.join(root, file)
                split_video(video_path, output_dir)

# 指定要处理的目录
source_directory = r'C:\剪映导出\yuan'
# 指定输出目录
output_directory = r'C:\剪映导出\qie'

split_videos_in_directory(source_directory, output_directory)

 

posted on 2024-10-04 09:41  大话人生  阅读(1)  评论(0编辑  收藏  举报