scripts/cronjob/media/vd_compress.py

113 lines
3.4 KiB
Python

from lib.all import *
import ffmpy
import cv2
import oss2
# 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/url_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 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_python_type = src_id.replace("/", "_")
cur_dir = os.getcwd() + "/"
local_src_path = cur_dir + src_id_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:
print("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_src_path + ".mp4"
ff = ffmpy.FFmpeg(
inputs={input_file: None},
outputs={output_file: '-c:v libx264'}
)
ff.run()
print(content_type, file_size)
print(local_src_path_new)
s = S()
s.proc_one()