예제 #1
0
파일: compose.py 프로젝트: marchon/autotv
def compose_clip(filenames, outname, pat):
    timestamps = []

    for f in filenames:
        try:
            with open(f.replace('.mp4', '.en.vtt'), 'r') as infile:
                data = infile.read()
            sentences = vtt.parse_auto_sub(data)
        except:
            continue

        if 'words' not in sentences[0]:
            continue

        if '<' in pat:
            text = ' '.join([s['text'] for s in sentences])
            doc = nlp(text)

            results = pos_regex_matches(doc, pat)
            results = [r.string.lower().strip() for r in results]
            results = [r for r in results if "'" not in r]
            results = list(set(results))
        else:
            results = pat.split('|')

        allwords = []
        for s in sentences:
            allwords += s['words']

        justwords = [w['word'].lower().strip() for w in allwords]

        for r in results:
            # print(r)
            indices = find_sub_list(r.split(' '), justwords)
            for i in indices:
                start = allwords[i[0]]['start']
                end = allwords[i[1]]['end'] + 0.2
                timestamps.append((f, r, start, end))

    timestamps = sorted(timestamps, key=lambda x: x[1])
    clips = []
    '''create with vidpy'''
    for f, r, start, end in timestamps:
        print(r)
        clip = Clip(f, start=start, end=end)
        clip.text(r,
                  font='Courgette',
                  size=60,
                  valign='bottom',
                  color='#FFFF00',
                  bbox=('1%', '1%', '98%', '98%'))
        clips.append(clip)

    tmpclips = []
    for i, chunk in enumerate(chunker(clips, 60)):
        tmpname = outname + '.tmp.{}.mp4'.format(str(i).zfill(5))
        comp = Composition(chunk, singletrack=True)
        comp.save(tmpname)
        tmpclips.append(tmpname)

    comp = Composition([Clip(t) for t in tmpclips], singletrack=True)
    comp.save(outname)

    for tmpname in tmpclips:
        os.remove(tmpname)
    '''create with ffmpeg'''
    # clipnum = 0
    # for f, r, start, end in timestamps:
    #     tempout = 'shots/' + str(clipnum).zfill(6) + '.mp4'
    #     fontfile = os.path.abspath("Courgette-Regular.ttf")
    #     args = '''ffmpeg -hide_banner -loglevel panic -y -i {} -ss {} -t {} -strict -2 -vf drawtext="fontfile={}:text='{}': fontcolor=yellow: fontsize=30: x=(w-text_w)/2: y=(h-text_h-20)" {}'''.format(f, start, end-start, fontfile, r, tempout)
    #     args = '''ffmpeg -hide_banner -loglevel panic -y -i {} -ss {} -t {} -strict -2 {}'''.format(f, start, end-start, tempout)
    #     with Popen(args, shell=True):
    #         pass
    #     clipnum += 1
    '''create clips with vidpy'''
    # clipnum = 0
    # for f, r, start, end in timestamps:
    #     print(r)
    #     tempout = 'shots/' + str(clipnum).zfill(6) + '.mp4'
    #     clip = Clip(f, start=start, end=end)
    #     clip.text(r, font='Courgette', size=60, valign='bottom', color='#FFFF00', bbox=('1%', '1%', '98%', '98%'))
    #     Composition([clip]).save(tempout, silent=True)
    #     clipnum += 1

    return outname
예제 #2
0
    'softglow': [],
}

clips = []
for f in filters:
    # create a clip one second long
    clip = Clip(vid, start=0, end=1)

    # mute it
    clip.volume(0)

    # retrieve the filters
    args = filters[f]

    if len(args) > 0:
        getattr(clip, f)(*args)
    else:
        getattr(clip, f)()

    # add the text of the filter
    clip.text(f, olcolor='#000000', outline=10)

    # append clip to the clips list
    clips.append(clip)

# make composition
comp = Composition(clips, singletrack=True)

# save file to hard drive
comp.save('filters.mp4')