This commit is contained in:
lwl0608 2024-09-14 17:17:59 +08:00
parent c2f1761ea9
commit 670336981c
3 changed files with 93 additions and 7 deletions

47
cronjob/media/monitor.py Normal file
View File

@ -0,0 +1,47 @@
from lib.all import *
from dingtalkchatbot.chatbot import DingtalkChatbot
dingding_webhook = "https://oapi.dingtalk.com/robot/send?access_token=20a1c312d1e5faeb97273cc29b42d817204d76922fba46d2552cb2945b5a4502"
dingding = DingtalkChatbot(dingding_webhook)
def send_msg(content, is_at_all=False):
return dingding.send_text(msg=content, is_at_all=is_at_all)
class S:
def __init__(self):
self.col_video = MongoDB(
host="mongodb://root:Wishpal2024@dds-bp1da1ddd62bede41.mongodb.rds.aliyuncs.com:3717,dds-bp1da1ddd62bede42.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-77304659",
port=3717,
db="media",
collection="video"
)
def __del__(self):
self.col_video.close()
def vd_compress_monitor(self):
query = {
"status": 0
}
cnt = self.col_video.count(query)
if cnt > 20:
content = '''{}
视频转码积压数: {}
'''.format(now_str(), cnt)
ret = send_msg(content, True)
else:
content = '''{}
视频转码积压数: {}
'''.format(now_str(), cnt)
ret = send_msg(content)
print(ret)
s = S()
s.vd_compress_monitor()

View File

@ -7,18 +7,19 @@ import oss2
import subprocess import subprocess
service_name = 'vd_compress' service_name = 'vd_compress'
log_dir = '/app/log' # log_dir = '/app/log'
# log_dir = '/Users/erwin/data/log' log_dir = '/Users/erwin/data/log'
logger = Logger(service_name, log_dir=log_dir, print_terminal=False) logger = Logger(service_name, log_dir=log_dir, print_terminal=False)
access_key_id = "LTAI5tAdu5LRvZwm4LJa21Fo" access_key_id = "LTAI5tAdu5LRvZwm4LJa21Fo"
access_key_secret = "WGvSQsDralTfFAAxhEqLBOgbXqflHo" access_key_secret = "WGvSQsDralTfFAAxhEqLBOgbXqflHo"
endpoint_internal = "https://oss-cn-hangzhou-internal.aliyuncs.com" endpoint_internal = "https://oss-cn-hangzhou.aliyuncs.com"
bucket_name = "wishpal-ironfan-media" bucket_name = "wishpal-ironfan-media"
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint_internal, bucket_name) 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_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" url_update_video_compress = "https://api.tiefen.fun/op/media/update_video_compress"
url_update_video_pf = "https://api.tiefen.fun/op/media/update_video_pf"
cdn_host = "https://filecdn01.tiefen.fun/" cdn_host = "https://filecdn01.tiefen.fun/"
acs_client = AcsClient(access_key_id, access_key_secret, 'cn-hangzhou') acs_client = AcsClient(access_key_id, access_key_secret, 'cn-hangzhou')
@ -37,6 +38,37 @@ def refresh_cdn_cache(url):
return response return response
def update_pixel_format(vid, pf, pf720):
param = {
"id": vid,
"pixel_format": pf,
"pixel_format_720": pf720
}
res = call_service(url_update_video_pf, param)
ret = safe_get_int(res, "ret")
return ret
def get_pixel_format(vd_path):
command = [
'ffprobe', '-v', 'quiet',
'-show_streams', '-select_streams', 'v:0',
'-print_format', 'json', vd_path
]
result = subprocess.run(command, capture_output=True, text=True)
output = result.stdout
data = json.loads(output)
streams = data.get('streams', [])
if streams:
pix_fmt = streams[0].get('pix_fmt', 'Unknown')
return pix_fmt
else:
return 'No video stream found'
class VdHelper: class VdHelper:
def __init__(self, vd_path): def __init__(self, vd_path):
self._video = cv2.VideoCapture(vd_path) self._video = cv2.VideoCapture(vd_path)
@ -220,7 +252,7 @@ class S:
vdh = VdHelper(local_src_path_new) vdh = VdHelper(local_src_path_new)
self.to_del_files.append(local_src_path_new) self.to_del_files.append(local_src_path_new)
# 色深 # 色深
pf = vdh.get_pixel_format() pf = get_pixel_format(vdh.get_vd_path())
logger.Info("pixel_format: {}".format(pf)) logger.Info("pixel_format: {}".format(pf))
output_file = local_h264_720_path + ".mp4" output_file = local_h264_720_path + ".mp4"
@ -243,16 +275,18 @@ class S:
# 更新db # 更新db
db_ret = self.set_compress_finish(video_id, file_size, "", 0, src_id_h264_720, output_file_size, 100, int(time.time())) db_ret = self.set_compress_finish(video_id, file_size, "", 0, src_id_h264_720, output_file_size, 100, int(time.time()))
# 更新pf
pf_output = get_pixel_format(output_file)
logger.Info("pixel_format_output: {}".format(pf_output))
pf_ret = update_pixel_format(video_id, pf, pf_output)
# 刷新cdn # 刷新cdn
file_url = cdn_host + src_id_h264_720 file_url = cdn_host + src_id_h264_720
refresh_res = refresh_cdn_cache(file_url) refresh_res = refresh_cdn_cache(file_url)
logger.Info("refresh ali cdn, url: {}, res: {}".format(file_url, refresh_res)) logger.Info("refresh ali cdn, url: {}, res: {}".format(file_url, refresh_res))
# vdh.remove()
# os.remove(output_file)
logger.Info("before, {}, {}, {}".format(src_id, content_type, file_size)) 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("after_h264720, {}, {}, {}, {}, {}".format(src_id_h264_720, output_content_type, output_file_size, db_ret, pf_ret))
logger.Info("host: {}".format(file_url)) logger.Info("host: {}".format(file_url))
return True return True
@ -332,3 +366,4 @@ while True:
s.set_compress_finish(video_id, 0, "", 0, "", 0, -200, int(time.time())) s.set_compress_finish(video_id, 0, "", 0, "", 0, -200, int(time.time()))
else: else:
s.set_compress_finish(video_id, 0, "", 0, "", 0, -100, int(time.time())) s.set_compress_finish(video_id, 0, "", 0, "", 0, -100, int(time.time()))
# break

View File

@ -254,3 +254,7 @@ def dict2json(dic):
if not isinstance(dic, dict): if not isinstance(dic, dict):
return json.dumps({}) return json.dumps({})
return json.dumps(dic, ensure_ascii=False) return json.dumps(dic, ensure_ascii=False)
def now_str():
return get_time_str_by_ts(int(time.time()))