我正在尝试制作一个简单的程序来保留或删除文件夹(及其子文件夹)中的视频,但是虽然我可以听到视频,但看不到它。
import os
import wx
import wx.media
from moviepy.editor import VideoFileClip
class VideoSelector(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Video Selector', size=(800, 600))
self.panel = wx.Panel(self)
self.media_ctrl = wx.media.MediaCtrl(self.panel, style=wx.SIMPLE_BORDER)
self.yes_button = wx.Button(self.panel, label="Yes (Keep)", pos=(10, 540))
self.no_button = wx.Button(self.panel, label="No (Delete)", pos=(120, 540))
self.video_list = []
self.current_video_index = 0
self.yes_button.Bind(wx.EVT_BUTTON, self.keep_video)
self.no_button.Bind(wx.EVT_BUTTON, self.delete_video)
self.Bind(wx.EVT_CLOSE, self.quit)
self.Show()
self.load_videos_in_directory()
def get_absolute_path(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
folder_path = os.path.join(dir_path, 'downloads')
return folder_path
def play(self, video_path):
self.media_ctrl.Stop()
if self.media_ctrl.Load(video_path):
self.media_ctrl.Play()
else:
print("Media not found")
self.quit(None)
def quit(self, event):
self.media_ctrl.Stop()
self.Destroy()
def keep_video(self, event):
if self.current_video_index < len(self.video_list):
self.check_video_duration(self.video_list[self.current_video_index])
self.next_video()
else:
wx.MessageBox("No more videos in the directory.", "Info", wx.OK | wx.ICON_INFORMATION)
def delete_video(self, event):
if self.current_video_index < len(self.video_list):
os.remove(self.video_list[self.current_video_index])
self.next_video()
else:
wx.MessageBox("No more videos in the directory.", "Info", wx.OK | wx.ICON_INFORMATION)
def next_video(self):
self.current_video_index += 1
if self.current_video_index < len(self.video_list):
video_path = self.video_list[self.current_video_index]
self.play(video_path)
else:
wx.MessageBox("No more videos in the directory.", "Info", wx.OK | wx.ICON_INFORMATION)
self.media_ctrl.Stop()
def check_video_duration(self, video_path):
clip = VideoFileClip(video_path)
duration = clip.duration
if duration > 120:
os.remove(video_path)
def load_videos_in_directory(self):
directory = self.get_absolute_path()
self.video_list = self.get_video_list(directory)
if not self.video_list:
wx.MessageBox("No video files found in the directory.", "Info", wx.OK | wx.ICON_INFORMATION)
else:
self.current_video_index = 0
self.play(self.video_list[self.current_video_index])
def get_video_list(self, directory):
video_list = []
for root, _, files in os.walk(directory):
for file in files:
if file.lower().endswith(('.mp4', '.avi', '.mkv', '.mov')):
video_list.append(os.path.join(root, file))
return video_list
if __name__ == '__main__':
app = wx.App()
Frame = VideoSelector()
app.MainLoop()
您已经将按钮放在视频顶部,没有指定其大小或位置,所以这就是您所看到的。
最好使用一些尺寸调整器来避免此类事情,因为他们会为您整理位置和布局。
真的!大致尝试这样的事情。 (您可能不需要指定
backend
,否则它对于您的系统来说是错误的。)
import os
import wx
import wx.media
#from moviepy.editor import VideoFileClip
class VideoSelector(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Video Selector', size=(800, 600))
self.panel = wx.Panel(self)
self.media_ctrl = wx.media.MediaCtrl(self, szBackend=wx.media.MEDIABACKEND_GSTREAMER)
self.yes_button = wx.Button(self.panel, label="Yes (Keep)")
self.no_button = wx.Button(self.panel, label="No (Delete)")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer2 = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.media_ctrl, 1, wx.EXPAND|wx.ALL, 5)
sizer2.Add(self.yes_button)
sizer2.Add(self.no_button)
self.panel.SetSizer(sizer2)
sizer.Add(self.panel, 0, wx.ALL, 5)
self.SetSizer(sizer)
self.video_list = []
self.current_video_index = 0
self.yes_button.Bind(wx.EVT_BUTTON, self.keep_video)
self.no_button.Bind(wx.EVT_BUTTON, self.delete_video)
self.Bind(wx.EVT_CLOSE, self.quit)
self.media_ctrl.Bind(wx.media.EVT_MEDIA_LOADED, self.play)
self.Show()
self.load_videos_in_directory()
def get_absolute_path(self):
dir_path = os.path.dirname(os.path.realpath(__file__))
folder_path = os.path.join(dir_path, '') #downloads')
return folder_path
def load(self, video_path):
self.media_ctrl.Stop()
if self.media_ctrl.Load(video_path):
pass
else:
print("Media not found")
self.quit(None)
def play(self, event):
self.media_ctrl.Play()
self.Refresh()
def quit(self, event):
self.media_ctrl.Stop()
self.Destroy()
def keep_video(self, event):
if self.current_video_index < len(self.video_list):
self.check_video_duration(self.video_list[self.current_video_index])
self.next_video()
else:
wx.MessageBox("No more videos in the directory.", "Info", wx.OK | wx.ICON_INFORMATION)
def delete_video(self, event):
if self.current_video_index < len(self.video_list):
os.remove(self.video_list[self.current_video_index])
self.next_video()
else:
wx.MessageBox("No more videos in the directory.", "Info", wx.OK | wx.ICON_INFORMATION)
def next_video(self):
self.current_video_index += 1
if self.current_video_index < len(self.video_list):
video_path = self.video_list[self.current_video_index]
self.load(video_path)
else:
wx.MessageBox("No more videos in the directory.", "Info", wx.OK | wx.ICON_INFORMATION)
self.media_ctrl.Stop()
def check_video_duration(self, video_path):
return
#clip = VideoFileClip(video_path)
#duration = clip.duration
#if duration > 120:
# os.remove(video_path)
def load_videos_in_directory(self):
directory = self.get_absolute_path()
self.video_list = self.get_video_list(directory)
if not self.video_list:
wx.MessageBox("No video files found in the directory.", "Info", wx.OK | wx.ICON_INFORMATION)
else:
self.current_video_index = 0
self.load(self.video_list[self.current_video_index])
def get_video_list(self, directory):
video_list = []
for root, _, files in os.walk(directory):
for file in files:
if file.lower().endswith(('.mp4', '.avi', '.mkv', '.mov')):
video_list.append(os.path.join(root, file))
return video_list
if __name__ == '__main__':
app = wx.App()
Frame = VideoSelector()
app.MainLoop()