python批量mp4轉gif

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# File mp4_to_gif.py
# Date 2019-07-03 10:43
# Author Medue

import hashlib
from cv2 import VideoCapture
from moviepy.editor import *

dir_paths = '/Users/akio/Downloads/'

files = os.listdir(dir_paths)

for file in files:
    file_ext = str(os.path.splitext(file)[-1]).lower()
    if file_ext != '.mp4':
        continue
    file_name = dir_paths+file
    clip = VideoFileClip(file_name)
    v_len = clip.duration
    if v_len > 10:
        v_len = 6

    if v_len < 3:
        zoom = 0
    elif 3 <= v_len <= 5:
        zoom = 1
    elif 5 < v_len < 7:
        zoom = 2.7
    else:
        zoom = 3
    # zoom = 2
    cap = VideoCapture(file_name)
    # 獲取視頻信息
    if zoom > 0:
        content = clip.subclip(0, v_len).resize((int(cap.get(3)/zoom), int(cap.get(4)/zoom)))  # 修改分辨率
    else:
        content = clip.subclip(0, v_len)  # 不修改分辨率
    # 導出GIF
    md5 = hashlib.md5()
    md5.update(file_name.encode(encoding='utf-8'))
    gif_name = md5.hexdigest()+'.gif'
    content.write_gif(dir_paths+gif_name)
    del(clip, cap, md5)
pass

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章