def isTrimM3u8File(self): #默认分段时间 if self.VideoDuration <= 120: DefSegmentTime = 2 elif 120 < self.VideoDuration <= 600: DefSegmentTime = 4 else: DefSegmentTime = 10 SuccFlag = True SegmentTime = DefSegmentTime self.OptimalSegTime = DefSegmentTime RunCmdObj = baseclass.runCmd(self.logger, self.VideoFileNameAlias, self.IsWaterMark, self.IsOverWrite) #尝试划分240p.mp4 #清理工作目录 RunCmdObj.run( ["rm -rf /tmp/%s/%s" % (self.VideoFileNameAlias, "calst")]) cmd = " cd /tmp/%s/ && mkdir %s &&" cmd = cmd + " ffmpeg.1.0.1 -i %s.%s -acodec copy -vcodec copy -bsf:v h264_mp4toannexb -f mpegts /tmp/%s/%s/output.ts &&" cmd = cmd + " cd /tmp/%s/%s/ &&" cmdstr = cmd % (self.VideoFileNameAlias, "calst", "240p", self.VideoFormat, self.VideoFileNameAlias, "calst", self.VideoFileNameAlias, "calst") cmdstr = cmdstr + " ffmpeg.1.0.1 -i output.ts -vcodec copy -acodec copy -map 0 -f segment -segment_time_delta 0.5 -segment_time %s" % SegmentTime cmdstr = cmdstr + " -segment_list output.m3u8 -segment_list_type m3u8 -segment_format mpegts output%04d.ts" RunCmdObj.run([cmdstr]) #得到划分后文件列表,找出最后一段ts文件 TsFileList = os.listdir("/tmp/%s/%s/" % (self.VideoFileNameAlias, "calst")) TsFileList.remove("output.m3u8") TsFileList.sort() FinalTsFile = TsFileList[len(TsFileList) - 1] #分析最后一段ts文件是否是正常视频文件 FinalTsFilePath = "/tmp/%s/%s/%s" % (self.VideoFileNameAlias, "calst", FinalTsFile) RunCmdObj.run(["ffprobe -show_streams %s" % (FinalTsFilePath)], QuitFlag=False) try: VStreamsList = baseclass.getVideoMetaDList(RunCmdObj.stdout, "STREAM") except Exception, e: VStreamsList = []
def isTrimM3u8File(self): #默认分段时间 if self.VideoDuration <= 120: DefSegmentTime = 2 elif 120 < self.VideoDuration <= 600: DefSegmentTime = 4 else: DefSegmentTime = 10 SuccFlag = True SegmentTime = DefSegmentTime self.OptimalSegTime = DefSegmentTime RunCmdObj = baseclass.runCmd(self.logger,self.VideoFileNameAlias,self.IsWaterMark,self.IsOverWrite) #尝试划分240p.mp4 #清理工作目录 RunCmdObj.run(["rm -rf /tmp/%s/%s"%(self.VideoFileNameAlias,"calst")]) cmd = " cd /tmp/%s/ && mkdir %s &&" cmd = cmd + " ffmpeg.1.0.1 -i %s.%s -acodec copy -vcodec copy -bsf:v h264_mp4toannexb -f mpegts /tmp/%s/%s/output.ts &&" cmd = cmd + " cd /tmp/%s/%s/ &&" cmdstr = cmd%(self.VideoFileNameAlias,"calst","240p",self.VideoFormat,self.VideoFileNameAlias,"calst",self.VideoFileNameAlias,"calst") cmdstr = cmdstr + " ffmpeg.1.0.1 -i output.ts -vcodec copy -acodec copy -map 0 -f segment -segment_time_delta 0.5 -segment_time %s"%SegmentTime cmdstr = cmdstr + " -segment_list output.m3u8 -segment_list_type m3u8 -segment_format mpegts output%04d.ts" RunCmdObj.run([cmdstr]) #得到划分后文件列表,找出最后一段ts文件 TsFileList = os.listdir("/tmp/%s/%s/"%(self.VideoFileNameAlias,"calst")) TsFileList.remove("output.m3u8") TsFileList.sort() FinalTsFile = TsFileList[len(TsFileList)-1] #分析最后一段ts文件是否是正常视频文件 FinalTsFilePath = "/tmp/%s/%s/%s"%(self.VideoFileNameAlias,"calst",FinalTsFile) RunCmdObj.run(["ffprobe -show_streams %s"%(FinalTsFilePath)],QuitFlag=False) try: VStreamsList = baseclass.getVideoMetaDList(RunCmdObj.stdout,"STREAM") except Exception,e: VStreamsList = []
# BitRate : 总码率 # VideoBitRate : 视频流码率 # AudioBitRate : 音频流码率 # VideoFrameWidth : 视频帧宽度值 # VideoFrameHeight : 视频帧高度值 # VideoDuration : 视频时长 # VideoSize : 视频大小 # VideoCode : 视频编码方式 # AudioCode : 音频编码方式 # AudioSampleRate : 音频采样率 # AudioChannels : 音频声道,单声道或双声道. # --/ RunCmdObj.run(["%s -show_format %s"%(FFPROBE,VideoFilePath)]) VFormatList = baseclass.getVideoMetaDList(RunCmdObj.stdout,"FORMAT") VFormatDict = VFormatList[0] VideoSize = int(VFormatDict["size"]) BitRate = VFormatDict["bit_rate"] VideoDuration = float(VFormatDict["duration"]) StreamNum = int(VFormatDict["nb_streams"]) RunCmdObj.run(["%s -show_streams %s"%(FFPROBE,VideoFilePath)]) VStreamsList = baseclass.getVideoMetaDList(RunCmdObj.stdout,"STREAM") # --/ # 依据视频文件包含的流个数和类型进行处理
# 以下信息由调用系统命令'ffprobe -show_format' , 'ffprobe -show_streams'获得. # BitRate : 总码率 # VideoBitRate : 视频流码率 # AudioBitRate : 音频流码率 # VideoFrameWidth : 视频帧宽度值 # VideoFrameHeight : 视频帧高度值 # VideoDuration : 视频时长 # VideoSize : 视频大小 # VideoCode : 视频编码方式 # AudioCode : 音频编码方式 # AudioSampleRate : 音频采样率 # AudioChannels : 音频声道,单声道或双声道. # --/ RunCmdObj.run(["%s -show_format %s" % (FFPROBE, VideoFilePath)]) VFormatList = baseclass.getVideoMetaDList(RunCmdObj.stdout, "FORMAT") VFormatDict = VFormatList[0] VideoSize = int(VFormatDict["size"]) BitRate = VFormatDict["bit_rate"] VideoDuration = float(VFormatDict["duration"]) StreamNum = int(VFormatDict["nb_streams"]) RunCmdObj.run(["%s -show_streams %s" % (FFPROBE, VideoFilePath)]) VStreamsList = baseclass.getVideoMetaDList(RunCmdObj.stdout, "STREAM") # --/ # 依据视频文件包含的流个数和类型进行处理 # --/
def isTrimM3u8File(self): #默认分段时间 if self.VideoDuration <= 120: DefSegmentTime = 2 elif 120 < self.VideoDuration <= 600: DefSegmentTime = 4 else: DefSegmentTime = 10 SuccFlag = True SegmentTime = DefSegmentTime self.OptimalSegTime = DefSegmentTime RunCmdObj = baseclass.runCmd(self.logger,self.VideoFileNameAlias,self.IsWaterMark,self.IsOverWrite) #尝试划分240p.mp4 #清理工作目录 RunCmdObj.run(["rm -rf /tmp/%s/%s"%(self.VideoFileNameAlias,"calst")]) cmd = " cd /tmp/%s/ && mkdir %s &&" cmd = cmd + " ffmpeg -i %s.%s -acodec copy -vcodec copy -bsf:v h264_mp4toannexb -f mpegts /tmp/%s/%s/output.ts &&" cmd = cmd + " cd /tmp/%s/%s/ &&" cmdstr = cmd%(self.VideoFileNameAlias,"calst","240p",self.VideoFormat,self.VideoFileNameAlias,"calst",self.VideoFileNameAlias,"calst") cmdstr = cmdstr + " ffmpeg -i output.ts -vcodec copy -acodec copy -map 0 -f segment -segment_time_delta 0.5 -segment_time %s"%SegmentTime cmdstr = cmdstr + " -segment_list output.m3u8 -segment_list_type m3u8 -segment_format mpegts output%04d.ts" RunCmdObj.run([cmdstr]) #得到划分后文件列表,找出最后一段ts文件 TsFileList = os.listdir("/tmp/%s/%s/"%(self.VideoFileNameAlias,"calst")) TsFileList.remove("output.m3u8") TsFileList.sort() FinalTsFile = TsFileList[len(TsFileList)-1] #分析最后一段ts文件是否是正常视频文件 FinalTsFilePath = "/tmp/%s/%s/%s"%(self.VideoFileNameAlias,"calst",FinalTsFile) RunCmdObj.run(["ffprobe -show_streams %s"%(FinalTsFilePath)]) VStreamsList = baseclass.getVideoMetaDList(RunCmdObj.stdout,"STREAM") VStream = 0 AStream = 0 if len(VStreamsList) == 2: for i in range(2): if VStreamsList[i]["codec_type"] == "video": VStream = VStream + 1 if VStream == 1: VStreamsVDict = VStreamsList[i] if VStreamsList[i]["codec_type"] == "audio": AStream = AStream + 1 if AStream == 1: VStreamsADict = VStreamsList[i] if VStream == 1 and AStream == 1: #最后一段ts文件视频流正常,音频流异常. audio_sample_rate = VStreamsADict["sample_rate"] audio_channels = VStreamsADict["channels"] audio_bit_rate = VStreamsADict["bit_rate"] if audio_sample_rate == 0 or audio_channels == 0 or audio_bit_rate == "N/A": SuccFlag = False elif VStream == 2 and AStream == 0: #最后一段ts文件只有视频流,无音频流. SuccFlag = False else: #最后一段ts文件只有音频流,无视频流. SuccFlag = False elif len(VStreamsList) == 1: #最后一段ts文件只包含一个流 SuccFlag = False else: #最后一段ts文件包含大于2个流 SuccFlag = False if SuccFlag == False: self.TrimM3u8Flag = True self.logger.info("Final TS segment file have problem,Need trim m3u8 file") #清理工作目录 RunCmdObj.run(["rm -rf /tmp/%s/%s"%(self.VideoFileNameAlias,"calst")])
@author: wye Copyright @ 2011 - 2012 Cloudiya Tech . Inc ''' import sys import baseclass FFPROBE="/usr/local/bin/ffprobe" # Stream file path StreamFilepath = sys.argv[1] # Video file Name Alias VideoAliasName = sys.argv[2] # Stream Property name PropertyFlag = sys.argv[3] logger = baseclass.getlog(VideoAliasName) RunCmdObj = baseclass.runCmd(logger,VideoAliasName,False,False) RunCmdObj.run(["%s -show_format %s"%(FFPROBE,StreamFilepath)]) VFormatList = baseclass.getVideoMetaDList(RunCmdObj.stdout,"FORMAT") VFormatDict = VFormatList[0] if PropertyFlag == "ab":print(VFormatDict["bit_rate"])
def isTrimM3u8File(self): #默认分段时间 if self.VideoDuration <= 120: DefSegmentTime = 2 elif 120 < self.VideoDuration <= 600: DefSegmentTime = 4 else: DefSegmentTime = 10 SuccFlag = True SegmentTime = DefSegmentTime self.OptimalSegTime = DefSegmentTime RunCmdObj = baseclass.runCmd(self.logger, self.VideoFileNameAlias, self.IsWaterMark, self.IsOverWrite) #尝试划分240p.mp4 #清理工作目录 RunCmdObj.run( ["rm -rf /tmp/%s/%s" % (self.VideoFileNameAlias, "calst")]) cmd = " cd /tmp/%s/ && mkdir %s &&" cmd = cmd + " ffmpeg -i %s.%s -acodec copy -vcodec copy -bsf:v h264_mp4toannexb -f mpegts /tmp/%s/%s/output.ts &&" cmd = cmd + " cd /tmp/%s/%s/ &&" cmdstr = cmd % (self.VideoFileNameAlias, "calst", "240p", self.VideoFormat, self.VideoFileNameAlias, "calst", self.VideoFileNameAlias, "calst") cmdstr = cmdstr + " ffmpeg -i output.ts -vcodec copy -acodec copy -map 0 -f segment -segment_time_delta 0.5 -segment_time %s" % SegmentTime cmdstr = cmdstr + " -segment_list output.m3u8 -segment_list_type m3u8 -segment_format mpegts output%04d.ts" RunCmdObj.run([cmdstr]) #得到划分后文件列表,找出最后一段ts文件 TsFileList = os.listdir("/tmp/%s/%s/" % (self.VideoFileNameAlias, "calst")) TsFileList.remove("output.m3u8") TsFileList.sort() FinalTsFile = TsFileList[len(TsFileList) - 1] #分析最后一段ts文件是否是正常视频文件 FinalTsFilePath = "/tmp/%s/%s/%s" % (self.VideoFileNameAlias, "calst", FinalTsFile) RunCmdObj.run(["ffprobe -show_streams %s" % (FinalTsFilePath)]) VStreamsList = baseclass.getVideoMetaDList(RunCmdObj.stdout, "STREAM") VStream = 0 AStream = 0 if len(VStreamsList) == 2: for i in range(2): if VStreamsList[i]["codec_type"] == "video": VStream = VStream + 1 if VStream == 1: VStreamsVDict = VStreamsList[i] if VStreamsList[i]["codec_type"] == "audio": AStream = AStream + 1 if AStream == 1: VStreamsADict = VStreamsList[i] if VStream == 1 and AStream == 1: #最后一段ts文件视频流正常,音频流异常. audio_sample_rate = VStreamsADict["sample_rate"] audio_channels = VStreamsADict["channels"] audio_bit_rate = VStreamsADict["bit_rate"] if audio_sample_rate == 0 or audio_channels == 0 or audio_bit_rate == "N/A": SuccFlag = False elif VStream == 2 and AStream == 0: #最后一段ts文件只有视频流,无音频流. SuccFlag = False else: #最后一段ts文件只有音频流,无视频流. SuccFlag = False elif len(VStreamsList) == 1: #最后一段ts文件只包含一个流 SuccFlag = False else: #最后一段ts文件包含大于2个流 SuccFlag = False if SuccFlag == False: self.TrimM3u8Flag = True self.logger.info( "Final TS segment file have problem,Need trim m3u8 file") #清理工作目录 RunCmdObj.run( ["rm -rf /tmp/%s/%s" % (self.VideoFileNameAlias, "calst")])