วิธีแบ่งวิดีโอเป็นส่วน ๆ โดยใช้ Python

Jan 05 2021

ฉันต้องการแยกไฟล์วิดีโอทุกขนาดออกเป็นส่วนต่างๆที่มีขนาดสูงสุดไม่เกิน 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)

คำตอบ

AvenDesta Jan 05 2021 at 07:30

นี่คือสคริปต์ที่ฉันเพิ่งสร้างโดยใช้ 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("-----------------###-----------------")