def clear_timeout_sharding(): # LOGGER.debug('clear timeout sharding') def file_path(file): # 文件路径 return sharding_upload_path + '/' + file sharding_upload_path = env.get("APP_UPLOAD_ROOT_PATH") clear_timeout = env.get('CLEAR_SHARDING') files = os.popen("ls -l %s | awk '{print $9}'" % (sharding_upload_path, )) for file in files: sharding_file_path = file_path(file.strip()) # LOGGER.debug('file %s' % (sharding_file_path, )) if file == '\n' or '.' in file: continue t = os.path.getctime(sharding_file_path) if t + int(clear_timeout) < time.time(): try: os.remove(sharding_file_path) LOGGER.debug('remove file %s' % (sharding_file_path, )) except Exception as e: raise e
def app_upload_accept_view(): # type: () -> ignore request_id = str(uuid.uuid4()).replace('-', '') upload_file = request.files['file'] fileMd5 = request.form.get('fileMd5') # 由前端提供,文件名加文件大小取的md5值 chunk = request.form.get('chunk', 0) filename = '%s%s' % (fileMd5, chunk) upload_file.save('%s/%s' % (env.get('APP_UPLOAD_ROOT_PATH'), filename)) result_body = {"_id": request_id, "error_code": 0, "error_info": ""} response = make_response(jsonify(result_body)) # type: ignore response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers['Access-Control-Allow-Headers'] = 'x-requested-with' return response
class StorageAsyncFactory(object): __proxy = None APP_STORAGE_TYPE = env.get('APP_STORAGE_TYPE', 'LOCAL').upper() # type: str @classmethod def make(cls): # type: () -> StorageAsyncInterface if cls.__proxy is None: if cls.APP_STORAGE_TYPE == 'LOCAL': cls.__proxy = StorageLocalAsyncImplement.start().proxy() elif cls.APP_STORAGE_TYPE == 'NAS': cls.__proxy = StorageNASAsyncImplement.start().proxy() else: raise ValueError('unknow app storage type: %s' % (cls.APP_STORAGE_TYPE, )) assert isinstance(cls.__proxy, ActorProxy) return cls.__proxy # type: ignore
def app_upload_accept_check_view(): # type: () -> ignore request_id = str(uuid.uuid4()).replace('-', '') upload_path = env.get("APP_UPLOAD_ROOT_PATH") fileMd5 = request.form.get('fileMd5') chunk = request.form.get('chunk', 0) file_slice_name = "%s%s" % (fileMd5, chunk) file_slice_path = upload_path + '/' + file_slice_name if os.path.exists(file_slice_path): result_body = {"_id": request_id, "error_code": 1, "error_info": "exists"} else: result_body = {"_id": request_id, "error_code": 0, "error_info": ""} response = make_response(jsonify(result_body)) # type: ignore response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers['Access-Control-Allow-Headers'] = 'x-requested-with' return response
def app_vod_view(filename): # type: (str) -> ignore r = re.match(r'.*?:(\d+)/', request.url_root) # XXX: 不能假定端口是减一的 port = int(r.group(1)) if r else 80 re_url = str(request.url).replace(":%d/" % port, ":%d/" % (port - 1)) # type: str vod_path = env.get('APP_VOD_ROOT_PATH') if filename[-5:] == '.m3u8': file_id = filename[:-5] if not storage.is_vod_dir_exists(file_id).get(timeout=60): abort(404) storage.get_vod_dir(file_id, vod_path + '/' + file_id).get(timeout=600) re_url = re_url.replace(filename, '%s/index.m3u8' % (file_id, )) elif filename[-4:] == '.flv': file_id = filename[:-4] if not storage.is_vod_file_exists(filename).get(timeout=60): abort(404) storage.get_vod_file(filename, vod_path + '/' + filename).get(timeout=600) return redirect(re_url)
def APP_WEB_SERVER_PUBLIC_IP(self): # type: () -> str return cast(str, env.get('APP_WEB_SERVER_PUBLIC_IP'))
def APP_VOD_ROOT_PATH(self): # type: () -> str return cast(str, env.get('APP_VOD_ROOT_PATH'))
def process_cut_vod_file(self): # type: () -> Dict[str, Any] def callback_ok(): # type: () -> None LOGGER.debug('[%s] %s task_id %s callback_ok' % (self.meeting_id, self.request_id, task_id)) body = { "error_code": 0, 'error_info': '', 'task_id': task_id, "file_id": file_id, "file_type": file_type, 'source_fileid': source_fileid, "source_url": source_url, "addition": addition, 'file_name': file_name, } data = { '_id': self.request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_cut_vod_file_done', } data.update(body) vod_manager = VodManagerFactory.make( self.meeting_id, file_id=cut_args['file_id']) data.update(vod_manager.vod_info) LOGGER.debug('cut info is %s' % (data, )) requests.post(self.BUSINESS_APP_SERVER_URL, json=data, timeout=60) def callback_error(): # type: () -> None LOGGER.debug('[%s] %s task_id%s callback_error' % (self.meeting_id, self.request_id, task_id)) body = { 'error_code': -1, 'error_info': 'UNKOWN_ERROR', 'task_id': task_id, "file_id": file_id, "file_type": file_type, "addition": addition, 'file_name': file_name, } data = { '_id': self.request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_cut_vod_file_done', } data.update(body) requests.post(self.BUSINESS_APP_SERVER_URL, json=data, timeout=60) APP_VOD_PALY_URL = env.get('APP_VOD_PALY_URL') file_id = self.body.get('file_id', '') if not file_id: abort(404) file_type = self.body.get('file_type', '') source_fileid = file_id source_url = '%s/%s/index.%s' % (APP_VOD_PALY_URL, file_id, file_type) filename = file_id + '.' + file_type vod_path = env.get('APP_VOD_ROOT_PATH') task_id = self.body.get('task_id') if not task_id: task_id = str(uuid.uuid4()).replace('-', '') cut_args = self.body.get('cut_args', '{}') file_name = cut_args.get('file_name') LOGGER.debug('file_name is %s, cut_args is %s' % (file_name, cut_args)) # 接收附加参数addition addition = self.body.get('addition') if 'file_id' not in cut_args: cut_args['file_id'] = str(uuid.uuid4()).replace('-', '') cutter.submit_task(task_id, file_id, file_type, filename, vod_path, cut_args, callback_ok, callback_error) result_body = { '_id': self.request_id, 'task_id': task_id, "error_code": 0, "error_info": "", } return result_body
def process_merge_vod_file(self): # type: () -> Dict[str, Any] def callback_ok(): # type: () -> None LOGGER.debug('[%s] %s task_id %s callback_ok' % (self.meeting_id, self.request_id, task_id)) body = { "error_code": 0, 'error_info': '', 'task_id': task_id, "file_id": merge_args['file_id'], "file_type": merge_args['file_type'], "addition": addition, } data = { '_id': self.request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_merge_vod_file_done', } data.update(body) vod_manager = VodManagerFactory.make( self.meeting_id, file_id=merge_args['file_id']) data.update(vod_manager.vod_info) LOGGER.debug('Merge info is %s' % (data,)) requests.post(self.BUSINESS_APP_SERVER_URL, json=data, timeout=60) def callback_error(): # type: () -> None LOGGER.debug('[%s] %s task_id%s callback_error' % (self.meeting_id, self.request_id, task_id)) body = { 'error_code': -1, 'error_info': 'UNKOWN_ERROR', 'task_id': task_id, "addition": addition, } data = { '_id': self.request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_merge_vod_file_done', } data.update(body) requests.post(self.BUSINESS_APP_SERVER_URL, json=data, timeout=60) vod_path = env.get('APP_VOD_ROOT_PATH') # 接收附加参数addition addition = self.body.get('addition', {}) task_id = self.body.get('task_id') if not task_id: task_id = str(uuid.uuid4()).replace('-', '') merge_args = self.body.get('merge_args', {}) merge_files = self.body.get('merge_files', {}) if 'file_id' not in merge_args: merge_args['file_id'] = str(uuid.uuid4()).replace('-', '') merger.submit_task(task_id, vod_path, merge_args, merge_files, callback_ok, callback_error) result_body = { '_id': self.request_id, 'task_id': task_id, "error_code": 0, "error_info": "", } # result_body.update({'file_id': file_id, 'file_type': file_type}) result_body.update({'file_id': merge_args['file_id'], 'file_type': merge_args['file_type']}) return result_body
def APP_WEB_SERVER_NGINX_RTMP_PORT(self): # type: () -> int return cast(int, env.get('APP_WEB_SERVER_NGINX_RTMP_PORT', type_=int))
def process_transcode_upload_file(self): # type: () -> Dict[str, Any] def callback_ok(): # type: () -> None LOGGER.debug('[%s] %s task_id %s callback_ok' % (self.meeting_id, self.request_id, task_id)) body = { "error_code": 0, 'error_info': '', 'task_id': task_id, "file_id": file_id, "file_type": file_type, } data = { '_id': self.request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_transcode_file_done', } data.update(body) vod_manager = VodManagerFactory.make( self.meeting_id, file_id=transcode_args['file_id']) data.update(vod_manager.vod_info) LOGGER.debug('Transcode info is %s' % (data,)) requests.post(self.BUSINESS_APP_SERVER_URL, json=data, timeout=60) def callback_error(): # type: () -> None LOGGER.debug('[%s] %s task_id%s callback_error' % (self.meeting_id, self.request_id, task_id)) body = { 'error_code': -1, 'error_info': 'UNKOWN_ERROR', 'task_id': task_id, "file_id": file_id, "file_type": file_type, } data = { '_id': self.request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_transcode_file_done', } data.update(body) requests.post(self.BUSINESS_APP_SERVER_URL, json=data, timeout=60) file_id = self.body.get('file_id', '') file_type = self.body.get('file_type', '') if not file_id: abort(404) filename = file_id + '.' + file_type upload_path = env.get('APP_UPLOAD_ROOT_PATH') vod_path = env.get('APP_VOD_ROOT_PATH') task_id = self.body.get('task_id') if not task_id: task_id = str(uuid.uuid4()).replace('-', '') transcode_args_str = self.body.get('transcode_args', '{}') transcode_args = json.loads(transcode_args_str) # type: Dict[str, Any] if 'file_id' not in transcode_args: transcode_args['file_id'] = str(uuid.uuid4()).replace('-', '') transcoder.submit_task(task_id, file_id, file_type, filename, upload_path, vod_path, transcode_args, callback_ok, callback_error) result_body = { '_id': self.request_id, 'task_id': task_id, "error_code": 0, "error_info": "", } return result_body
def auto_transcode(file_id, task_id, file_type, origin_filename,upload_file_path, compid, userid, upload_task_id, request_id, meeting_id): def callback_ok(): # type: () -> None upload_filename = file_id + '.' + file_type relative_upload_file_path = '/' + 'upload' + "/" + upload_filename # 相对路径,返回给后台的数据 body = { "error_code": 0, 'error_info': '', 'task_id': task_id, "file_id": file_id, "file_type": 'm3u8', "file_name": origin_filename, "base_url": relative_upload_file_path, # "source_type": source_type, "compid": compid, "userid": userid, 'addition': {'upload_task_id': upload_task_id} } data = { '_id': request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_transcode_file_done', } data.update(body) vod_manager = VodManagerFactory.make( meeting_id, file_id=transcode_args['file_id']) data.update(vod_manager.vod_info) LOGGER.debug('AutoTranscode callback_ok data:%s' % (data, )) requests.post(BUSINESS_APP_SERVER_URL, json=data, timeout=60) def callback_error(): # type: () -> None body = { 'error_code': -1, 'error_info': 'UNKOWN_ERROR', 'task_id': task_id, "file_id": file_id, "file_type": file_type, # "source_type": source_type, "file_name": origin_filename, "compid": compid, "userid": userid, 'addition': {'upload_task_id': upload_task_id} } data = { '_id': request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_transcode_file_done', } data.update(body) LOGGER.debug('callback_error data:%s' % (data, )) requests.post(BUSINESS_APP_SERVER_URL, json=data, timeout=60) transcode_args = { "template_type": 1, "s": "1280x720", "r": 24, "crf": 34, "file_id": file_id, "file_type": "m3u8", } filename = file_id + '.' + file_type BUSINESS_APP_SERVER_URL = env.get('BUSINESS_APP_SERVER_URL') # type: str upload_path = env.get('APP_UPLOAD_ROOT_PATH') vod_path = env.get('APP_VOD_ROOT_PATH') LOGGER.debug('Transcoding, origin_filename is %s, file_id is %s, \ file_type is %s, upload_file_path is %s' % ( origin_filename, file_id, file_type, upload_file_path)) while True: if os.path.exists(upload_file_path): # 判断是否存在,若存在说明合并完成,可进行转码 break time.sleep(0.2) if not task_id: task_id = str(uuid.uuid4()).replace('-', '') if 'file_id' not in transcode_args: transcode_args['file_id'] = str(uuid.uuid4()).replace('-', '') transcoder.submit_task(task_id, file_id, file_type, filename, upload_path, vod_path, transcode_args, callback_ok, callback_error)
def BUSINESS_APP_SERVER_URL(self): # type: () -> str return cast(str, env.get('BUSINESS_APP_SERVER_URL'))
def app_upload_complete_view(): # type: () -> ignore BUSINESS_APP_SERVER_URL = env.get('BUSINESS_APP_SERVER_URL') # type: str request_id = str(uuid.uuid4()).replace('-', '') def callback_ok(): # type: () -> None LOGGER.debug('%s task_id %s callback_ok' % (request_id, upload_task_id)) body = { "error_code": 0, 'error_info': '', 'task_id': upload_task_id, "file_id": file_id, "file_type": file_type, "file_name": origin_filename, "compid": compid, "userid": userid, } data = { '_id': request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_update_file_done', } data.update(body) LOGGER.debug('complete data is %s' % (data, )) requests.post(BUSINESS_APP_SERVER_URL, json=data, timeout=60) def callback_error(): # type: () -> None LOGGER.debug('%s task_id %s callback_error' % (request_id, upload_task_id)) body = { 'error_code': -1, 'error_info': 'UNKOWN_ERROR', 'task_id': upload_task_id, "file_id": file_id, "file_type": file_type, 'compid': str(compid), } data = { '_id': request_id, 't': int(float(time.time())), 'sign_type': 'md5', 'sign': '00000000000000000000000000000000', 'nonce': '123456', 'event_type': 'notify_update_file_done', } data.update(body) requests.post(BUSINESS_APP_SERVER_URL, json=data, timeout=60) # origin_filename = request.args.get('file_id').strip() origin_filename = request.args.get('filename') file_id = str(uuid.uuid4()).replace('-', '') if not origin_filename: origin_filename = file_id file_type = request.args.get('file_type') compid = request.args.get('compid') userid = request.args.get('userid') target_filename = file_id + '.' + file_type upload_task_id = request.args.get('task_id') upload_path = env.get('APP_UPLOAD_ROOT_PATH') target_file_path = '%s/%s' % (upload_path, target_filename) istranscode = request.args.get('auto_transcode') meeting_id = request.args.get('meeting_id') upload_file_completer.submit_task(upload_task_id, target_filename, target_file_path, upload_path, callback_ok, callback_error) upload_file_path = upload_path + '/' + target_filename task_id = request.args.get('task_id') if istranscode > 0: logging.debug('''auto_transcode(file_id, task_id, file_type, origin_filename,upload_file_path, compid, userid, upload_task_id, request_id, meeting_id)''') auto_transcode(file_id, task_id, file_type, origin_filename,upload_file_path, compid, userid, upload_task_id, request_id, meeting_id) result_body = {'_id': request_id, "error_code": 0, "error_info": ""} result_body.update({'file_id': file_id, 'file_type': file_type}) response = make_response(jsonify(result_body)) # type: ignore response.headers['Access-Control-Allow-Origin'] = '*' response.headers['Access-Control-Allow-Methods'] = 'OPTIONS,HEAD,GET,POST' response.headers['Access-Control-Allow-Headers'] = 'x-requested-with' return response
def FFMPEG_CMD_PATH(self): # type: () -> str return cast(str, env.get('FFMPEG_CMD_PATH'))
def APP_WEB_SERVER_PUBLIC_PORT(self): # type: () -> int return cast(int, env.get('APP_WEB_SERVER_PUBLIC_PORT', type_=int))
def APP_STORAGE_NAS_ROOT_PATH(self): # type: () -> str r = env.get('APP_STORAGE_NAS_ROOT_PATH') # type: str return r
def APP_UPLOAD_ROOT_PATH(self): # type: () -> str r = env.get('APP_UPLOAD_ROOT_PATH') # type: str return r
def APP_UPLOAD_ROOT_PATH(self): # type: () -> str return cast(str, env.get("APP_UPLOAD_ROOT_PATH"))
def APP_VOD_PALY_URL(self): # type: () -> str return cast(str, env.get('APP_VOD_PALY_URL'))
def APP_LIVE_NEED_AUTH_PUBLISH(self): # type: () -> bool r = env.get('APP_LIVE_NEED_AUTH_PUBLISH', default=False, type_=bool) return cast(bool, r)
def APP_RECORD_IS_RECODE_FLV(self): # type: () -> str return cast(str, env.get('APP_RECORD_IS_RECODE_FLV'))
def BUSINESS_APP_NEED_AUTH_PUBLISH(self): # type: () -> bool r = env.get( 'BUSINESS_APP_NEED_AUTH_PUBLISH', default=False, type_=bool) return cast(bool, r)
def APP_WEB_SERVER_NGINX_RTMP_IP(self): # type: () -> str return cast(str, env.get('APP_WEB_SERVER_NGINX_RTMP_IP'))