基于 Python 编写的视频转 GIF 动画工具(附代码)

本文章主要涉及以下工作:
   (1)讲述了如何使用Python实现视频转GIF动画。
   (2)提供了项目的 Python 代码以及相应的使用文档。
如果文章有用,欢迎各位→点赞👏 + 收藏💞 + 留言🔔 😁🌹🌹
项目代码:Convenient-Tools-With-Python
如果项目代码有用,请给Github项目star一下;谢谢啦 😁🌹🌹

1. 环境配置及运行方式

  • 打开终端,输入如下命令,安装本文件所需的第三方库。

    pip install moviepy 
    
  • 继续输入以下命令,即可将对应的视频文件转换成GIF动画,GIF文件保存在与视频文件同一个目录下。

    python Video2Gif.py --path 1.mp4 --resize (200,500) --fps 10 --clip (3,10)  
                                                0.5                                    
    
  • 命令中的参数说明:

    • path : 视频文件的路径,可以是绝对路径,也可以是相对路径。
    • resize : 修改尺寸,接收元组或者0-1之间的值,比如 (300, 200) 的意思是尺寸转为宽300高200,0.5 的意思是尺寸缩放到一半。
    • fps : 抽帧,接收整数,比如 20 的意思就是将mp4生成每秒 20 帧的GIF。
    • clip : 视频裁剪,接收元组,比如 (3, 10) 的意思是裁剪 3-10 秒之间的视频生成GIF。

2. 实现代码

视频转GIF动画工具的Python代码如下所示。

import argparse

from moviepy.editor import *


def opt_argparse():
    parser = argparse.ArgumentParser()
    parser.add_argument('--path', type=str, default='', help='The path to the video file, either absolute or relative')
    parser.add_argument('--resize', type=str, default=None,
                        help='Modify the size and accept a tuple or a value between 0 and 1. For example, (300, 200) means that the size is converted to 300 wide by 200 high, and 0.5 means that the size is scaled to half')
    parser.add_argument('--fps', type=int, default=None,
                        help='Pumping frames and accept integers. For example 20 means generating the mp4 as a Gif at 20 frames per second')
    parser.add_argument('--clip', type=str, default=None,
                        help='Video crop and accept tuple. For example (3, 10) means crop video between 3-10 seconds to generate Gif')
    opt = parser.parse_args()
    return opt


class Mp42GifOp(object):
    def __init__(self, resize=None, fps=None, clip=None):
        """
        :param resize: 修改尺寸,接收元组或者0-1之间的值,比如 (300, 200) 的意思是尺寸转为宽300高200,0.5 的意思是尺寸缩放到一半
        :param fps: 抽帧,接收整数,比如 20 的意思就是将mp4生成每秒 20 帧的gif
        :param clip: 视频裁剪,接收元组,比如 (3, 10) 的意思是裁剪 3-10 秒之间的视频生成gif
        """
        self.resize = resize
        self.fps = fps
        self.clip = clip

    def mp42gif(self, mp4path, gifpath):
        vfc = VideoFileClip(mp4path)
        if self.clip:
            vfc = vfc.subclip(*eval(self.clip))
        if self.resize:
            vfc = vfc.resize(eval(self.resize))
        clip = (vfc)
        if self.fps:
            clip.write_gif(gifpath, fps=self.fps)
        else:
            clip.write_gif(gifpath)
        print("{} to {} complete!".format(mp4path, gifpath))


if __name__ == '__main__':
    opt = opt_argparse()
    # 初始化
    m2g = Mp42GifOp(opt.resize, opt.fps, opt.clip)
    # 生成gif路径
    spl = opt.path.replace('\\', '/').rsplit('/', 1)
    if len(spl) == 1:
        gifname = spl[0].rsplit('.', 1)[0]
        path = ''
    else:
        gifname = spl[1].rsplit('.', 1)[0]
        path = spl[0]
    gif_path = os.path.join(path, '{}.{}'.format(gifname, 'gif'))
    print(opt.path, gif_path)
    # 转换
    m2g.mp42gif(opt.path, gif_path)
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值