diff --git a/danmaku_workflow.py b/danmaku_workflow.py index 6c7d67f..c877ef1 100644 --- a/danmaku_workflow.py +++ b/danmaku_workflow.py @@ -4,6 +4,7 @@ import platform import subprocess import sys import threading +import traceback from hashlib import md5 from typing import Optional, IO, Union @@ -292,13 +293,18 @@ class WorkerThread(QThread): return duration_str_to_float(_duration_str) def run(self) -> None: - self.label.start_running() - job = self.label.get_job() - if job.type == Job.DANMAKU_ENCODE: - self.run_danmaku_encode(job) - elif job.type == Job.PURE_SPLIT: - self.quick_split_video(job.video) - self.label.stop_running() + try: + self.label.start_running() + job = self.label.get_job() + if job.type == Job.DANMAKU_ENCODE: + self.run_danmaku_encode(job) + elif job.type == Job.PURE_SPLIT: + self.quick_split_video(job.video) + except Exception as e: + print(e) + print(traceback.format_exc()) + finally: + self.label.stop_running() def run_danmaku_encode(self, job: Job): base_danmaku = job.danmaku.pop(0) @@ -323,6 +329,9 @@ class WorkerThread(QThread): for _f in job.subtitles: if os.path.isfile(_f): os.remove(_f) + for _f in split_files: + if os.path.isfile(_f): + os.remove(_f) def encode_video_with_subtitles(self, orig_filename: str, subtitles: list[str], new_filename: str): if FFMPEG_USE_NVIDIA_GPU: @@ -347,10 +356,8 @@ class WorkerThread(QThread): "-hwaccel", "qsv", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264_qsv", - "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", - "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", - "-qmin", "10", "-qmax", "32", "-crf", "16", - "-fflags", "+genpts", "-shortest", "-movflags", "faststart", + "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", + *_common_ffmpeg_params(), # "-t", "10", new_filename ], **subprocess_args(True)) @@ -361,10 +368,8 @@ class WorkerThread(QThread): "-hwaccel", "vaapi", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles) + ",hwupload", "-c:a", "copy", "-c:v", "h264_vaapi", - "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", - "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", - "-qmin", "10", "-qmax", "32", "-crf", "16", - "-fflags", "+genpts", "-shortest", "-movflags", "faststart", + "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", + *_common_ffmpeg_params(), # "-t", "10", new_filename ], **subprocess_args(True)) @@ -375,10 +380,8 @@ class WorkerThread(QThread): "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), "-c:a", "copy", "-c:v", "h264", - "-f", "mp4", "-preset:v", "fast", "-profile:v", "high", "-level", "4.1", - "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", - "-qmin", "10", "-qmax", "32", "-crf", "16", - "-fflags", "+genpts", "-shortest", "-movflags", "faststart", + "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", + *_common_ffmpeg_params(), # "-t", "10", new_filename ], **subprocess_args(True)) @@ -392,8 +395,7 @@ class WorkerThread(QThread): print("[!]Not Enabled Both GPU") self.encode_video_with_subtitles(orig_filename, subtitles, new_fullpath) return [new_fullpath] - _duration_str = self.get_video_real_duration(orig_filename) - duration = duration_str_to_float(_duration_str) + duration = self.get_video_real_duration(orig_filename) if duration > (VIDEO_CLIP_EACH_SEC * 5): # qsv 压制前2段,剩余交由nvenc压制 _slices = int(duration / VIDEO_CLIP_EACH_SEC) @@ -407,6 +409,7 @@ class WorkerThread(QThread): "-hwaccel", "qsv", "-ss", str((_slices - 1) * VIDEO_CLIP_EACH_SEC), "-copyts", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), + "-ss", str((_slices - 1) * VIDEO_CLIP_EACH_SEC), "-c:a", "copy", "-c:v", "h264_qsv", "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", *_common_ffmpeg_params(), @@ -425,6 +428,8 @@ class WorkerThread(QThread): # "-t", "10", new_fullpath1 ], **subprocess_args(True)) + threading.Thread(target=self.handle_ffmpeg_output, args=(encode_process0.stdout, True,)).start() + threading.Thread(target=self.handle_ffmpeg_output, args=(encode_process1.stdout,)).start() encode_process0.wait() encode_process1.wait() return [new_filename0, new_filename1] @@ -441,6 +446,7 @@ class WorkerThread(QThread): "-hwaccel", "qsv", "-ss", str(_slices * VIDEO_CLIP_EACH_SEC), "-copyts", "-i", orig_filename, "-vf", ",".join("subtitles=%s" % i for i in subtitles), + "-ss", str(_slices * VIDEO_CLIP_EACH_SEC), "-c:a", "copy", "-c:v", "h264_qsv", "-f", "mp4", "-b:v", VIDEO_BITRATE, "-rc:v", "vbr", "-tune:v", "hq", *_common_ffmpeg_params(), @@ -459,6 +465,8 @@ class WorkerThread(QThread): # "-t", "10", new_fullpath1 ], **subprocess_args(True)) + threading.Thread(target=self.handle_ffmpeg_output, args=(encode_process0.stdout, True,)).start() + threading.Thread(target=self.handle_ffmpeg_output, args=(encode_process1.stdout,)).start() encode_process1.wait() encode_process0.wait() return [new_filename1] @@ -492,6 +500,7 @@ class WorkerThread(QThread): "{}.mp4".format(current_dt) ], **subprocess_args(True)) self.handle_ffmpeg_output(split_process.stdout) + split_process.wait() current_sec += VIDEO_CLIP_EACH_SEC def handle_ffmpeg_output(self, stdout: Optional[IO[bytes]], second=False) -> str: