This commit is contained in:
lwl0608 2024-09-14 12:30:56 +08:00
parent 967343db80
commit 1d152d07f0
1 changed files with 59 additions and 9 deletions

View File

@ -8,13 +8,13 @@ import oss2
import subprocess
service_name = 'vd_compress'
log_dir = '/app/log'
# log_dir = '/Users/erwin/data/log'
# 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-internal.aliyuncs.com"
endpoint_internal = "https://oss-cn-hangzhou.aliyuncs.com"
bucket_name = "wishpal-ironfan-media"
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint_internal, bucket_name)
@ -37,6 +37,9 @@ class VdHelper:
def get_vd_path(self):
return self._vd_path
def set_vd_path(self, vd_path):
self._vd_path = vd_path
def get_fps(self):
return self._fps
@ -63,12 +66,44 @@ class VdHelper:
return new_width, new_height
def get_pixel_format(self):
command = [
'ffprobe', '-v', 'quiet',
'-show_streams', '-select_streams', 'v:0',
'-print_format', 'json', self._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'
def transfer_h264_720p_25fps_10bit_to_8bit(self, output_path_8bit, output_path):
# ffmpeg -i input.mov -pix_fmt gbrp10le -vf format=gbrp -pix_fmt yuv420p input_8bit.mp4
output_args = "-pix_fmt gbrp10le -vf format=gbrp -pix_fmt yuv420p -y"
ff = ffmpy.FFmpeg(
inputs={self.get_vd_path(): None},
outputs={output_path_8bit: output_args}
)
ff.run()
logger.Info("finish 10bit to 8bit, path: {}".format(output_path_8bit))
self.set_vd_path(output_path_8bit)
self.transfer_h264_720p_25fps(output_path)
def transfer_h264_720p_25fps(self, output_path):
new_width, new_height = self.resize(720)
new_size_str = "{}x{}".format(new_width, new_height)
output_args = '-c:v libx264 -s {} -crf {}'.format(new_size_str, 23)
output_args = '-c:v libx264 -s {} -crf {} -y'.format(new_size_str, 23)
if self.get_fps() > 25:
output_args = '-c:v libx264 -s {} -crf {} -r 25'.format(new_size_str, 23)
output_args = '-c:v libx264 -s {} -crf {} -r 25 -y'.format(new_size_str, 23)
ff = ffmpy.FFmpeg(
inputs={self.get_vd_path(): None},
outputs={output_path: output_args}
@ -97,7 +132,7 @@ class S:
def get_one_wait_compress_video(self):
param = {
"ids": [], # 52062
"ids": [130337], # 52062
"status": 0,
"offset": 0,
"limit": 1,
@ -155,6 +190,7 @@ class S:
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"))
logger.Info("video object, content_type: {}, fsize: {}".format(content_type, file_size))
if content_type == "video/mp4":
local_src_path_new = local_src_path + ".mp4"
elif content_type == "video/quicktime":
@ -169,12 +205,25 @@ class S:
# vd helper
vdh = VdHelper(local_src_path_new)
self.to_del_files.append(local_src_path_new)
# 转成264 720p mp4
# 色深
pf = vdh.get_pixel_format()
logger.Info("pixel_format: {}".format(pf))
output_file = local_h264_720_path + ".mp4"
vdh.transfer_h264_720p_25fps(output_file)
self.to_del_files.append(output_file)
# self.to_del_files.append(output_file)
if pf not in ["yuv420p", "yuvj420p"]:
# 先转成8bit, 再转成264 720p mp4
output_file_8bit = local_h264_720_path + "_8bit_tmp.mp4"
vdh.transfer_h264_720p_25fps_10bit_to_8bit(output_file_8bit, output_file)
else:
# 转成264 720p mp4
vdh.transfer_h264_720p_25fps(output_file)
# 上传
# 如果文件存在,则先删除再上传
if bucket.object_exists(src_id_h264_720):
ret_oss = bucket.delete_object(src_id_h264_720)
logger.Info("{} already exists in oss, delete status: {}".format(src_id_h264_720, ret_oss.status))
upload_ret = self.upload_video_to_oss(output_file, src_id_h264_720)
upload_ret_status = upload_ret.status
logger.Info("upload_ret, {}, {}".format(src_id, upload_ret_status))
@ -268,3 +317,4 @@ while True:
s.set_compress_finish(video_id, 0, "", 0, "", 0, -200, int(time.time()))
else:
s.set_compress_finish(video_id, 0, "", 0, "", 0, -100, int(time.time()))
break