基于OpenCV+Pillow的mp4视频转gif图片可视化工具代码

代码语言:python

所属分类:其他

代码描述:基于OpenCV+Pillow的mp4视频转gif图片可视化工具代码

代码标签: 基于 opencv Pillow mp4 视频 gif 图片 可视化 工具 代码

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

#!/usr/bin/env python3
"""
MP4 → GIF 转换器
=================
基于 OpenCV + Pillow,无需独立安装 FFmpeg 命令行工具
支持单文件与批量文件夹转换
"""

import os
import sys
import queue
import threading
from pathlib import Path

import tkinter as tk
from tkinter import ttk, filedialog, messagebox

try:
    import cv2
except ImportError:
    sys.exit("缺少依赖,请运行: pip install opencv-python")

try:
    from PIL import Image
except ImportError:
    sys.exit("缺少依赖,请运行: pip install pillow")

# ══════════════════════════════════════════════════════════════
#  常量
# ══════════════════════════════════════════════════════════════

VIDEO_EXTS = {
    '.mp4', '.avi', '.mkv', '.mov', '.wmv',
    '.flv', '.webm', '.m4v', '.ts', '.mpg', '.mpeg', '.3gp',
}

# GitHub-dark inspired palette
C = dict(
    bg       = '#0d1117',
    surface  = '#161b22',
    surface2 = '#21262d',
    border   = '#30363d',
    accent   = '#58a6ff',
    accent_h = '#79b8ff',
    green    = '#3fb950',
    red      = '#f85149',
    yellow   = '#d29922',
    text     = '#c9d1d9',
    dim      = '#8b949e',
    bright   = '#f0f6fc',
)

FONT      = 'Segoe UI'
FONT_MONO = 'Consolas'

PRESETS = {
    '小文件 (320px / 8fps / 64色)': dict(width='320', fps='8',  colors='64'),
    '平  衡 (480px / 10fps / 128色)': dict(width='480', fps='10', colors='128'),
    '高质量 (640px / 15fps / 256色)': dict(width='640', fps='15', colors='256'),
    '自定义': {},
}


# ══════════════════════════════════════════════════════════════
#  Application
# ══════════════════════════════════════════════════════════════

class App:
    """MP4 → GIF 转换器主应用"""

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('MP4 → GIF 转换器')
        self.root.geometry('1080x760')
        self.root.minsize(980, 700)
        self.root.configure(bg=C['bg'])

        # ── 状态 ──
        self.files: list[str] = []
        self.converting = False
        self.cancel_flag = False
        self._q: queue.Queue = queue.Queue()

        # ── TK 变量 ──
        self.v_preset   = tk.StringVar(value=list(PRESETS.keys())[1])
        self.v_start    = tk.StringVar(value='0')
        self.v_duration = tk.StringVar(value='0')
        self.v_fps      = tk.StringVar(value='10')
        self.v_width    = tk.StringVar(value='480')
        self.v_colors   = tk.StringVar(value='128')
        self.v_loop     = tk.StringVar(value='0')
        self.v_outdir   = tk.StringVar(value='.........完整代码请登录后点击上方下载按钮下载查看

网友评论0