使用python批量视频编辑脚本

#coding:utf-8
import wx
from moviepy.editor import *
import sys
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title,size = (500,250))

panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)

hbox1 = wx.BoxSizer(wx.HORIZONTAL)
l1 = wx.StaticText(panel, -1, "去重之前的视频路径:")

hbox1.Add(l1, 1, wx.EXPAND|wx.ALIGN_LEFT|wx.ALL,5)

self.t1 = wx.TextCtrl(panel,style = wx.TE_READONLY|wx.TE_LEFT)

hbox1.Add(self.t1,1,wx.EXPAND|wx.ALIGN_LEFT|wx.ALL,5)

self.t1.Bind(wx.EVT_TEXT,self.OnKeyTyped)
self.button = wx.Button(panel, -1, "选择文件夹", pos=(300, 5))
self.Bind(wx.EVT_BUTTON, self.OnButton1, self.button)
self.button.SetDefault()


vbox.Add(hbox1)

hbox2 = wx.BoxSizer(wx.HORIZONTAL)
l2 = wx.StaticText(panel, -1, "去重之后的视频路径:")

hbox2.Add(l2, 1, wx.ALIGN_LEFT|wx.ALL,5)
self.t2 = wx.TextCtrl(panel,style = wx.TE_READONLY|wx.TE_LEFT)

hbox2.Add(self.t2,1,wx.EXPAND|wx.ALIGN_LEFT|wx.ALL,5)

self.button = wx.Button(panel, -1, "选择文件夹", pos=(300, 40))
self.Bind(wx.EVT_BUTTON, self.OnButton2, self.button)
self.button.SetDefault()

self.button = wx.Button(panel, -1, "开始处理", pos=(180, 100))
self.Bind(wx.EVT_BUTTON, self.OnButton3, self.button)
self.button.SetDefault()


vbox.Add(hbox2)

#self.t2.Bind(wx.EVT_TEXT_MAXLEN,self.OnMaxLen)

panel.SetSizer(vbox)

self.Centre()
self.Show()
self.Fit()

def OnKeyTyped(self, event):
print event.GetString()


def OnButton1(self, event):
""""""
dlg = wx.DirDialog(self,u"选择文件夹",style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
self.t1.Clear()
self.t1.AppendText(dlg.GetPath())
print dlg.GetPath() #文件夹路径

dlg.Destroy()

def OnButton2(self, event):
""""""
dlg = wx.DirDialog(self,u"选择文件夹",style=wx.DD_DEFAULT_STYLE)
if dlg.ShowModal() == wx.ID_OK:
self.t2.Clear()
self.t2.AppendText(dlg.GetPath())
#print dlg.GetPath() #文件夹路径

dlg.Destroy()

def OnButton3(self, event):
""""""
filelist=[]
rootdir=self.t1.GetValue()
newrootdir=self.t2.GetValue()
filelist=self.getallfilename(filelist,rootdir)

#print filelist
#for videofile in filelist:
# self.video_editor(self,videofile,)
print filelist
for videofile in filelist:
print repr(self.dofullfilename(rootdir,videofile))
print repr(self.dofullfilename(newrootdir,videofile))
# self.video_editor(self.dofullfilename(rootdir,videofile),self.dofullfilename(newrootdir,videofile))
self.video_editor(self.dofullfilename(rootdir,videofile),self.dofullfilename(newrootdir,videofile))

wx.MessageBox("处理完成", "处理完成" ,wx.OK | wx.ICON_INFORMATION)
def getallfileload(self,filelist,rootdir):

FileNames = os.listdir(rootdir)
for file_name in FileNames:
fullfilename=os.path.join(rootdir,file_name)
filelist.append(fullfilename)
print fullfilename

return filelist
def getallfilename(self,filelist,rootdir):

for parent,dirnames,filenames in os.walk(rootdir):
for filename in filenames:
filelist.append(filename)
return filelist

def dofullfilename(self,newrootdir,file_name):
newfilename=os.path.join(newrootdir,file_name)
return newfilename

def video_editor(self,oldvideo,newvideo):
""""""

video = VideoFileClip(oldvideo)
# video = VideoFileClip(u'E:\\test\\1.mp4')
# Load myHolidays.mp4 and select the subclip 00:00:05 - 00:00:10
#clip = VideoFileClip(u'E:\\test\\1.mp4').subclip(0,280)
print int(video.duration)
clip = VideoFileClip(oldvideo).subclip(0,int(video.duration))
# Generate a text clip (many options available ! )
#image_clip = ImageClip('../image/logo.png')
#image_clip=image_clip.set_duration(12).set_position(650,10).set_start(0)

txt_clip = TextClip(unicode(" "),fontsize=50,color='white')
txt_clip = txt_clip.set_pos('center').set_duration(int(video.duration))

# Overlay the text clip above the first clip
final_clip = CompositeVideoClip([clip, txt_clip])

# write the result to a file in any format
#final_clip.to_videofile(u'E:\\test\\new\\1.mp4')
final_clip.to_videofile(newvideo)


def get_file_times(self,filename):
u"""
获取视频时长(s:秒)
"""
clip = VideoFileClip(filename)
file_time = clip.duration
return file_time
def OnEnterPressed(self,event):
print "Enter pressed"

def OnMaxLen(self,event):
print "Maximum length reached"

###############################################################################
if __name__ == '__main__':
app = wx.App()
Mywin(None, '去重视频编辑')
app.MainLoop()

猜你喜欢

转载自www.cnblogs.com/dengyi/p/8997298.html
今日推荐