-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig_loader.py
More file actions
192 lines (163 loc) · 6.59 KB
/
config_loader.py
File metadata and controls
192 lines (163 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# -*- coding: utf-8 -*-
"""Configuration management for FFmpeg transcoding pipeline."""
from __future__ import annotations
import os
import yaml
from pathlib import Path
from typing import Any, Dict, List, Optional
from dataclasses import dataclass, field, asdict
@dataclass
class ScanConfig:
"""Scanning configuration."""
root_directories: List[str] = field(default_factory=list)
file_extensions: List[str] = field(default_factory=lambda: [
".avi", ".flv", ".av1", ".m4v", ".mkv", ".mov", ".mp4",
".ts", ".mts", ".webm", ".wmv"
])
parallel_scan: bool = True
max_scan_workers: int = 8
scan_cache_enabled: bool = True
@dataclass
class EncodingConfig:
"""Encoding configuration."""
target_codec: str = "hevc"
bits_per_pixel: float = 0.045
target_crf: int = 23
hardware_encoder_priority: List[str] = field(default_factory=lambda: ["hevc_nvenc", "hevc_qsv", "hevc_amf", "libx265"])
force_10bit: bool = True
size_ok_margin: float = 1.2
tag_hevc_as_hvc1: bool = True
@dataclass
class LanguageConfig:
"""Language handling configuration."""
default_language: str = "eng"
keep_languages: List[str] = field(default_factory=lambda: ["eng", "fre", "ger", "heb", "hun", "ita", "jpn", "rum", "rus", "spa"])
ensure_english_subtitle: bool = True
ai_subtitle_languages: List[str] = field(default_factory=list)
ai_audio_languages: List[str] = field(default_factory=list)
@dataclass
class ArtifactConfig:
"""Artifact generation configuration."""
enabled: bool = False
force_on_skip: bool = True
enable_matrix: bool = True
enable_speed_up: bool = True
enable_short_clip: bool = True
matrix_columns: int = 4
matrix_rows: int = 3
matrix_width: int = 320
matrix_start_time: float = 30.0
short_duration: float = 33.0
speed_factor: float = 2.0
quality_crf: int = 24
@dataclass
class ProcessingConfig:
"""Processing behavior configuration."""
parallel_processing: bool = False
max_workers: int = 1
pause_on_exit: bool = True
error_logs_enabled: bool = True
smart_rename: bool = False
check_corruption: bool = False
@dataclass
class LLMConfig:
"""LLM integration configuration."""
enabled: bool = True
use_local_llm: bool = True
@dataclass
class OutputConfig:
"""Output paths configuration."""
work_directory: Optional[str] = None
exceptions_directory: Optional[str] = None
@dataclass
class Config:
"""Main configuration container."""
scanning: ScanConfig = field(default_factory=ScanConfig)
encoding: EncodingConfig = field(default_factory=EncodingConfig)
languages: LanguageConfig = field(default_factory=LanguageConfig)
artifacts: ArtifactConfig = field(default_factory=ArtifactConfig)
processing: ProcessingConfig = field(default_factory=ProcessingConfig)
llm: LLMConfig = field(default_factory=LLMConfig)
output: OutputConfig = field(default_factory=OutputConfig)
@classmethod
def from_yaml(cls, path: Path) -> Config:
"""Load configuration from YAML file."""
if not path.exists():
return cls() # Return defaults
with open(path, 'r', encoding='utf-8') as f:
data = yaml.safe_load(f) or {}
def _safe_init(klass, section_data):
"""Init dataclass using only keys it actually declares."""
valid = {f.name for f in klass.__dataclass_fields__.values()}
return klass(**{k: v for k, v in section_data.items() if k in valid})
return cls(
scanning=_safe_init(ScanConfig, data.get('scanning', {})),
encoding=_safe_init(EncodingConfig, data.get('encoding', {})),
languages=_safe_init(LanguageConfig, data.get('languages', {})),
artifacts=_safe_init(ArtifactConfig, data.get('artifacts', {})),
processing=_safe_init(ProcessingConfig, data.get('processing', {})),
llm=_safe_init(LLMConfig, data.get('llm', {})),
output=_safe_init(OutputConfig, data.get('output', {})),
)
def to_yaml(self, path: Path) -> None:
"""Save configuration to YAML file."""
data = {
'scanning': asdict(self.scanning),
'encoding': asdict(self.encoding),
'languages': asdict(self.languages),
'artifacts': asdict(self.artifacts),
'processing': asdict(self.processing),
'llm': asdict(self.llm),
'output': asdict(self.output),
}
with open(path, 'w', encoding='utf-8') as f:
yaml.dump(data, f, default_flow_style=False, indent=2, sort_keys=False)
def to_dict(self) -> Dict[str, Any]:
"""Convert to dictionary for JSON serialization."""
return {
'scanning': asdict(self.scanning),
'encoding': asdict(self.encoding),
'languages': asdict(self.languages),
'artifacts': asdict(self.artifacts),
'processing': asdict(self.processing),
'llm': asdict(self.llm),
'output': asdict(self.output),
}
def validate(self) -> List[str]:
"""Validate configuration and return list of issues."""
issues = []
# Validate root directories exist
for dir_path in self.scanning.root_directories:
if not Path(dir_path).exists():
issues.append(f"Root directory does not exist: {dir_path}")
# Validate numeric ranges
if not 0.0 < self.encoding.bits_per_pixel < 1.0:
issues.append(f"bits_per_pixel should be between 0 and 1, got {self.encoding.bits_per_pixel}")
if not 0 < self.encoding.target_crf < 51:
issues.append(f"target_crf should be between 0 and 51, got {self.encoding.target_crf}")
if self.processing.max_workers < 1:
issues.append(f"max_workers must be >= 1, got {self.processing.max_workers}")
return issues
# Global config instance
_config: Optional[Config] = None
def load_config(config_path: Optional[Path] = None) -> Config:
"""Load configuration from file or create default."""
global _config
if config_path is None:
# Look for config.yaml in script directory
script_dir = Path(__file__).parent
config_path = script_dir / "config.yaml"
_config = Config.from_yaml(config_path)
# Validate
issues = _config.validate()
if issues:
print("⚠️ Configuration warnings:")
for issue in issues:
print(f" - {issue}")
return _config
def get_config() -> Config:
"""Get the current configuration."""
global _config
if _config is None:
load_config()
return _config