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 = []
VideoFrameWidth : %s \n \ VideoFrameHeight : %s \n \ VideoDuration : %s \n \ VideoSize : %s \n \ VideoFormat : %s \n \ IsWaterMark : %s \n \ " % (VideoFileNameAlias, VideoFilePath, VideoBitRate, VideoFrameWidth, VideoFrameHeight, VideoDuration, VideoSize, VideoFormat, IsWaterMark)) ################################################# #重新初始化日志对象 logger = baseclass.getlog(VideoFileNameAlias, loglevel=LogLevel) #初始化执行命令对象 RunCmdObj = baseclass.runCmd(logger) ################################################# myselfdir = os.path.split(os.path.realpath(__file__))[0] HadoopBinDir = HadoopInsDir + "/bin/hadoop" HadoopStreamJarPath = HadoopInsDir + "/contrib/streaming/hadoop-streaming-%s.jar" % ( HadoopVersion) ################################################# """ 初始化目录 """ RunCmdObj.run([ "mkdir /tmp/%s && mkdir /tmp/%s/inputfiles && mkdir /tmp/%s/avfiles" % (VideoFileNameAlias, VideoFileNameAlias, VideoFileNameAlias) ])
logger.info("ready to get file....") # --/ # 重新初始化日志对象,执行命令对象,本地临时文件存储环境 # --/ #重新初始化日志对象 logger = baseclass.getlog(vid,loglevel=LogLevel) logger.info("ready to go....") #初始化mysql对象 MysqlObj = baseclass.interWithMysql(logger,vid) #初始化执行命令对象 RunCmdObj = baseclass.runCmd(logger,vid,IsWaterMark,IsOverWrite) logger.info("runcomdobj init...") #初始化Hadoop对象 #HdfsObj = baseclass.hdfs(logger,vid,HadoopBinDir) #初始化webhadoop,mapper对象 MapHost = HadoopNNAddr.split(":")[0] MapWebHdfs = baseclass.WebHadoopOld(MapHost,"50071","cloudiyadatauser",logger) #初始化webhadoop,storage对象 StoHost = "10.2.0.8,10.2.0.10" StoWebHdfs = baseclass.WebHadoop(StoHost,14000,"cloudiyadatauser",logger) logger.info("yes ,the obj init finished...")
logger.info("ready to get file....") # --/ # 重新初始化日志对象,执行命令对象,本地临时文件存储环境 # --/ #重新初始化日志对象 logger = baseclass.getlog(vid, loglevel=LogLevel) logger.info("ready to go....") #初始化mysql对象 MysqlObj = baseclass.interWithMysql(logger, vid) #初始化执行命令对象 RunCmdObj = baseclass.runCmd(logger, vid, IsWaterMark, IsOverWrite) logger.info("runcomdobj init...") #初始化Hadoop对象 #HdfsObj = baseclass.hdfs(logger,vid,HadoopBinDir) #初始化webhadoop,mapper对象 MapHost = HadoopNNAddr.split(":")[0] MapWebHdfs = baseclass.WebHadoopOld(MapHost, "50071", "cloudiyadatauser", logger) #初始化webhadoop,storage对象 StoHost = "10.1.0.8,10.1.0.10" StoWebHdfs = baseclass.WebHadoop(StoHost, 14000, "cloudiyadatauser", logger) logger.info("yes ,the obj init finished...")
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")])
MysqlObj.writeStatus("fail",info_dict['iswatermark'],info_dict['isoverwrite'],info="reducer init exception: %s"%e) sys.exit() #重新初始化日志对象和mysql对象 logger = baseclass.getlog(info_dict['name'],loglevel=info_dict['loglevel']) MysqlObj = baseclass.interWithMysql(logger,info_dict['name']) #初始化redis对象 RedisObj = baseclass.interWithRedis(logger,info_dict['name']) # --/ # Execute commands stage # --/ try: RunCmdObj = baseclass.runCmd(logger,info_dict['name'],info_dict['iswatermark'],info_dict['isoverwrite']) VideoObj = reduceCmdOutput(info_dict,hadoopinfo_dict,logger) RunCmdObj.run(VideoObj.initReducerEnv(),QuitFlag=False) #初始化map hdfs对象 MapHdfsHost = VideoObj.HadoopNNAddr.split(":")[0] MapWebHdfs = baseclass.WebHadoopOld(MapHdfsHost,"50071","cloudiyadatauser",logger) #初始化storage hdfs对象 StoHdfsHost = "10.1.0.8,10.1.0.10" StoWebHdfs = baseclass.WebHadoop(StoHdfsHost,14000,"cloudiyadatauser",logger) VideoAvdir_inhdfs = "/%s/avfiles" % VideoObj.VideoFileNameAlias VideoTmpdir_inlocal = "/tmp/%s" % VideoObj.VideoFileNameAlias
@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"])
#视频元数据 info_dict = pickle.load(open('video.info','r')) #hadoop信息 hadoopinfo_dict = pickle.load(open('hadoop.info','r')) except Exception,e: logger.error("reducer init exception: %s"%e) sys.exit() logger = baseclass.getlog(info_dict['name'],loglevel=info_dict['loglevel']) try: RunCmdObj = baseclass.runCmd(logger) VideoObj = reduceCmdOutput(info_dict,hadoopinfo_dict) RunCmdObj.run(VideoObj.initReducerEnv(),QuitFlag=False) RunCmdObj.run(VideoObj.downMa(),QuitFlag=False) cmddict = VideoObj.complexHandle() RunCmdObj.run(cmddict["download"],QuitFlag=False) RunCmdObj.run(cmddict["ts"]) RunCmdObj.run(cmddict["mv"])
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")])
#重新初始化日志对象和mysql对象 logger = baseclass.getlog(info_dict['name'], loglevel=info_dict['loglevel']) MysqlObj = baseclass.interWithMysql(logger, info_dict['name']) #初始化redis对象 RedisObj = baseclass.interWithRedis(logger, info_dict['name']) # --/ # Execute commands stage # --/ try: RunCmdObj = baseclass.runCmd(logger, info_dict['name'], info_dict['iswatermark'], info_dict['isoverwrite']) VideoObj = reduceCmdOutput(info_dict, hadoopinfo_dict, logger) RunCmdObj.run(VideoObj.initReducerEnv(), QuitFlag=False) #初始化map hdfs对象 MapHdfsHost = VideoObj.HadoopNNAddr.split(":")[0] MapWebHdfs = baseclass.WebHadoopOld(MapHdfsHost, "50071", "cloudiyadatauser", logger) #初始化storage hdfs对象 StoHdfsHost = "10.1.0.8,10.1.0.10" StoWebHdfs = baseclass.WebHadoop(StoHdfsHost, 14000, "cloudiyadatauser", logger)