Get that word out of your mouth.

By R.

Generated programmatically using moviepy. Sample code below.

#!/usr/bin/env python

from collections import Counter
import random
import time

from moviepy.editor import VideoFileClip, concatenate, vfx


if __name__ == '__main__':

    srtfile = 'sotu.en.vtt'

    # converts subtitle file timestamps to seconds.
    def str2secs(s):
        hh = int(s[0:2])
        mm = int(s[3:5])
        ss = float(s[6:])
        return hh*3600.0+mm*60.0+ss

    # looks for lines containing intervals, parses timestamps if a match.
    st = 'scan'
    tpl = 'DD:DD:DD.DDD --> DD:DD:DD.DDD'
    def tplmatch(s):
        if len(tpl) != len(s):
            return False, None
        for i,c in enumerate(tpl):
            si = s[i]
            if c == 'D':
                if not si.isdigit():
                    return False, None
            else:
                if si != c:
                    return False, None
        s = tuple(str2secs(ss) for ss in s.split(' --> '))
        return True, s


    # parse subtitles file.
    with open(srtfile) as srtin:
        trange = None
        elements = []
        for line in srtin:
            line = line.strip()
            if st == 'scan':
                match, trange = tplmatch(line)
                if match:
                    st = 'capture'
            elif st == 'capture':
                elements.append((trange, line))
                st = 'scan'

        # Uncomment to output a list of 25 most common words in
        # subtitles.

        # print(elements)
        # txt = ' '.join(ln for tr, ln in elements)
        # counts = Counter(w for w in txt.split(' ') if len(w) > 5)
        # print(counts.most_common(25))

        print('finding cuts...')
        lookfor = 'America'
        cuts = []
        for lcut, l in elements:
            cst, cen = lcut
            cdur = cen - cst
            st = l.find(lookfor)
            while st >= 0:
                en = st+len(lookfor)
                cuts.append(
                    (cst+(float(st)/len(l)*cdur), cst+(float(en)/len(l)*cdur))
                )
                st = l[en:].find(lookfor)
                if st >= 0:
                    st += en
        print('...got %d segment(s).' % len(cuts))
        print('loading src video...')
        vid = VideoFileClip('sotu.mp4')
        print('making subclips...')
        subclips = [vid.subclip(st, en) for st, en in cuts]
        # one in six clips are time-skewed (slowed down).
        # of those, one in three is also color-inverted.
        weighted = tuple(v for v, x in ((True, 1), (False, 5)) for i in range(x))
        for ci, clip in enumerate(subclips):
            if random.choice(weighted):
                skew = random.random()
                print(' - time skewing...%0.2f' % skew)
                weighted2 = tuple(v for v, x in ((True, 1), (False, 2)) 
                    for i in range(x))
                if random.choice(weighted2):
                    subclips[ci] = clip.fx(vfx.invert_colors).fx(vfx.speedx, skew)
                else:
                    subclips[ci] = clip.fx(vfx.speedx, skew)
        print('concatenating...')
        newvid = concatenate(subclips)
        print('saving...')
        # write new video to timestamped mp4.
        newvid.to_videofile('america-%s.mp4' % time.time())
        print('done.')