在这篇博客中,我将向大家展示如何使用 wxPython 创建一个简单的图形用户界面 (GUI) 应用程序,该应用程序可以管理视频文件列表、播放视频,并生成视频截图。我们将逐步实现这些功能,并确保代码易于理解和扩展。
C:\pythoncode\new\searchmediafileinfolder.py
项目概述
本项目的目标是创建一个视频文件管理器应用,它能够:
- 列出视频文件:用户可以选择一个文件夹,应用会显示该文件夹中的所有视频文件。
- 显示视频时长:用户点击视频文件后,可以查看视频的时长信息。
- 播放视频:用户双击视频文件,应用将调用默认的媒体播放器播放视频。
- 生成视频截图:用户可以选择视频并设定截图时间间隔,应用将生成视频截图,并将截图存放在以视频文件命名的文件夹中。
- 自动打开截图文件夹:截图生成后,应用会自动打开截图文件夹以方便用户查看。
所有代码
import wx
import os
import datetime
import subprocess
import sys
import cv2 # Ensure OpenCV is installed
import threading
class FileListFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="视频文件列表", size=(600, 400))
self.panel = wx.Panel(self)
self.current_path = ""
self.file_list_ctrl = wx.ListCtrl(self.panel, style=wx.LC_REPORT | wx.LC_SINGLE_SEL)
self.file_list_ctrl.InsertColumn(0, "文件名")
self.file_list_ctrl.InsertColumn(1, "大小")
self.file_list_ctrl.InsertColumn(2, "修改时间")
self.file_list_ctrl.Bind(wx.EVT_LIST_ITEM_SELECTED, self.on_file_selected)
self.file_list_ctrl.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.on_file_double_clicked)
self.path_label = wx.StaticText(self.panel, label="路径:")
self.path_textctrl = wx.TextCtrl(self.panel, style=wx.TE_READONLY)
self.path_button = wx.Button(self.panel, label="选择路径")
self.path_button.Bind(wx.EVT_BUTTON, self.on_select_path)
self.interval_label = wx.StaticText(self.panel, label="截图间隔(秒):")
self.interval_textctrl = wx.TextCtrl(self.panel, value="1")
self.capture_button = wx.Button(self.panel, label="生成截图")
self.capture_button.Bind(wx.EVT_BUTTON, self.on_capture)
self.export_button = wx.Button(self.panel, label="导出为文本")
self.export_button.Bind(wx.EVT_BUTTON, self.on_export)
self.play_button = wx.Button(self.panel, label="播放")
self.play_button.Bind(wx.EVT_BUTTON, self.on_play)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.path_label, 0, wx.ALL, 5)
sizer.Add(self.path_textctrl, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 5)
sizer.Add(self.path_button, 0, wx.ALL, 5)
sizer.Add(self.interval_label, 0, wx.ALL, 5)
sizer.Add(self.interval_textctrl, 0, wx.EXPAND | wx.LEFT | wx.RIGHT, 5)
sizer.Add(self.capture_button, 0, wx.ALL, 5)
sizer.Add(self.file_list_ctrl, 1, wx.EXPAND | wx.ALL, 5)
sizer.Add(self.export_button, 0, wx.ALL, 5)
sizer.Add(self.play_button, 0, wx.ALL, 5)
self.panel.SetSizer(sizer)
def on_select_path(self, event):
dlg = wx.DirDialog(self, "选择路径", style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
self.current_path = dlg.GetPath()
self.path_textctrl.SetValue(self.current_path)
self.update_file_list()
dlg.Destroy()
def update_file_list(self):
self.file_list_ctrl.DeleteAllItems()
if not self.current_path:
return
file_list = self.search_video_files(self.current_path)
for filename, file_path, file_size, modified_time in file_list:
modified_time_str = datetime.datetime.fromtimestamp(modified_time)