MoviePy
Examples of using the python library MoviePy
Summarizing a video
from moviepy.editor import *
clip = VideoFileClip("CC501_hypercard.ogv")# .subclip(10, 15)
x = clip.subclip(10, 15)
# x.write_videofile("x.webm")
p = []
# ADD STUFF TO OUR PLAYLIST
for i in range(0, int(clip.duration), 60):
print "i am editing from", i, "to", i+1
x = clip.subclip(i, i+1)
p.append(x)
# SAVE THE MOVIE
n = concatenate_videoclips(p)
n.write_videofile("loop.webm")
Cutting with subtitles (videogrep)
Here's a file that should be called "srt.py":
import re
def srtparse (src):
""" parse srt src (string), returns list of dicts with start, end, timecode, content """
spat = re.compile(r"^(?:\d+\n)?((?:\d\d:)?\d\d:\d\d(?:,\d\d\d)? *--> *(?:(?:\d\d:)?\d\d:\d\d(?:,\d\d\d)?)?)$", re.M)
tcpat = re.compile(r"(?:(\d\d):)?(\d\d):(\d\d)(?:,(\d\d\d))? *--> *(?:(?:(\d\d):)?(\d\d):(\d\d)(?:,(\d\d\d))?)?")
# spat = re.compile(r"^(?:\d+\n)?(\d\d:\d\d:\d\d(?:[,\.]\d\d\d)? --> \d\d:\d\d:\d\d(?:[,\.]\d\d\d)?)$", re.M)
# tcpat = re.compile(r"(\d\d):(\d\d):(\d\d)(?:[,\.](\d\d\d))? --> (\d\d):(\d\d):(\d\d)(?:[,\.](\d\d\d))?")
tt = spat.split(src)
ret = []
for i in range(1, len(tt), 2):
timecode = tt[i]
content = tt[i+1]
tcs = tcpat.match(timecode).groups()
if tcs[3] == None:
start = (int(tcs[0])*3600) + (int(tcs[1])*60) + float(tcs[2])
else:
start = (int(tcs[0])*3600) + (int(tcs[1])*60) + float(tcs[2]+"."+tcs[3])
if tcs[4]:
if tcs[7] == None:
end = (int(tcs[4])*3600) + (int(tcs[5])*60) + float(tcs[6])
else:
end = (int(tcs[4])*3600) + (int(tcs[5])*60) + float(tcs[6]+"."+tcs[7])
else:
end = None
ret.append({
'start': start,
'end': end,
'timecode': timecode,
'content': content.strip()
})
return ret
A script to load subtitles, and edit together all instances of an "uh"...
from srt import srtparse
from moviepy.editor import *
m = "Purely Python Imaging with Pymaging-OpnmcItfiHc"
with open(m+".en.srt") as f:
tt = srtparse(f.read())
clip = VideoFileClip(m+".mp4")
cc = []
for t in tt:
if 'uh' in t['content']:
cc.append(clip.subclip(t['start'], t['end']))
out = concatenate_videoclips(cc)
out.write_videofile("uh.webm")
Auto titling a clip from archive.org
from moviepy.editor import *
from urllib2 import urlopen
import json
clip = VideoFileClip("Purely Python Imaging with Pymaging-OpnmcItfiHc.mp4")
print clip.duration
# d = "Hello World"
data = json.load(urlopen("https://archive.org/details/CC501_hypercard?output=json"))
# print data
d = data['metadata']['description'][0]
# print "the description from archive . org is", d
t = TextClip(d, fontsize=36, color="white", method="caption", size=(640, 480)).set_duration(10)
# t.fps = 24
# t.write_videofile("autotitle.webm")
o = concatenate_videoclips([t, clip.subclip(0, 10)], method="compose")
# o.fps = 24
o.write_videofile("autotitle.webm")