convert.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:popcorn-exporter 作者: antiboredom 项目源码 文件源码
def convert(data, outfile='composition.mp4'):
    '''Converts popcorn track list into an mp4'''

    clips = []

    # generate background color clip
    # convert hex color from popcorn to an RGB tuple
    bg_color = data.get('backgroundColor', '#000000').lstrip('#')
    bg_color = tuple(int(bg_color[i:i+2], 16) for i in (0, 2, 4))
    total_duration = float(data['duration'])
    background_clip = mpy.ColorClip(size=(WIDTH, HEIGHT), col=bg_color).set_duration(total_duration)
    clips.append(background_clip)

    # generate moviepy clips from popcorn options
    tracks = sorted(data['tracks'], key=lambda k: k['order'], reverse=True)
    for track in tracks:
        for event in track['trackEvents']:
            clip = create_clip(event['type'], event['popcornOptions'])
            clips.append(clip)

    # ignore track events that create_clip can't handle
    clips = [c for c in clips if c is not None]

    # composite the video
    video = mpy.CompositeVideoClip(clips, size=(1280, 720))

    tmpoutfile = outfile + '.temp.mp4'

    video.write_videofile(
        tmpoutfile,
        fps=24,
        codec='libx264',
        audio_codec='aac',
        temp_audiofile='temp-audio.m4a',
        remove_temp=True
    )

    shutil.move(tmpoutfile, outfile)

    return outfile
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号