-1

I need to split a video file of any size into various parts of maximum size of up to 75 MB. I found this code, but it doesn't work:

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)
Twenkid
  • 825
  • 7
  • 15

2 Answers2

0

Here is a script I just created using MoviePy. But instead of dividing by byte-size it divides by video length. So if you set divide_into_count to 5 and you have a video of length 22 minutes, then you get videos of length 5, 5, 5, 5, 2 minutes.

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("-----------------###-----------------")
Aven Desta
  • 2,114
  • 12
  • 27
0

Avne Desta's solution ( which is great and helped me really ), with some slight changes I consider this a bit more readable code in my humble opinion ( really no offense ! ) since it goes not reverse and some minor naming things. It uses a bit more convenient file names (also imho), part_1 part_2 etc. It splits into chunks of fixed time.

All credits shall go the original creator, not me !

full_video = r"20secs.mp4"
partDura = 8 # duration of a part in seconds

from moviepy.editor import VideoFileClip
from time import sleep

fullDura = VideoFileClip(full_video).duration
startPos = 0

i = 1 
while True:
    endPos = startPos + partDura

    if endPos > fullDura:
        endPos = fullDura

    clip = VideoFileClip(full_video).subclip(startPos, endPos)

    part_name = "part_"+str(i)+".mp4"
    clip.to_videofile(part_name, codec="libx264", temp_audiofile='temp-audio.m4a', remove_temp=True, audio_codec='aac')
    print("part ",i,"done")
    i += 1
    
    startPos = endPos # jump to next clip
    
    if startPos >= fullDura:
        break