繁体   English   中英

如何使用 Python 分割视频?

[英]How to split a video in parts using Python?

我需要将任意大小的视频文件拆分为最大大小不超过 75 MB 的各个部分。 我找到了这段代码,但它不起作用:

import cv

capture = cv.CaptureFromFile(filename)
while Condition1:
    # Need a frame to get the output video dimensions
    frame = cv.RetrieveFrame(capture) # Will return None if there are no frames
    # New video file
    video_out = cv.CreateVideoWriter(output_filenameX, CV_FOURCC('M','J','P','G'), capture.fps, frame.size(), 1)
    # Write the frames
    cv.WriteFrame(video_out, frame)
    while Condition2:
        frame = cv.RetrieveFrame(capture) # Will return None if there are no frames
        cv.WriteFrame(video_out, frame)

这是我刚刚使用 MoviePy 创建的脚本。 但不是除以字节大小,而是除以视频长度。 因此,如果您将divide_into_count设置为 5,并且您有一个长度为 22 分钟的视频,那么您将获得长度为 5、5、5、5、2 分钟的视频。

from moviepy.editor import VideoFileClip
from time import sleep

full_video = "full.mp4"
current_duration = VideoFileClip(full_video).duration
divide_into_count = 5
single_duration = current_duration/divide_into_count
current_video = f"{current_duration}.mp4"

while current_duration > single_duration:
    clip = VideoFileClip(full_video).subclip(current_duration-single_duration, current_duration)
    current_duration -= single_duration
    current_video = f"{current_duration}.mp4"
    clip.to_videofile(current_video, codec="libx264", temp_audiofile='temp-audio.m4a', remove_temp=True, audio_codec='aac')

    print("-----------------###-----------------")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM