Skip to content

Instantly share code, notes, and snippets.

@agalera
Created February 6, 2017 09:32
Show Gist options
  • Save agalera/8cd63429b06e21d1420c6030c6b45c53 to your computer and use it in GitHub Desktop.
Save agalera/8cd63429b06e21d1420c6030c6b45c53 to your computer and use it in GitHub Desktop.
split video with moviepy
from moviepy.editor import *
from multiprocessing import Process, Semaphore
import sys
segment_length = float(sys.argv[1])
frames = float(sys.argv[2])
original_video = VideoFileClip("original.mp4")
duration = original_video.duration
clip_start = 0
num = 0
pool_sema = Semaphore(6)
def write_videofile(clip_start, clip_end):
try:
clip = VideoFileClip("original.mp4").subclip(clip_start, clip_end).resize((1280, 720))
print(clip_start, clip_end)
clip.write_videofile("output_%s.mp4" % num, fps=frames, bitrate="4000k",
threads=1, preset='ultrafast', codec='h264')
except:
print("error", clip_start, clip_end)
finally:
pool_sema.release()
while clip_start < duration:
clip_end = clip_start + segment_length
if clip_end > duration:
clip_end = duration
pool_sema.acquire()
p = Process(target=write_videofile, args=(clip_start, clip_end)).start()
clip_start = clip_end
num += 1
@WhiskersReneeWe
Copy link

Really helpful! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment