视频播放器程序
以下是一个使用 Python 的 Tkinter 库和 VLC 媒体播放器库实现的视频播放器程序。该程序具有基本的播放、暂停、停止、快进和快退功能,以及音量控制和全屏播放。
依赖库
- Tkinter:Python 的内置 GUI 库
- vlc.py:VLC 媒体播放器库
- os:操作系统库
代码
import tkinter as tk
from tkinter import filedialog
import vlc
import os
class VideoPlayer:
def __init__(self, root):
self.root = root
self.root.title("视频播放器")
self.root.geometry("800x600")
# 创建 VLC 实例
self.vlc_instance = vlc.Instance()
self.player = self.vlc_instance.media_player_new()
# 创建 UI 元素
self.video_frame = tk.Frame(self.root)
self.video_frame.pack(fill="both", expand=True)
self.control_frame = tk.Frame(self.root)
self.control_frame.pack(fill="x")
self.play_button = tk.Button(self.control_frame, text="播放", command=self.play_video)
self.play_button.pack(side="left")
self.pause_button = tk.Button(self.control_frame, text="暂停", command=self.pause_video)
self.pause_button.pack(side="left")
self.stop_button = tk.Button(self.control_frame, text="停止", command=self.stop_video)
self.stop_button.pack(side="left")
self.fast_forward_button = tk.Button(self.control_frame, text="快进", command=self.fast_forward)
self.fast_forward_button.pack(side="left")
self.fast_backward_button = tk.Button(self.control_frame, text="快退", command=self.fast_backward)
self.fast_backward_button.pack(side="left")
self.volume_scale = tk.Scale(self.control_frame, from_=0, to=100, orient="horizontal", command=self.set_volume)
self.volume_scale.set(50)
self.volume_scale.pack(side="right")
self.fullscreen_button = tk.Button(self.control_frame, text="全屏", command=self.fullscreen)
self.fullscreen_button.pack(side="right")
self.open_button = tk.Button(self.control_frame, text="打开文件", command=self.open_file)
self.open_button.pack(side="right")
def play_video(self):
self.player.play()
def pause_video(self):
self.player.pause()
def stop_video(self):
self.player.stop()
def fast_forward(self):
self.player.set_time(self.player.get_time() + 10000)
def fast_backward(self):
self.player.set_time(self.player.get_time() - 10000)
def set_volume(self, value):
self.player.audio_set_volume(int(value))
def fullscreen(self):
self.player.set_fullscreen(True)
def open_file(self):
filename = filedialog.askopenfilename(title="打开文件", filetypes=[("视频文件", ".mp4 .mkv .avi")])
if filename:
self.player.set_media(self.vlc_instance.media_new(filename))
self.player.play()
if __name__ == "__main__":
root = tk.Tk()
video_player = VideoPlayer(root)
root.mainloop()
使用方法
- 运行程序,会出现一个窗口。
- 点击"打开文件"按钮,选择一个视频文件。
- 点击"播放"按钮,开始播放视频。
- 点击"暂停"按钮,暂停播放。
- 点击"停止"按钮,停止播放。
- 点击"快进"或"快退"按钮,快进或快退 10 秒。
- 拖动音量滑块,调整音量。
- 点击"全屏"按钮,切换全屏模式。
注意
- 该程序使用 VLC 媒体播放器库,需要安装 VLC 媒体播放器。
- 该程序仅支持 MP4、MKV 和 AVI 格式的视频文件。