Sorry,有钱真的可以为所欲为 ~

  • sorry,有钱真的可以为所欲为 ~
    • 源起
    • python版关键点
      • 1. jinja2模板渲染自定义ass
      • 2. ffmpeg根据MP4和ass生成gif
    • 完整源码

sorry,有钱真的可以为所欲为 ~

源起

sorry是一款很有意思的应用,源自于sorry,有钱真的可以为所欲为这个梗。
亮点是可以换自己的梗生成gif。
可惜部署环境是ubuntu+ruby,我就重制了个全平台的python重置版sorrypy(
Github & 码云)博大家一笑。
荣誉首先属于xtyxtyx

Hardy兄弟的NodeJs版:node-sorry也非常好用,有DEMO为证,嘎嘎。

sorry 原版网站 生成的样例:

python版关键点

1. jinja2模板渲染自定义ass

def render_ass(template_name, sentences, filename):
    output_file_path = "static/cache/%s.ass" % filename
    template = ass_text(template_name)
    rendered_ass_text = Template(template).render(sentences=sentences)
    with open(output_file_path, "w", encoding="utf8") as fp:
        fp.write(rendered_ass_text)
    return output_file_path

2. ffmpeg根据MP4和ass生成gif

def make_gif_with_ffmpeg(template_name, sentences, filename):
    ass_path = render_ass(template_name, sentences, filename)
    gif_path = "static/cache/" + filename
    video_path = "static/" + template_name + "/template.mp4"
    print(ass_path, gif_path, video_path)
    cmd = "ffmpeg -i {video_path} -r 8 -vf ass={ass_path},scale=300:-1 -y {gif_path}" \
        .format(video_path=video_path, ass_path=ass_path, gif_path=gif_path)
    print(cmd)
    p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
    p.wait()
    if p.returncode != 0:
        print("Error.")
    return -1

完整源码

Ruby原版:sorry Github
Python版:sorrypy Github & 码云

欢迎 star & fork ,哈哈~~


版权声明:转载必须注明本文转自 East196 的博客:http://blog.csdn.net/east196

你可能感兴趣的:(Python,可视化)