You've already forked FrameTour-RenderWorker
127 lines
3.3 KiB
Python
127 lines
3.3 KiB
Python
"""
|
|
FFmpeg工具模块 - 提供FFmpeg命令构建和处理的公共函数
|
|
"""
|
|
import logging
|
|
from typing import List, Tuple, Optional
|
|
from config.settings import get_ffmpeg_config
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def build_base_ffmpeg_args() -> List[str]:
|
|
"""
|
|
构建基础FFmpeg参数
|
|
|
|
Returns:
|
|
基础参数列表
|
|
"""
|
|
config = get_ffmpeg_config()
|
|
args = ["ffmpeg", "-y", "-hide_banner"]
|
|
args.extend(config.progress_args)
|
|
args.extend(config.loglevel_args)
|
|
return args
|
|
|
|
def build_null_audio_input() -> List[str]:
|
|
"""
|
|
构建空音频输入参数
|
|
|
|
Returns:
|
|
空音频输入参数列表
|
|
"""
|
|
config = get_ffmpeg_config()
|
|
return config.null_audio_args
|
|
|
|
def build_amix_filter(input1: str, input2: str, output: str) -> str:
|
|
"""
|
|
构建音频混合滤镜
|
|
|
|
Args:
|
|
input1: 第一个音频输入
|
|
input2: 第二个音频输入
|
|
output: 输出流名称
|
|
|
|
Returns:
|
|
混合滤镜字符串
|
|
"""
|
|
config = get_ffmpeg_config()
|
|
return f"{input1}[{input2}]{config.amix_args[0]}[{output}]"
|
|
|
|
def build_overlay_scale_filter(video_input: str, overlay_input: str, output: str) -> str:
|
|
"""
|
|
构建覆盖层缩放滤镜
|
|
|
|
Args:
|
|
video_input: 视频输入流
|
|
overlay_input: 覆盖层输入流
|
|
output: 输出流名称
|
|
|
|
Returns:
|
|
缩放滤镜字符串
|
|
"""
|
|
config = get_ffmpeg_config()
|
|
if config.overlay_scale_mode == "scale":
|
|
return f"{video_input}[{overlay_input}]scale=iw:ih[{output}]"
|
|
else:
|
|
return f"{video_input}[{overlay_input}]{config.overlay_scale_mode}=iw:ih[{output}]"
|
|
|
|
def get_annexb_filter() -> str:
|
|
"""
|
|
获取annexb转换滤镜
|
|
|
|
Returns:
|
|
annexb滤镜名称
|
|
"""
|
|
config = get_ffmpeg_config()
|
|
encoder_args_str = " ".join(config.encoder_args).lower()
|
|
if "hevc" in encoder_args_str:
|
|
return "hevc_mp4toannexb"
|
|
return "h264_mp4toannexb"
|
|
|
|
def build_standard_output_args() -> List[str]:
|
|
"""
|
|
构建标准输出参数
|
|
|
|
Returns:
|
|
输出参数列表
|
|
"""
|
|
config = get_ffmpeg_config()
|
|
return [
|
|
*config.video_args,
|
|
*config.audio_args,
|
|
*config.encoder_args,
|
|
*config.default_args
|
|
]
|
|
|
|
def validate_ffmpeg_file_extensions(file_path: str) -> bool:
|
|
"""
|
|
验证文件扩展名是否为FFmpeg支持的格式
|
|
|
|
Args:
|
|
file_path: 文件路径
|
|
|
|
Returns:
|
|
是否为支持的格式
|
|
"""
|
|
supported_extensions = {
|
|
'.mp4', '.avi', '.mov', '.mkv', '.flv', '.wmv', '.webm',
|
|
'.ts', '.m2ts', '.mts', '.m4v', '.3gp', '.asf', '.rm',
|
|
'.mp3', '.wav', '.aac', '.flac', '.ogg', '.m4a', '.wma'
|
|
}
|
|
|
|
import os
|
|
_, ext = os.path.splitext(file_path.lower())
|
|
return ext in supported_extensions
|
|
|
|
def estimate_processing_time(input_duration: float, complexity_factor: float = 1.0) -> float:
|
|
"""
|
|
估算处理时间
|
|
|
|
Args:
|
|
input_duration: 输入文件时长(秒)
|
|
complexity_factor: 复杂度因子(1.0为普通处理)
|
|
|
|
Returns:
|
|
预估处理时间(秒)
|
|
"""
|
|
# 基础处理速度假设为实时的0.5倍(即处理1秒视频需要2秒)
|
|
base_processing_ratio = 2.0
|
|
return input_duration * base_processing_ratio * complexity_factor |