[Python] 纯文本查看 复制代码
import wx
from moviepy.editor import *
import os
#转mp4
def convert_to_mp4(input_video_path, output_directory):
# 获取输入视频文件的文件名(不包括扩展名)
video_name = os.path.splitext(os.path.basename(input_video_path))[0]
# 加载视频
video = VideoFileClip(input_video_path)
# 构建输出 mp4 文件路径
output_mp4_path = os.path.join(output_directory, f"{video_name}.mp4")
# 转换为 MP4 格式
video.write_videofile(output_mp4_path, codec='libx264')
# 关闭视频文件
video.close()
#转MP3
def convert_to_mp3(input_video_path, output_directory):
# 获取输入视频文件的文件名(不包括扩展名)
video_name = os.path.splitext(os.path.basename(input_video_path))[0]
video = VideoFileClip(input_video_path)
# 提取音频轨道
audio = video.audio
# 构建输出 mp3 文件路径
output_mp3_path = os.path.join(output_directory, f"{video_name}.mp3")
# 保存为 MP3 格式
audio.write_audiofile(output_mp3_path)
# 关闭视频和音频文件
video.close()
audio.close()
#转avi
def convert_to_avi(input_video_path, output_directory):
# 获取输入视频文件的文件名(不包括扩展名)
video_name = os.path.splitext(os.path.basename(input_video_path))[0]
# 加载视频
video = VideoFileClip(input_video_path)
# 构建输出 AVI 文件路径
output_avi_path = os.path.join(output_directory, f"{video_name}.avi")
# 转换为 AVI 格式
video.write_videofile(output_avi_path, codec='libx264', audio_codec='pcm_s16le')
# 关闭视频文件
video.close()
#转wmv
def convert_to_wmv(input_video_path, output_directory):
# 获取输入视频文件的文件名(不包括扩展名)
video_name = os.path.splitext(os.path.basename(input_video_path))[0]
# 加载视频
video = VideoFileClip(input_video_path)
# 构建输出 WMV 文件路径
output_wmv_path = os.path.join(output_directory, f"{video_name}.wmv")
# 转换为 WMV 格式
video.write_videofile(output_wmv_path, codec='wmv2')
# 关闭视频文件
video.close()
#转gif
def convert_to_gif(input_video_path, output_directory, fps=10):
# 获取输入视频文件的文件名(不包括扩展名)
video_name = os.path.splitext(os.path.basename(input_video_path))[0]
# 加载视频
video = VideoFileClip(input_video_path)
# 构建输出 gif 文件路径
output_gif_path = os.path.join(output_directory, f"{video_name}.gif")
# 将视频转换为 GIF
video.write_gif(output_gif_path, fps=fps)
# 关闭视频文件
video.close()
#转mov
def convert_to_mov(input_video_path, output_directory):
# 获取输入视频文件的文件名(不包括扩展名)
video_name = os.path.splitext(os.path.basename(input_video_path))[0]
# 加载视频
video = VideoFileClip(input_video_path)
# 构建输出 MOV 文件路径
output_mov_path = os.path.join(output_directory, f"{video_name}.mov")
# 转换为 MOV 格式
video.write_videofile(output_mov_path, codec='libx264', fps=video.fps)
# 关闭视频文件
video.close()
#函数集合
def function_dispatcher(number,input_video_path,output_directory):
functions = {
0: convert_to_mp4,
1: convert_to_avi,
2: convert_to_wmv,
3: convert_to_mp3,
4: convert_to_gif,
5: convert_to_mov,
}
if number in functions:
functions[number](input_video_path,output_directory)
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='原创:吾爱qianaonan', size=(430, 239), name='frame', style=541072384)
self.启动窗口 = wx.Panel(self)
self.Centre()
self.编辑框1 = wx.TextCtrl(self.启动窗口, size=(293, 29), pos=(8, 5), value='', name='text', style=16)
self.按钮1 = wx.Button(self.启动窗口, size=(80, 32), pos=(315, 3), label='选择文件', name='button')
self.标签1 = wx.StaticText(self.启动窗口, size=(80, 24), pos=(10, 44), label='转换成', name='staticText',
style=2321)
self.单选框2 = wx.RadioButton(self.启动窗口, size=(58, 24), pos=(8, 71), name='radioButton', label='MP4')
self.单选框3 = wx.RadioButton(self.启动窗口, size=(40, 24), pos=(73, 71), name='radioButton', label='AVI')
self.单选框4 = wx.RadioButton(self.启动窗口, size=(58, 24), pos=(136, 71), name='radioButton', label='WMV')
self.单选框5 = wx.RadioButton(self.启动窗口, size=(61, 24), pos=(209, 71), name='radioButton', label='MP3')
self.单选框6 = wx.RadioButton(self.启动窗口, size=(42, 24), pos=(275, 71), name='radioButton', label='GIF')
self.单选框7 = wx.RadioButton(self.启动窗口, size=(80, 24), pos=(327, 71), name='radioButton', label='MOV')
self.按钮2 = wx.Button(self.启动窗口, size=(80, 32), pos=(317, 107), label='保存目录', name='button')
self.编辑框2 = wx.TextCtrl(self.启动窗口, size=(293, 29), pos=(8, 107), value='', name='text', style=16)
self.按钮3 = wx.Button(self.启动窗口, size=(80, 32), pos=(254, 156), label='开始转换', name='button')
self.Bind(wx.EVT_BUTTON, self.OnSelectFile, self.按钮1) # 绑定选择文件按钮的事件
self.Bind(wx.EVT_BUTTON, self.OnSelectFolder, self.按钮2)
self.按钮3.Bind(wx.EVT_BUTTON, self.按钮3_按钮被单击)
self.标签2 = wx.StaticText(self.启动窗口,size=(195, 36),pos=(15, 153),label='请谨慎使用gif转换,一般是视频过\n长会导致程序卡死',name='staticText',style=17)
self.标签2.SetForegroundColour((255, 0, 0, 255))
self.标签3 = wx.StaticText(self.启动窗口,size=(80, 17),pos=(254, 56),label='10帧/秒',name='staticText',style=2321)
self.标签4 = wx.StaticText(self.启动窗口,size=(80, 46),pos=(335, 151),label='视频越长\n花费时间越长',name='staticText',style=2321)
self.标签4.SetForegroundColour((255, 0, 0, 255))
def GetSelectedRadioButtonIndex(self):
radio_buttons = [self.单选框2, self.单选框3, self.单选框4, self.单选框5, self.单选框6, self.单选框7]
for i, button in enumerate(radio_buttons):
if button.GetValue():
return i
return None
def OnSelectFile(self, event):
wildcard = "Video Files (*.mp4;*.avi;*.wmv;*.mov)|*.mp4;*.avi;*.wmv;*.mov|All Files (*.*)|*.*" # 文件类型过滤器
dialog = wx.FileDialog(self, "选择文件", wildcard=wildcard,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE)
if dialog.ShowModal() == wx.ID_CANCEL:
return
paths = dialog.GetPaths()
directories = paths # 提取文件路径
self.编辑框1.SetValue("\n".join(directories)) # 在编辑框中显示目录,使用换行分隔多个目录
dialog.Destroy()
def OnSelectFolder(self, event):
dialog = wx.DirDialog(self, "选择文件夹", style=wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
if dialog.ShowModal() == wx.ID_CANCEL:
return
folder_path = dialog.GetPath()
self.编辑框2.SetValue(folder_path) # 在编辑框2中显示所选文件夹路径
dialog.Destroy()
def 按钮3_按钮被单击(self,event):
selected_index = self.GetSelectedRadioButtonIndex()#输出单选项位置位置
input_path1=self.编辑框1.GetValue()
output_directory1=self.编辑框2.GetValue()
#print(type(selected_index))
function_dispatcher(selected_index,input_path1,output_directory1)
class myApp(wx.App):
def OnInit(self):
self.frame = Frame()
self.frame.Show(True)
return True
if __name__ == '__main__':
app = myApp()
app.MainLoop()