Python을 사용하여 비디오를 부분적으로 분할하는 방법은 무엇입니까?

Jan 05 2021

모든 크기의 비디오 파일을 최대 75MB의 다양한 부분으로 분할해야합니다. 이 코드를 찾았지만 작동하지 않습니다.

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_count5로 설정 하고 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("-----------------###-----------------")