
使用Python,支持常见的视频格式转换。
开箱即用的软件放在文末了,有能力者请自行改进。
如果需要使用该工具,务必安装FFmpeg框架支持,可以从 FFmpeg 官网 下载适用于操作系统的版本。
代码:
import tkinter as tk
from tkinter import filedialog, messagebox
from moviepy.editor import VideoFileClip
import threading
import os
def video_to_gif(input_video_path, output_gif_path, start_time=None, end_time=None):
"""
将视频转换为GIF。
参数:
- input_video_path: 输入视频文件路径
- output_gif_path: 输出GIF文件路径
- start_time: GIF的开始时间(以秒为单位),默认为视频开头
- end_time: GIF的结束时间(以秒为单位),默认为视频结尾
"""
try:
# 加载视频文件
video_clip = VideoFileClip(input_video_path)
# 裁剪视频到指定时间段
if start_time is not None and end_time is not None:
video_clip = video_clip.subclip(start_time, end_time)
# 更新状态为“处理中”
status_var.set("GIF转换中,请稍候...")
# 将视频转换为GIF并保存
if output_gif_path: # 确保路径有效
video_clip.write_gif(output_gif_path)
# 更新状态为“完成”
status_var.set(f"GIF已成功保存为 {output_gif_path}")
messagebox.showinfo("完成", f"GIF已成功保存为 {output_gif_path}")
else:
status_var.set("转换取消")
messagebox.showwarning("取消", "转换已取消,无效的保存路径。")
except Exception as e:
status_var.set("转换失败")
messagebox.showerror("错误", f"出现错误: {e}")
finally:
# 重新启用转换按钮
convert_button.config(state=tk.NORMAL)
def convert_to_gif():
if not video_path.get():
messagebox.showerror("错误", "请先选择一个视频文件。")
return
start = start_time.get()
end = end_time.get()
try:
start = float(start) if start else None
end = float(end) if end else None
except ValueError:
messagebox.showerror("错误", "请输入有效的起始和结束时间。")
return
output_path = filedialog.asksaveasfilename(defaultextension=".gif", filetypes=[("GIF files", "*.gif")])
# 如果用户取消保存文件操作
if not output_path:
status_var.set("转换取消")
return
# 禁用转换按钮
convert_button.config(state=tk.DISABLED)
def do_conversion():
video_to_gif(video_path.get(), output_path, start_time=start, end_time=end)
# 启动转换线程
conversion_thread = threading.Thread(target=do_conversion)
conversion_thread.start()
def select_file():
file_path = filedialog.askopenfilename(filetypes=[("Video files", "*.mp4;*.avi;*.mov;*.mkv")])
if file_path:
video_path.set(file_path)
video_label.config(text="文件已加载:" + os.path.basename(file_path))
# 创建主窗口
root = tk.Tk()
root.title("视频转GIF工具")
root.geometry("500x350")
# 视频路径显示
video_path = tk.StringVar()
video_label = tk.Label(root, text="点击下方按钮选择视频文件:", width=60, height=4, relief="sunken")
video_label.pack(pady=10)
# 文件选择按钮
select_button = tk.Button(root, text="选择文件", command=select_file)
select_button.pack(pady=10)
# 起始时间输入
tk.Label(root, text="起始时间 (秒):").pack()
start_time = tk.Entry(root)
start_time.pack(pady=5)
# 结束时间输入
tk.Label(root, text="结束时间 (秒):").pack()
end_time = tk.Entry(root)
end_time.pack(pady=5)
# 状态显示
status_var = tk.StringVar()
status_label = tk.Label(root, textvariable=status_var, width=60)
status_label.pack(pady=10)
# 转换按钮
convert_button = tk.Button(root, text="转换为GIF", command=convert_to_gif)
convert_button.pack(pady=20)
# 运行主循环
root.mainloop()
稍微优化了一下多线程。
import tkinter as tk
from tkinter import filedialog, messagebox
from moviepy.editor import VideoFileClip
import threading
import os
def video_to_gif(input_video_path, output_gif_path, start_time=None, end_time=None):
"""
将视频转换为GIF。
参数:
- input_video_path: 输入视频文件路径
- output_gif_path: 输出GIF文件路径
- start_time: GIF的开始时间(以秒为单位),默认为视频开头
- end_time: GIF的结束时间(以秒为单位),默认为视频结尾
"""
try:
# 加载视频文件
video_clip = VideoFileClip(input_video_path)
# 裁剪视频到指定时间段
if start_time is not None and end_time is not None:
video_clip = video_clip.subclip(start_time, end_time)
# 更新状态为“处理中”
status_var.set("GIF转换中,请稍候...")
# 将视频转换为GIF并保存
if output_gif_path: # 确保路径有效
video_clip.write_gif(output_gif_path)
# 更新状态为“完成”
status_var.set(f"GIF已成功保存为 {output_gif_path}")
messagebox.showinfo("完成", f"GIF已成功保存为 {output_gif_path}")
else:
status_var.set("转换取消")
messagebox.showwarning("取消", "转换已取消,无效的保存路径。")
except Exception as e:
status_var.set("转换失败")
messagebox.showerror("错误", f"出现错误: {e}")
finally:
# 重新启用转换按钮
convert_button.config(state=tk.NORMAL)
def convert_to_gif():
if not video_path.get():
messagebox.showerror("错误", "请先选择一个视频文件。")
return
start = start_time.get()
end = end_time.get()
try:
start = float(start) if start else None
end = float(end) if end else None
except ValueError:
messagebox.showerror("错误", "请输入有效的起始和结束时间。")
return
output_path = filedialog.asksaveasfilename(defaultextension=".gif", filetypes=[("GIF files", "*.gif")])
# 如果用户取消保存文件操作
if not output_path:
status_var.set("转换取消")
return
# 禁用转换按钮
convert_button.config(state=tk.DISABLED)
def do_conversion():
video_to_gif(video_path.get(), output_path, start_time=start, end_time=end)
# 启动转换线程
conversion_thread = threading.Thread(target=do_conversion)
conversion_thread.start()
def select_file():
file_path = filedialog.askopenfilename(filetypes=[("Video files", "*.mp4;*.avi;*.mov;*.mkv")])
if file_path:
video_path.set(file_path)
video_label.config(text="文件已加载:" + os.path.basename(file_path))
# 创建主窗口
root = tk.Tk()
root.title("视频转GIF工具")
root.geometry("500x350")
# 视频路径显示
video_path = tk.StringVar()
video_label = tk.Label(root, text="本程序仅支持MP4、AVI、MOV、MKV格式的视频文件。", width=60, height=4, relief="sunken")
video_label.pack(pady=10)
# 文件选择按钮
select_button = tk.Button(root, text="选择文件", command=select_file)
select_button.pack(pady=10)
# 起始时间输入
tk.Label(root, text="起始时间 (秒):").pack()
start_time = tk.Entry(root)
start_time.pack(pady=5)
# 结束时间输入
tk.Label(root, text="结束时间 (秒):").pack()
end_time = tk.Entry(root)
end_time.pack(pady=5)
# 状态显示
status_var = tk.StringVar()
status_label = tk.Label(root, textvariable=status_var, width=60)
status_label.pack(pady=10)
# 转换按钮
convert_button = tk.Button(root, text="转换为GIF", command=convert_to_gif)
convert_button.pack(pady=20)
# 运行主循环
root.mainloop()
多文件转换:
import tkinter as tk
from tkinter import filedialog, messagebox, ttk
from moviepy.editor import VideoFileClip
import threading
import os
def video_to_gif(input_video_path, output_gif_path, start_time=None, end_time=None, progress_var=None):
"""
将视频转换为GIF。
参数:
- input_video_path: 输入视频文件路径
- output_gif_path: 输出GIF文件路径
- start_time: GIF的开始时间(以秒为单位),默认为视频开头
- end_time: GIF的结束时间(以秒为单位),默认为视频结尾
- progress_var: 用于显示进度的 tk.IntVar 对象
"""
try:
# 加载视频文件
video_clip = VideoFileClip(input_video_path)
# 裁剪视频到指定时间段
if start_time is not None and end_time is not None:
video_clip = video_clip.subclip(start_time, end_time)
# 将视频转换为GIF并保存
video_clip.write_gif(output_gif_path)
# 更新进度为100%
if progress_var:
progress_var.set(100)
messagebox.showinfo("完成", f"GIF已成功保存为 {output_gif_path}")
except Exception as e:
messagebox.showerror("错误", f"转换过程中出现错误: {e}")
def convert_to_gif(files):
start = start_time.get()
end = end_time.get()
try:
start = float(start) if start else None
end = float(end) if end else None
except ValueError:
messagebox.showerror("错误", "请输入有效的起始和结束时间。")
return
# 为每个文件创建线程
for file_path in files:
output_path = os.path.splitext(file_path)[0] + ".gif"
# 创建一个进度条
progress_var = tk.IntVar()
progress_bar = ttk.Progressbar(root, orient="horizontal", length=300, mode="determinate", variable=progress_var)
progress_bar.pack(pady=5)
progress_bar_label = tk.Label(root, text=f"转换中: {os.path.basename(file_path)}")
progress_bar_label.pack()
# 启动转换线程
conversion_thread = threading.Thread(target=video_to_gif, args=(file_path, output_path, start, end, progress_var))
conversion_thread.start()
def select_files():
file_paths = filedialog.askopenfilenames(filetypes=[("Video files", "*.mp4;*.avi;*.mov;*.mkv")])
if file_paths:
# 将选中的文件路径显示在上方框内
file_list.set('\n'.join(file_paths))
convert_button.config(state=tk.NORMAL)
# 创建主窗口
root = tk.Tk()
root.title("批量视频转GIF工具")
root.geometry("600x400")
# 视频路径显示
file_list = tk.StringVar()
video_label = tk.Label(root, textvariable=file_list, width=60, height=4, relief="sunken", anchor="w", justify="left")
video_label.pack(pady=10)
# 文件选择按钮
select_button = tk.Button(root, text="选择文件", command=select_files)
select_button.pack(pady=10)
# 起始时间输入
tk.Label(root, text="起始时间 (秒):").pack()
start_time = tk.Entry(root)
start_time.pack(pady=5)
# 结束时间输入
tk.Label(root, text="结束时间 (秒):").pack()
end_time = tk.Entry(root)
end_time.pack(pady=5)
# 转换按钮
convert_button = tk.Button(root, text="批量转换为GIF", command=lambda: convert_to_gif(file_list.get().split('\n')))
convert_button.pack(pady=20)
convert_button.config(state=tk.DISABLED)
# 运行主循环
root.mainloop()
多文件转换没有打包软件,请自行打包。
Download permission
View
-
¥Download for freeDownload after commentDownload after login
- {{attr.name}}:
Your current level is
Login for free downloadLogin
Your account has been temporarily suspended and cannot be operated!
Download after commentComment
Download after paying ¥ points
please firstLogin
You have run out of downloads ( times) please come back tomorrow orUpgrade Membership
Download after paying pointsPay Now
Download after paying pointsPay Now
Your current user level is not allowed to downloadUpgrade Membership
You have obtained download permission
You can download resources every daytimes, remaining todaytimes left today
- All rights reserved.
- No part of this website, including text and images, may be reproduced, modified, distributed, or transmitted in any form or by any means, without the prior written permission of the author.
- Unauthorized commercial use is strictly prohibited.
- Unauthorized personal use is strictly prohibited.