def compose_with_vidpy(
    maxduration=60,
    thresh=0.2,
    fade=0.3,
    duration=4,
    sections=3,
    padding=0.5,
    outname="home.mp4",
):
    shots = {}
    allshots = []

    for f in glob("videos/*.shots.json"):
        # if re.search(r'^videos/\d+', f) is None:
        #     continue

        with open(f, "r") as infile:
            data = json.load(infile)

        f = f.replace(".shots.json", "")

        _shots = [(f, d["time"]) for d in data if d["score"] > thresh]
        _shots = [d["time"] for d in data if d["score"] > thresh]
        shots[f] = []
        for i, d in enumerate(_shots):
            if i > 0:
                start = _shots[i - 1]
            else:
                start = 0
            end = d
            shots[f].append((start, end))

        # if len(_shots) > 5:
        #     shots[f] = _shots
        #     allshots += _shots

    offset = 0
    clips = []
    while offset < maxduration:
        filename = random.choice(list(shots.keys()))
        if len(shots[filename]) < 5:
            continue
        start, end = random.choice(shots[filename])
        start += padding
        end -= padding
        dur = min(end - start, duration - padding)

        clip = Clip(filename, start=start, end=start + dur, offset=offset)
        clip.zoompan([0, 0, "100%", "100%"], ["-25%", "-25%", "150%", "150%"],
                     0, 100)
        clip.fadein(fade)
        offset += dur - fade
        clips.append(clip)

    # if stitch:
    comp = Composition(clips)
    comp.save(outname)
def extract_shots(thresh=0.2, duration=4, padding=0.5):
    shots = {}
    allshots = []

    for f in glob('videos/*.shots.json'):

        with open(f, 'r') as infile:
            data = json.load(infile)

        f = f.replace('.shots.json', '')

        _shots = [d['time'] for d in data if d['score'] > thresh]
        for i, d in enumerate(_shots):

            if i > 0:
                start = _shots[i - 1]
            else:
                start = 0
            end = d

            start += padding
            end -= padding
            end = min(end, start + duration - padding)

            if end - start < 1:
                continue

            print(start, end)

            outname = 'shots/{}_{}_{}.mp4'.format(f.replace('videos/', ''),
                                                  start, end)
            if os.path.exists(outname):
                continue

            clip = Clip(f, start=start, end=end)
            clip.zoompan([0, 0, '100%', '100%'],
                         ['-25%', '-25%', '150%', '150%'], 0, 100)
            clip.save(outname)