【问题标题】:How to split a video in parts using Python?如何使用 Python 分割视频?
【发布时间】:2021-01-04 22:55:14
【问题描述】:

我需要将任意大小的视频文件拆分为最大大小不超过 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)

【问题讨论】:

  • this 是一个好的开始

标签: python video compression txt


【解决方案1】:

这是我刚刚使用 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("-----------------###-----------------")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2016-09-08
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多