diff --git a/cronjob/media/vd_compress.py b/cronjob/media/vd_compress.py index 32b655a..e82b367 100644 --- a/cronjob/media/vd_compress.py +++ b/cronjob/media/vd_compress.py @@ -1,3 +1,4 @@ +import os import time from lib.all import * @@ -6,26 +7,21 @@ import cv2 import oss2 import subprocess -service_name = 'vd_compress_new' -# log_dir = '/app/log' -log_dir = '/Users/erwin/data/log' -logger = Logger(service_name, log_dir=log_dir) - -# input_file = "h264_origin.mp4" -# output_file = "h265_origin.mp4" -# # -# ff = ffmpy.FFmpeg( -# inputs={input_file: None}, -# outputs={output_file: '-c:v libx265'} -# ) -# # -# ff.run() +service_name = 'vd_compress' +log_dir = '/app/log' +# log_dir = '/Users/erwin/data/log' +logger = Logger(service_name, log_dir=log_dir, print_terminal=False) access_key_id = "LTAI5tAdu5LRvZwm4LJa21Fo" access_key_secret = "WGvSQsDralTfFAAxhEqLBOgbXqflHo" -endpoint_internal = "https://oss-cn-hangzhou.aliyuncs.com" +endpoint_internal = "https://oss-cn-hangzhou-internal.aliyuncs.com" bucket_name = "wishpal-ironfan-media" +bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint_internal, bucket_name) +url_get_videos_by_status = "https://api.tiefen.fun/op/media/get_videos_by_status" +url_update_video_compress = "https://api.tiefen.fun/op/media/update_video_compress" +hw_cdn_host = "https://filecdnhw01.tiefen.fun/" + class VdHelper: def __init__(self, vd_path): @@ -101,16 +97,22 @@ def get_video_w_h(path, p=1080): class S: def __init__(self): - self.bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint_internal, bucket_name) - self.url_get_videos_by_status = "https://api.tiefen.fun/op/media/get_videos_by_status" - self.url_update_video_compress = "https://api.tiefen.fun/op/media/update_video_compress" - self.hw_cdn_host = "https://filecdnhw01.tiefen.fun/" + # self.bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint_internal, bucket_name) + # self.url_get_videos_by_status = "https://api.tiefen.fun/op/media/get_videos_by_status" + # self.url_update_video_compress = "https://api.tiefen.fun/op/media/update_video_compress" + # self.hw_cdn_host = "https://filecdnhw01.tiefen.fun/" + self.to_del_files = list() + + def __del__(self): + for fp in self.to_del_files: + if os.path.exists(fp): + os.remove(fp) def save_video_from_oss(self, oss_src_id: str, local_path: str): - return self.bucket.get_object_to_file(oss_src_id, local_path) + return bucket.get_object_to_file(oss_src_id, local_path) def upload_video_to_oss(self, local_path: str, oss_src_id: str): - return self.bucket.put_object_from_file(oss_src_id, local_path) + return bucket.put_object_from_file(oss_src_id, local_path) def get_one_wait_compress_video(self): param = { @@ -119,7 +121,7 @@ class S: "offset": 0, "limit": 1, } - res = call_service(self.url_get_videos_by_status, param) + res = call_service(url_get_videos_by_status, param) data = safe_get_dict(res, "data") lis = safe_get_list(data, "video") if len(lis) > 0: @@ -138,7 +140,7 @@ class S: "resize_t": resize_t, } logger.Info("{}".format(dict2json(param))) - res = call_service(self.url_update_video_compress, param) + res = call_service(url_update_video_compress, param) ret = safe_get_int(res, "ret") return ret @@ -165,15 +167,17 @@ class S: elif content_type == "video/x-m4v": local_src_path_new = local_src_path + ".m4v" else: - logger.Error("invalid content_type, id: {}, src_id: {}, content_type: {}".format(video_id, self.hw_cdn_host + src_id, content_type)) + logger.Error("invalid content_type, id: {}, src_id: {}, content_type: {}".format(video_id, hw_cdn_host + src_id, content_type)) return False os.renames(local_src_path, local_src_path_new) # vd helper vdh = VdHelper(local_src_path_new) + self.to_del_files.append(local_src_path_new) # 转成264 720p mp4 output_file = local_h264_720_path + ".mp4" vdh.transfer_h264_720p_25fps(output_file) + self.to_del_files.append(output_file) # 上传 upload_ret = self.upload_video_to_oss(output_file, src_id_h264_720) @@ -185,12 +189,12 @@ class S: # 更新db db_ret = self.set_compress_finish(video_id, file_size, "", 0, src_id_h264_720, output_file_size, 100, int(time.time())) - vdh.remove() - os.remove(output_file) + # vdh.remove() + # os.remove(output_file) logger.Info("before, {}, {}, {}".format(src_id, content_type, file_size)) logger.Info("after_h264720, {}, {}, {}, {}".format(src_id_h264_720, output_content_type, output_file_size, db_ret)) - logger.Info("host: {}".format(self.hw_cdn_host + src_id_h264_720)) + logger.Info("host: {}".format(hw_cdn_host + src_id_h264_720)) return True @@ -239,29 +243,9 @@ def get_video_stat(video_path): print(f"高度: {height}") -def get_bit_rate(video_path): - import subprocess - - # 使用ffmpeg命令获取视频码率信息 - command = ["ffmpeg", "-i", video_path, "-hide_banner", "-loglevel", "error"] - output = subprocess.check_output(command, stderr=subprocess.STDOUT) - - # 解析ffmpeg输出中的码率信息 - for line in output.decode().split('\n'): - if 'bitrate' in line: - print(line) - - -# 示例用法 -# bitrate = get_video_bitrate("xvd1_src.mov") -# -# if bitrate: -# print(f"视频码率: {bitrate} kbps") - -s = S() -# s.proc_one() idx = 0 while True: + s = S() video = s.get_one_wait_compress_video() if not video: logger.Info("no video, sleep") @@ -278,14 +262,3 @@ while True: except Exception as e: logger.Error("{}, {} _Panic: {}".format(idx, video_id, str(e))) s.set_compress_finish(video_id, 0, "", 0, "", 0, -100, int(time.time())) -# get_video_stat("vdprod_7b_09_f8c4-bd63-4d4a-898b-6d1431f06994.mov") -# get_video_stat("vdprod_7b_09_f8c4-bd63-4d4a-898b-6d1431f06994.mp4") - -# get_video_stat("big_src.mov") -# # proc_test_h264("big_src.mov", "big_h264_1080.mp4") -# proc_test_h264_720("big_src.mov", "big_h264_720_0.mp4", 0) -# proc_test_h264_720("big_src.mov", "big_h264_720_23.mp4", 23) -# proc_test_h264_720("big_src.mov", "big_h264_720_30.mp4", 30) -# proc_test_h264_720("big_src.mov", "big_h264_720_40.mp4", 40) -# proc_test_h264_720("big_src.mov", "big_h264_720_50.mp4", 50) -# # get_bit_rate("xvd1_h264_720.mp4") diff --git a/lib/log.py b/lib/log.py index a3d7eb0..916815d 100644 --- a/lib/log.py +++ b/lib/log.py @@ -33,10 +33,11 @@ class ServiceLoggerHandler(logging.Handler): class Logger: - def __init__(self, service_name, log_level=logging.DEBUG, log_dir=None): + def __init__(self, service_name, log_level=logging.DEBUG, log_dir=None, print_terminal=True): self.service_name = service_name self.log_level = log_level self.log_dir = log_dir + self.print_terminal = print_terminal logging.addLevelName(10, 'DEBG') logging.addLevelName(20, 'INFO') @@ -60,11 +61,13 @@ class Logger: formatter = logging.Formatter( '[%(asctime)s] [%(levelname).4s] (%(name)s, %(call_fname)s:%(call_line)d) %(message)s') fh.setFormatter(formatter) - ch.setFormatter(formatter) + if print_terminal: + ch.setFormatter(formatter) # 给logger添加handler self.logger.addHandler(fh) - self.logger.addHandler(ch) + if print_terminal: + self.logger.addHandler(ch) def Debug(self, msg): previous_frame = inspect.currentframe().f_back diff --git a/ttt/x.py b/ttt/x.py new file mode 100644 index 0000000..8b3e93c --- /dev/null +++ b/ttt/x.py @@ -0,0 +1,9 @@ +from lib.all import * + +st = get_today_zero_time() - 86400 * 7 +et = st + 86400 + +print( + get_time_str_by_ts(st), + get_time_str_by_ts(et) +)