MoviePy

From XPUB & Lens-Based wiki
Revision as of 16:12, 3 February 2015 by Michael Murtaugh (talk | contribs)

Examples of using the python library MoviePy

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")