Python调用ffmpeg开源视频处理库,批量处理视频
代码示例
#coding=utf-8 importos importsubprocess importdatetime importjson,pprint importre,time importthreading importrandom importshutil classFFmpeg: def__init__(self,editvdo,addlogo=None,addmusic=None, addvdohead=None,addvdotail=None): self.editvdo=editvdo self.addlogo=addlogo self.addmusic=addmusic self.addvdohead=addvdohead self.addvdotail=addvdotail self.vdo_time,self.vdo_width,self.vdo_height,self.attr_dict=self.get_attr() self.editvdo_path=os.path.dirname(editvdo) self.editvdo_name=os.path.basename(editvdo) defget_attr(self): """ 获取视频属性参数 :return: """ strcmd=r'ffprobe-print_formatjson-show_streams-i"{}"'.format(self.editvdo) status,output=subprocess.getstatusoutput(strcmd) agrs=eval(re.search('{.*}',output,re.S).group().replace("\n","").replace("",'')) streams=agrs.get('streams',[]) agrs_dict=dict() [agrs_dict.update(x)forxinstreams] vdo_time=agrs_dict.get('duration') vdo_width=agrs_dict.get('width') vdo_height=agrs_dict.get('height') attr=(vdo_time,vdo_width,vdo_height,agrs_dict) returnattr defedit_head(self,start_time,end_time,deposit=None): """ 截取指定长度视频 :paramsecond:去除开始的多少秒 :paramdeposit:另存为文件 :return:True/Flase """ ifNone==deposit: deposit=self.editvdo_path+'/'+'edit_head'+self.editvdo_name start=time.strftime('%H:%M:%S',time.gmtime(start_time)) end=time.strftime('%H:%M:%S',time.gmtime(end_time)) strcmd='ffmpeg-i"{}"-vcodeccopy-acodeccopy-ss{}-to{}"{}"-y'.format( self.editvdo,start,end,deposit) result=subprocess.run(args=strcmd,stdout=subprocess.PIPE,shell=True) ifos.path.exists(deposit): os.remove(self.editvdo) os.rename(deposit,self.editvdo) returnTrue else: returnFalse defedit_logo(self,deposit=None): """ 添加水印 :paramdeposit:添加水印后另存为路径,为空则覆盖 :return:True/False """ ifNone==deposit: deposit=self.editvdo_path+'/'+'edit_logo'+self.editvdo_name strcmd=r'ffmpeg-i"{}"-vf"movie=\'{}\'[watermark];[in]'\ r'[watermark]overlay=main_w-overlay_w-10:10[out]""{}"'.format( self.editvdo,self.addlogo,deposit) result=subprocess.run(args=strcmd,stdout=subprocess.PIPE,shell=True) ifos.path.exists(deposit): os.remove(self.editvdo) os.rename(deposit,self.editvdo) returnTrue else: returnFalse defedit_music(self,deposit=None): ifNone==deposit: deposit=self.editvdo_path+'/'+'edit_music'+self.editvdo_name strcmd=r'ffmpeg-y-i"{}"-i"{}"-filter_complex"[0:a]'\ r'pan=stereo|c0=1*c0|c1=1*c1[a1],[1:a]'\ r'pan=stereo|c0=1*c0|c1=1*c1[a2],[a1][a2]amix=duration=first,'\ r'pan=stereo|c0以上就是Python调用ffmpeg开源视频处理库,批量处理视频的详细内容,更多关于python批量处理视频的资料请关注毛票票其它相关文章!