update VideoCompress
This commit is contained in:
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"save_to": "single",
|
"save_to": "single",
|
||||||
"crf": 18,
|
"crf": null,
|
||||||
"bitrate": null,
|
"bitrate": "15M",
|
||||||
"codec": "h264",
|
"codec": "h264_qsv",
|
||||||
"hwaccel": null,
|
"hwaccel": "qsv",
|
||||||
"extra": [],
|
"extra": [],
|
||||||
"ffmpeg": "ffmpeg",
|
"ffmpeg": "ffmpeg",
|
||||||
"ffprobe": "ffprobe",
|
"ffprobe": "ffprobe",
|
||||||
|
|||||||
@ -7,7 +7,6 @@ from datetime import datetime
|
|||||||
from time import time
|
from time import time
|
||||||
from rich.logging import RichHandler
|
from rich.logging import RichHandler
|
||||||
from rich.progress import Progress
|
from rich.progress import Progress
|
||||||
from rich.console import Console
|
|
||||||
from pickle import dumps, loads
|
from pickle import dumps, loads
|
||||||
from typing import Optional, Callable, Literal, List, Any, TYPE_CHECKING
|
from typing import Optional, Callable, Literal, List, Any, TYPE_CHECKING
|
||||||
import atexit
|
import atexit
|
||||||
@ -15,6 +14,7 @@ import re
|
|||||||
import get_frame
|
import get_frame
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
|
import shutil
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from pydantic import BaseModel, Field, field_validator, model_validator
|
from pydantic import BaseModel, Field, field_validator, model_validator
|
||||||
@ -171,6 +171,7 @@ else:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning("Failed to create config file.", exc_info=e)
|
logging.warning("Failed to create config file.", exc_info=e)
|
||||||
|
|
||||||
|
current_running_file:Optional[Path] = None
|
||||||
|
|
||||||
def get_cmd(video_path: str | Path, output_file: str | Path) -> list[str]:
|
def get_cmd(video_path: str | Path, output_file: str | Path) -> list[str]:
|
||||||
if isinstance(video_path, Path):
|
if isinstance(video_path, Path):
|
||||||
@ -289,6 +290,7 @@ def process_video(
|
|||||||
compress_dir: Optional[Path] = None,
|
compress_dir: Optional[Path] = None,
|
||||||
update_func: Optional[Callable[[Optional[int], Optional[str]], None]] = None,
|
update_func: Optional[Callable[[Optional[int], Optional[str]], None]] = None,
|
||||||
):
|
):
|
||||||
|
global current_running_file
|
||||||
|
|
||||||
if compress_dir is None:
|
if compress_dir is None:
|
||||||
# 在视频文件所在目录下创建 compress 子目录(如果不存在)
|
# 在视频文件所在目录下创建 compress 子目录(如果不存在)
|
||||||
@ -308,6 +310,7 @@ def process_video(
|
|||||||
|
|
||||||
video_path_str = str(video_path.absolute())
|
video_path_str = str(video_path.absolute())
|
||||||
command = get_cmd(video_path_str, output_file)
|
command = get_cmd(video_path_str, output_file)
|
||||||
|
current_running_file = output_file
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.Popen(
|
result = subprocess.Popen(
|
||||||
@ -341,6 +344,8 @@ def process_video(
|
|||||||
rate = match.group(0) if match else None
|
rate = match.group(0) if match else None
|
||||||
update_func(frame_number, rate)
|
update_func(frame_number, rate)
|
||||||
|
|
||||||
|
current_running_file = None
|
||||||
|
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"处理文件 {video_path_str} 失败"
|
f"处理文件 {video_path_str} 失败"
|
||||||
@ -391,6 +396,14 @@ def process_video(
|
|||||||
if not ret:
|
if not ret:
|
||||||
logging.error("重试仍然失败。")
|
logging.error("重试仍然失败。")
|
||||||
return False
|
return False
|
||||||
|
else:
|
||||||
|
if video_path.stat().st_size <= output_file.stat().st_size:
|
||||||
|
logging.info(
|
||||||
|
f"压缩后文件比原文件大,直接复制原文件: {video_path_str}"
|
||||||
|
)
|
||||||
|
output_file.unlink(missing_ok=True)
|
||||||
|
shutil.copy2(video_path, output_file)
|
||||||
|
return True
|
||||||
else:
|
else:
|
||||||
logging.debug(f"文件处理成功: {video_path_str} -> {output_file}")
|
logging.debug(f"文件处理成功: {video_path_str} -> {output_file}")
|
||||||
|
|
||||||
@ -401,6 +414,9 @@ def process_video(
|
|||||||
f"执行 ffmpeg 命令时发生异常, 文件:{str(video_path_str)},cmd={' '.join(map(str,command))}",
|
f"执行 ffmpeg 命令时发生异常, 文件:{str(video_path_str)},cmd={' '.join(map(str,command))}",
|
||||||
exc_info=e,
|
exc_info=e,
|
||||||
)
|
)
|
||||||
|
if current_running_file is not None:
|
||||||
|
current_running_file.unlink(missing_ok=True)
|
||||||
|
current_running_file = None
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -578,12 +594,27 @@ def exit_pause():
|
|||||||
elif os.name == "posix":
|
elif os.name == "posix":
|
||||||
os.system("read -p 'Press Enter to continue...'")
|
os.system("read -p 'Press Enter to continue...'")
|
||||||
|
|
||||||
|
def finalize():
|
||||||
|
global current_running_file
|
||||||
|
if current_running_file is not None:
|
||||||
|
try:
|
||||||
|
current_running_file.unlink(missing_ok=True)
|
||||||
|
except Exception as e:
|
||||||
|
try:
|
||||||
|
logging.error(
|
||||||
|
"Failed to delete incomplete output file after keyboard interrupt. CHECK IF LAST PROCSSING VIDEO IS COMPLETED",
|
||||||
|
exc_info=e,
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
print("Failed to delete incomplete output file after keyboard interrupt. CHECK IF LAST PROCSSING VIDEO IS COMPLETED")
|
||||||
|
current_running_file = None
|
||||||
|
|
||||||
def main(_root=None):
|
def main(_root=None):
|
||||||
|
|
||||||
atexit.register(exit_pause)
|
atexit.register(exit_pause)
|
||||||
|
atexit.register(finalize)
|
||||||
|
|
||||||
global root
|
global root, current_running_file
|
||||||
|
|
||||||
if _root is not None:
|
if _root is not None:
|
||||||
setup_logging()
|
setup_logging()
|
||||||
@ -623,11 +654,11 @@ def main(_root=None):
|
|||||||
logging.info("Normal termination of Video Compress.")
|
logging.info("Normal termination of Video Compress.")
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
"Error termination via keyboard interrupt, CHECK IF LAST PROCSSING VIDEO IS COMPLETED."
|
"Error termination via keyboard interrupt."
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(
|
logging.error(
|
||||||
"Error termination via unhandled error, CHECK IF LAST PROCSSING VIDEO IS COMPLETED.",
|
"Error termination via unhandled error",
|
||||||
exc_info=e,
|
exc_info=e,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user