From 92160b05ea840e08a2b3df164482cfb8b597ba3c Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Mon, 13 Jan 2025 10:33:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- biz/ffmpeg.py | 1 + entity/ffmpeg.py | 24 +++++++++++------------- template/__init__.py | 7 ++++--- util/api.py | 6 +++++- util/ffmpeg.py | 2 +- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/biz/ffmpeg.py b/biz/ffmpeg.py index e3d61b9..927637c 100644 --- a/biz/ffmpeg.py +++ b/biz/ffmpeg.py @@ -69,6 +69,7 @@ def parse_video(source, task_params, template_info): def start_ffmpeg_task(ffmpeg_task): for task in ffmpeg_task.analyze_input_render_tasks(): start_ffmpeg_task(task) + ffmpeg_task.correct_task_type() return ffmpeg.start_render(ffmpeg_task) diff --git a/entity/ffmpeg.py b/entity/ffmpeg.py index 18f28d0..ef9e19e 100644 --- a/entity/ffmpeg.py +++ b/entity/ffmpeg.py @@ -124,11 +124,11 @@ class FfmpegTask(object): def get_ffmpeg_args(self): args = ['-y', '-hide_banner'] - video_output_str = "[0:v]" if self.task_type == 'encode': + # args += ('-hwaccel', 'qsv', '-hwaccel_output_format', 'qsv') input_args = [] filter_args = [] - output_args = ["-shortest", "-c:v", "h264_qsv"] + output_args = ["-shortest", "-c:v", "h264_qsv", "-global_quality", "28", "-look_ahead", "1"] if self.annexb: output_args.append("-bsf:v") output_args.append("h264_mp4toannexb") @@ -192,12 +192,7 @@ class FfmpegTask(object): f.write("file '"+input_file+"'\n") elif isinstance(input_file, FfmpegTask): f.write("file '" + input_file.get_output_file() + "'\n") - input_args.append("-f") - input_args.append("concat") - input_args.append("-safe") - input_args.append("0") - input_args.append("-i") - input_args.append(_tmp_file) + input_args += ("-f", "concat", "-safe", "0", "-i", _tmp_file) output_args.append("-c:v") output_args.append("copy") if len(self.audios) > 0: @@ -207,11 +202,9 @@ class FfmpegTask(object): output_args.append("copy") output_args.append("-f") output_args.append("mp4") + output_args += ("-c:v", "h264_qsv", "-r", "25", "-global_quality", "28", "-look_ahead", "1") return args + input_args + output_args + [self.get_output_file()] - output_args.append("-c:v") - output_args.append("h264_qsv") - output_args.append("-r") - output_args.append("25") + output_args += ("-c:v", "h264_qsv", "-r", "25", "-global_quality", "28", "-look_ahead", "1") filter_args = [] video_output_str = "[0:v]" audio_output_str = "[0:a]" @@ -266,7 +259,12 @@ class FfmpegTask(object): else: self.output_file = "rand_" + str(uuid.uuid4()) + ".mp4" else: - self.output_file = file + if isinstance(file, FfmpegTask): + if file == self: + return + self.output_file = file.get_output_file() + if type(file) is str: + self.output_file = file def check_annexb(self): for input_file in self.input_file: diff --git a/template/__init__.py b/template/__init__.py index c96f376..381860a 100644 --- a/template/__init__.py +++ b/template/__init__.py @@ -85,11 +85,12 @@ def download_template(template_id): if 'source' in _template: if str(_template['source']).startswith("http"): _, _fn = os.path.split(_template['source']) - oss.download_from_oss(_template['source'], os.path.join(template_info['local_path'], _fn)) + new_fp = os.path.join(template_info['local_path'], _fn) + oss.download_from_oss(_template['source'], new_fp) if _fn.endswith(".mp4"): from util.ffmpeg import to_annexb - _fn = to_annexb(os.path.join(template_info['local_path'], _fn)) - _template['source'] = os.path.relpath(_fn, template_info['local_path']) + new_fp = to_annexb(new_fp) + _template['source'] = os.path.relpath(new_fp, template_info['local_path']) if 'overlays' in _template: for i in range(len(_template['overlays'])): overlay = _template['overlays'][i] diff --git a/util/api.py b/util/api.py index f308b0e..f0e0d99 100644 --- a/util/api.py +++ b/util/api.py @@ -139,6 +139,7 @@ def report_task_failed(task_info): def upload_task_file(task_info, ffmpeg_task): + logger.info("开始上传文件: %s", task_info.get("id")) try: response = session.post('{0}/{1}/uploadUrl'.format(os.getenv('API_ENDPOINT'), task_info.get("id")), json={ 'accessKey': os.getenv('ACCESS_KEY'), @@ -149,10 +150,13 @@ def upload_task_file(task_info, ffmpeg_task): return False data = response.json() url = data.get('data', "") + logger.info("开始上传文件: %s 至 %s", task_info.get("id"), url) try: with open(ffmpeg_task.get_output_file(), 'rb') as f: requests.put(url, data=f) except requests.RequestException as e: logger.error("上传失败!", e) return False - return True \ No newline at end of file + finally: + logger.info("上传文件结束: %s", task_info.get("id")) + return True diff --git a/util/ffmpeg.py b/util/ffmpeg.py index bd1e8d6..214b726 100644 --- a/util/ffmpeg.py +++ b/util/ffmpeg.py @@ -23,11 +23,11 @@ def to_annexb(file): def start_render(ffmpeg_task: FfmpegTask): logger.info(ffmpeg_task) - logger.info(ffmpeg_task.get_ffmpeg_args()) if not ffmpeg_task.need_run(): ffmpeg_task.set_output_file(ffmpeg_task.input_file[0]) return True ffmpeg_args = ffmpeg_task.get_ffmpeg_args() + logger.info(ffmpeg_args) if len(ffmpeg_args) == 0: ffmpeg_task.set_output_file(ffmpeg_task.input_file[0]) return True