def compress_media(media_db: Media): if MediaHelp.is_err(media_db.state): return if media_db.desc_mpath == None: with lock: media_db.desc_mpath = MediaPath.pdc().search_by_abs_path( MediaPath.desc(), is_src=False) pass d_abs_path = desc_path( media_db) # ypath.join(media_db.desc_mpath.path, media_db.desc_path) # 如果开关开着. 则不管desc是否已有.,根据数据库去覆盖. if media_db.state < MediaHelp.STATE_COMPRESS_HLS: # 标记为 未转码完毕 if os.path.exists(d_abs_path): if Globals.MEDIA_SERVICE_COVER_DESC: os.remove(d_abs_path) else: logger.info('MediaService.target exists 所以直接修改数据') modify_state(media_db, MediaHelp.STATE_COMPRESS_HLS) else: if not os.path.exists(d_abs_path): # 状态是转码完毕后. 但是desc文件不存在. 则需要重新转码 modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) if media_db.state >= MediaHelp.STATE_COMPRESS_HLS: logger.info('该文件已经转码过了:' + media_db.abs_path) return if os.path.exists(d_abs_path): os.remove(d_abs_path) ypath.create_dirs(d_abs_path) # media_db.nginx_path = target if media_db.codec_type == 'h264': logger.info('这个视频是 h264流视频, 可以直接复制' + media_db.abs_path) can_audio_copy = (media_db.audio_name == 'aac' or media_db.audio_name == 'mp3') if not can_audio_copy: EmailUtil.send('该文件需要检查audio:' + media_db.abs_path + ' \nmedia_db.audio_name:' + media_db.audio_name) # input('该文件需要检查audio:' + media_db.abs_path) if media_db.abs_path.endswith('.mp4') and can_audio_copy: os.symlink(media_db.abs_path, d_abs_path) else: # 这里进行复制内容 audio_cmd = '-acodec copy' if can_audio_copy else '-acodec mp3' # audio_cmd = '-acodec copy' yutils.process_cmd(ffmpeg_tools + ' -i \"' + media_db.abs_path + '\" -vcodec copy ' + audio_cmd + ' \"' + d_abs_path + '\"') else: # '\"%s\" -i \"%s\" \"%s\"' % (ffmpeg_tools, src_path, target) logger.info('这个视频不是:' + media_db.abs_path) yutils.process_cmd('\"%s\" -i \"%s\" \"%s\"' % (ffmpeg_tools, media_db.abs_path, d_abs_path)) if not os.path.exists(d_abs_path): logger.error('源文件错误:%s' % d_abs_path) modify_state(media_db, MediaHelp.STATE_SRC_ERROR) else: modify_state(media_db, MediaHelp.STATE_COMPRESS_HLS)
def create_thum(media_db: Media): if MediaHelp.is_err(media_db.state): return desc_root = media_db.desc_mpath.path media_tum_root = ypath.join( desc_root, movie_config.img_info.img_root ) # TmpUtil.desc() / movie_config.img_info.img_root # convert_root # if media_db.state >= MediaHelp.STATE_VIDEO_THUM: # logger.info('该文件已经转缩略图过了:' + media_db.abs_path) # return target_img_dir = ypath.join(media_tum_root, media_db.desc_path) ypath.create_dirs(target_img_dir) desc = ypath.join(target_img_dir, movie_config.img_info.img) desc_thum = ypath.join(target_img_dir, movie_config.img_info.thum) if os.path.exists(desc) and os.path.exists(desc_thum): logger.info('该视频不需要做缩略图裁切,因为已有:%s' % desc) return # 裁切缩略图的比例 thum_percent = int(movie_config.base_info.thum_w) / int( movie_config.base_info.thum_h) max_thum_time = int(movie_config.base_info.max_thum_time) min_thum_time = int(movie_config.base_info.min_thum_time) ypath.create_dirs(desc) r_time = random.randint( min_thum_time if media_db.duration > min_thum_time else 0, max_thum_time if media_db.duration > max_thum_time else media_db.duration) d_abs_path = ypath.join(media_root(desc_root), media_db.desc_path) cmd = ffmpeg_tools + ' -i \"' + d_abs_path + '\" -y -vframes 1 -ss 00:00:' + str( r_time) + ' -f image2 \"' + desc + '\"' yutils.process_cmd(cmd) if not os.path.exists(desc): return img = Image.open(desc) w, h = img.size crop_img = img.crop(yutils.crop_size(w, h, thum_percent)) crop_img.save(desc_thum)
def compress_media(media_db): if MediaHelp.is_err(media_db.state): return # 如果开关开着. 则不管desc是否已有.,根据数据库去覆盖. if media_db.state < MediaHelp.STATE_VIDOE_COMPRESS_FINISH: # 标记为 未转码完毕 if os.path.exists(media_db.desc_path): if Globals.MEDIA_SERVICE_COVER_DESC: os.remove(media_db.desc_path) else: logger.info('MediaService.target exists 所以直接修改数据') modify_state(media_db, MediaHelp.STATE_VIDOE_COMPRESS_FINISH) else: if not os.path.exists(media_db.desc_path): # 状态是转码完毕后. 但是desc文件不存在. 则需要重新转码 modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) if media_db.state >= MediaHelp.STATE_VIDOE_COMPRESS_FINISH: logger.info('该文件已经转码过了:' + media_db.abs_path) return if os.path.exists(media_db.desc_path): os.remove(media_db.desc_path) ypath.create_dirs(media_db.desc_path) # media_db.nginx_path = target if media_db.codec_type == 'h264': logger.info('这个视频是 h264流视频, 可以直接复制' + media_db.abs_path) if media_db.abs_path.endswith('.mp4'): os.symlink(media_db.abs_path, media_db.desc_path) else: # 这里进行复制内容 yutils.process_cmd( ffmpeg_tools + ' -i \"' + media_db.abs_path + '\" -vcodec copy -acodec copy \"' + media_db.desc_path + '\"') else: # '\"%s\" -i \"%s\" \"%s\"' % (ffmpeg_tools, src_path, target) logger.info('这个视频不是:' + media_db.abs_path) yutils.process_cmd('\"%s\" -i \"%s\" \"%s\"' % (ffmpeg_tools, media_db.abs_path, media_db.desc_path)) if not os.path.exists(media_db.desc_path): logger.error('源文件错误:%s' % media_db.desc_path) modify_state(media_db, MediaHelp.STATE_SRC_ERROR) else: modify_state(media_db, MediaHelp.STATE_VIDOE_COMPRESS_FINISH)
def sync_to_os(bat): with open(bat, 'r') as f: cmd_line = f.readline() cmd_splite = cmd_line.split() region_index = cmd_splite.index('-r') + 1 region = cmd_splite[region_index] secret_id_index = cmd_splite.index('-a') + 1 secret_id = cmd_splite[secret_id_index] secret_key_index = cmd_splite.index('-s') + 1 secret_key = cmd_splite[secret_key_index] bucket_index = cmd_splite.index('-b') + 1 bucket = cmd_splite[bucket_index] config2 = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=None, Scheme='https') client = CosS3Client(config2) with open(upload_list_path, 'r') as f: can_upload = len(f.readlines()) > 0 if can_upload and os.path.exists(upload_list_path): yutils.process_cmd(bat, done_call=sync_path) else: print('没有要上传的文件!') if os.path.exists(delete_list_path): # 执行批量删除 delete_list_obj = [] with open(delete_list_path, 'r', encoding='utf-8') as f: delete_list = f.readlines() for line in delete_list: delete_list_obj.append({'Key': line.split('|')[0]}) print(delete_list_obj) if len(delete_list_obj) > 0: client.delete_objects(Bucket=bucket, Delete={'Object': delete_list_obj}) else: print('没有要删除的文件!')
def create_ts(media_db: Media): # d_abs_path = ypath.join(media_root(media_db), media_db.desc_path) desc_root = media_db.desc_mpath.path media_ts_dir = ypath.join(desc_root, movie_config.ts_info.ts_dir, media_db.desc_path) if media_db.state >= MediaHelp.STATE_VIDEO_TS and os.path.isdir( media_ts_dir): logger.info('该视频已经切片过了:' + media_db.abs_path) return if os.path.exists(media_ts_dir): shutil.rmtree(media_ts_dir) ypath.create_dirs(media_ts_dir, True) media_desc_path = desc_path( media_db) # ypath.join(desc_path(media_db), media_db.desc_path) m3u8_file = ypath.join(media_ts_dir, movie_config.ts_info.u8name) cmd = '\"' + ffmpeg_tools + '\" -i \"' + media_desc_path + \ '\" -codec copy -vbsf h264_mp4toannexb -map 0 -f segment -segment_list \"' + \ m3u8_file + '\" -segment_time 30 \"' + media_ts_dir + '/%05d.ts\"' yutils.process_cmd(cmd) modify_state(media_db, MediaHelp.STATE_VIDEO_TS) # m3u8_path = ypath.join(media_ts_dir, movie_config.ts_info.u8name) # media_db.m3u8_path = ypath.join(media_db.desc_path, movie_config.ts_info.u8name) print(desc_root)
def movie_info_res(cmdlist, _): if len(cmdlist) <= 0: modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) return jsonbean = json.loads(''.join(cmdlist)) if 'streams' not in jsonbean.keys(): modify_state(media_db, MediaHelp.STATE_SRC_ERROR) return streamlist = jsonbean['streams'] format = jsonbean['format'] media_db.md5 = yutils.get_md5(media_db.abs_path) media_db.duration = int(float(format['duration'])) # cur_file_info['duration'] = media_db.size = int(format['size']) if len(streamlist) <= 0: modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) return audio_streams = [] decode_map = '' for stream_item in streamlist: if stream_item['codec_type'] == 'audio': logger.info(str(stream_item)) audio_streams.append(stream_item) else: if stream_item['codec_type'] == 'video': # 如果是视频.保存视频信息 media_db.codec_type = stream_item['codec_name'] media_db.codec_long_name = stream_item['codec_long_name'] media_db.width = int(stream_item['width']) media_db.height = int(stream_item['height']) try: media_db.r_frame_rate = round( eval(stream_item['r_frame_rate'])) media_db.avg_frame_rate = round( eval(stream_item['avg_frame_rate'])) except ZeroDivisionError: media_db.r_frame_rate = 0 media_db.avg_frame_rate = 0 decode_map += ' -map 0:' + str(stream_item['index']) # 有多个语种 digout = False if len(audio_streams) > 1: for audio_stream in audio_streams: if 'tags' in audio_stream and 'title' in audio_stream['tags'] \ and ('国语' == audio_stream['tags']['title'] or '中语' == audio_stream['tags']['title']): decode_map += ' -map 0:' + str(audio_stream['index']) media_db.audio_long_name = audio_stream['codec_long_name'] media_db.audio_name = audio_stream['codec_name'] digout = True break else: if len(audio_streams) == 1: media_db.audio_long_name = audio_streams[0]['codec_long_name'] media_db.audio_name = audio_streams[0]['codec_name'] logger.info('该视频音轨只有一个,不需要转换:' + media_db.abs_path) modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) return if not digout: if len(audio_streams) >= 1: media_db.audio_long_name = audio_streams[0]['codec_long_name'] media_db.audio_name = audio_streams[0]['codec_name'] # media_db.audio_long_name = stream_item['codec_long_name'] # media_db.audio_name = stream_item['codec_name'] for audio_stream in audio_streams: decode_map += ' -map 0:' + str(audio_stream['index']) # out_content += str(index) + ':' + str(audio_stream) + '\n' # index += 1 # select_audio = len(audio_streams) # while len(audio_streams) <= select_audio or select_audio < 0: # select_audio = int(input(out_content + '选择音轨:')) with lock: mulit_audio_path = ypath.join(MediaPath.src(), mulit_audio_dir) desc_mulit_path = ypath.decompose_path(media_db.abs_path, src_db.path, str(mulit_audio_path)) out_file = desc_mulit_path + '.chi' + ypath.file_exten( media_db.abs_path) ypath.create_dirs(desc_mulit_path) if os.path.exists(out_file): os.remove(out_file) logger.info(out_file) copy_cmd = ffmpeg_tools + ' -i \"' + media_db.abs_path + '\"' + decode_map + ' -vcodec copy -acodec copy \"' + out_file + '\"' yutils.process_cmd(copy_cmd, done_call=rm_on_audio_copy, param=(media_db.abs_path, out_file, desc_mulit_path))
def analysis_audio_info(media_db): def movie_info_res(cmdlist, _): if len(cmdlist) <= 0: modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) return jsonbean = json.loads(''.join(cmdlist)) if 'streams' not in jsonbean.keys(): modify_state(media_db, MediaHelp.STATE_SRC_ERROR) return streamlist = jsonbean['streams'] format = jsonbean['format'] media_db.md5 = yutils.get_md5(media_db.abs_path) media_db.duration = int(float(format['duration'])) # cur_file_info['duration'] = media_db.size = int(format['size']) if len(streamlist) <= 0: modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) return audio_streams = [] decode_map = '' for stream_item in streamlist: if stream_item['codec_type'] == 'audio': logger.info(str(stream_item)) audio_streams.append(stream_item) else: if stream_item['codec_type'] == 'video': # 如果是视频.保存视频信息 media_db.codec_type = stream_item['codec_name'] media_db.codec_long_name = stream_item['codec_long_name'] media_db.width = int(stream_item['width']) media_db.height = int(stream_item['height']) try: media_db.r_frame_rate = round(eval(stream_item['r_frame_rate'])) media_db.avg_frame_rate = round(eval(stream_item['avg_frame_rate'])) except ZeroDivisionError: media_db.r_frame_rate = 0 media_db.avg_frame_rate = 0 decode_map += ' -map 0:' + str(stream_item['index']) # 有多个语种 digout = False if len(audio_streams) > 1: for audio_stream in audio_streams: if 'tags' in audio_stream and 'title' in audio_stream['tags'] \ and ('国语' == audio_stream['tags']['title'] or '中语' == audio_stream['tags']['title']): decode_map += ' -map 0:' + str(audio_stream['index']) digout = True break else: logger.info('该视频音轨只有一个,不需要转换:' + media_db.abs_path) modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) return if not digout: for audio_stream in audio_streams: decode_map += ' -map 0:' + str(audio_stream['index']) # out_content += str(index) + ':' + str(audio_stream) + '\n' # index += 1 # select_audio = len(audio_streams) # while len(audio_streams) <= select_audio or select_audio < 0: # select_audio = int(input(out_content + '选择音轨:')) # desc_file = file + mulit_audio_path = TmpUtil.src() / movie_config.base_info.mulit_audio_dir desc_mulit_path = ypath.decompose_path(media_db.abs_path, str(media_src_root), str(mulit_audio_path)) out_file = desc_mulit_path + '.chi' + ypath.file_exten(media_db.abs_path) ypath.create_dirs(desc_mulit_path) if os.path.exists(out_file): os.remove(out_file) logger.info(out_file) copy_cmd = ffmpeg_tools + ' -i \"' + media_db.abs_path + '\"' + decode_map + ' -vcodec copy -acodec copy \"' + out_file + '\"' yutils.process_cmd(copy_cmd, done_call=rm_on_audio_copy, param=(media_db.abs_path, out_file, desc_mulit_path)) if MediaHelp.is_err(media_db.state): return # 音轨结束后,保存源文件到movie_otherformat目录,替换原有文件名. def rm_on_audio_copy(_, files): shutil.move(files[0], files[2]) shutil.move(files[1], files[0]) modify_state(media_db, MediaHelp.STATE_AUDIO_FINISH) if media_db.state >= MediaHelp.STATE_AUDIO_FINISH: logger.info('该文件的音轨已经转换过了:' + media_db.abs_path) return logger.info('开始转换:' + media_db.abs_path) yutils.process_cmd( ffprobe_tools + ' \"' + media_db.abs_path + '\" -print_format json -show_format -show_streams', done_call=movie_info_res)
def download_oncos(): yutils.process_cmd('coscmd list -ar ' + bucket_dir, done_call=download)
def sync_path(_, param): # 上传列表需要优化.一两张不需要处理成这样吧? 2000+文件上传需要几分钟 1w? 10分钟? yutils.process_cmd('coscmd upload -rs %s %s' % (local_path, bucket_dir)) pass
def create_diff_list(bat): if not yutils.is_win(): with open(bat, 'r') as f: yutils.process_cmd(f.readline(), done_call=print_diff_list) else: yutils.process_cmd(bat, done_call=print_diff_list)
def print_diff_list(res, param): logger.info('bucket suc! do next:list os files') yutils.process_cmd('coscmd list -ar ' + bucket_dir, done_call=list_finish)