scripts/cronjob/media/vd_compress.py

174 lines
5.6 KiB
Python

import time
from lib.all import *
import ffmpy
import cv2
import oss2
service_name = 'vd_compress'
# 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()
access_key_id = "LTAI5tAdu5LRvZwm4LJa21Fo"
access_key_secret = "WGvSQsDralTfFAAxhEqLBOgbXqflHo"
endpoint_internal = "https://oss-cn-hangzhou.aliyuncs.com"
bucket_name = "wishpal-ironfan-media"
def get_video_w_h(path):
video = cv2.VideoCapture(path)
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 横屏 960 * 540
if width > height:
rate = float(720) / float(height)
new_width = int(float(width) * rate)
new_height = 720
else:
rate = float(720) / float(width)
new_width = 720
new_height = int(float(height) * rate)
return width, height, new_width, new_height
# video_path = "bbbbb.mov"
# print(get_video_w_h(video_path))
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/"
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)
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)
def get_one_wait_compress_video(self):
param = {
# "ids": [23431],
"status": 0,
"offset": 0,
"limit": 1,
}
res = call_service(self.url_get_videos_by_status, param)
data = safe_get_dict(res, "data")
lis = safe_get_list(data, "video")
if len(lis) > 0:
return lis[0]
return None
def set_compress_finish(self, video_id, size_src, src_id_h264, size_h264, status, resize_t):
param = {
"id": video_id,
"size_src": size_src,
"src_id_h264": src_id_h264,
"size_h264": size_h264,
"status": status,
"resize_t": resize_t,
}
logger.Info("{}".format(dict2json(param)))
res = call_service(self.url_update_video_compress, param)
ret = safe_get_int(res, "ret")
return ret
def proc_one(self):
video = self.get_one_wait_compress_video()
if not video:
return
video_id = safe_get_int(video, "id")
src_id = safe_get_str(video, "src_id")
src_id_h264 = src_id.replace("vdprod", "vdprodh264")
src_id_python_type = src_id.replace("/", "_")
src_id_h264_python_type = src_id_h264.replace("/", "_")
cur_dir = os.getcwd() + "/"
local_src_path = cur_dir + src_id_python_type
local_h264_path = cur_dir + src_id_h264_python_type
# 下载视频到本地
obj = self.save_video_from_oss(src_id, local_src_path)
content_type = safe_get_str(obj.headers, "Content-Type")
file_size = int(safe_get_str(obj.headers, "Content-Length"))
local_src_path_new = local_src_path
if content_type == "video/mp4":
local_src_path_new = local_src_path + ".mp4"
elif content_type == "video/quicktime":
local_src_path_new = local_src_path + ".mov"
else:
logger.Info("invalid content_type, id: {}, src_id: {}, content_type: {}", video_id, self.hw_cdn_host + src_id, content_type)
return
os.renames(local_src_path, local_src_path_new)
# 转成264 mp4
input_file = local_src_path_new
output_file = local_h264_path + ".mp4"
ff = ffmpy.FFmpeg(
inputs={input_file: None},
outputs={output_file: '-c:v libx264'}
)
ff.run()
# 上传
upload_ret = self.upload_video_to_oss(output_file, src_id_h264)
upload_ret_status = upload_ret.status
logger.Info("upload_ret, {}, {}".format(src_id, upload_ret_status))
output_content_type = "video/mp4"
output_file_size = os.path.getsize(output_file)
# 更新db
db_ret = self.set_compress_finish(video_id, file_size, src_id_h264, output_file_size, 100, int(time.time()))
os.remove(input_file)
os.remove(output_file)
logger.Info("before, {}, {}, {}".format(src_id, content_type, file_size))
logger.Info("after_h264, {}, {}, {}, {}".format(src_id_h264, output_content_type, output_file_size, db_ret))
logger.Info("host: {}".format(self.hw_cdn_host + src_id_h264))
def get_video_stat(video_path):
video = cv2.VideoCapture(video_path)
# 获取视频帧数
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
# 获取视频帧率
fps = video.get(cv2.CAP_PROP_FPS)
# 获取视频宽度和高度
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(video_path)
print(f"帧数: {total_frames}")
print(f"帧率: {fps}")
print(f"宽度: {width}")
print(f"高度: {height}")
s = S()
while True:
s.proc_one()
# get_video_stat("vdprod_7b_09_f8c4-bd63-4d4a-898b-6d1431f06994.mov")
# get_video_stat("vdprod_7b_09_f8c4-bd63-4d4a-898b-6d1431f06994.mp4")