subtitle-generator 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ """
2
+ Configuration loader with YAML support.
3
+
4
+ Loads configuration from default.yaml and optionally overrides
5
+ with user-specific configuration.
6
+ """
7
+
8
+ import logging
9
+ import os
10
+ from pathlib import Path
11
+ from typing import Optional
12
+
13
+ import yaml
14
+
15
+ from .settings import Settings
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+ # Global settings instance
20
+ _settings: Optional[Settings] = None
21
+
22
+ # Default config locations
23
+ DEFAULT_CONFIG_PATH = Path(__file__).parent.parent.parent / "config" / "default.yaml"
24
+ USER_CONFIG_PATH = Path.home() / ".subtitle" / "config.yaml"
25
+
26
+
27
+ def load_config(
28
+ config_path: Optional[str] = None,
29
+ user_config_path: Optional[str] = None,
30
+ ) -> Settings:
31
+ """
32
+ Load configuration from YAML files.
33
+
34
+ Loads the default configuration and optionally overrides with
35
+ user-specific configuration.
36
+
37
+ Args:
38
+ config_path: Optional path to default config (uses built-in if None)
39
+ user_config_path: Optional path to user config
40
+
41
+ Returns:
42
+ Settings instance with loaded configuration
43
+ """
44
+ global _settings
45
+
46
+ # Start with defaults
47
+ config_data = {}
48
+
49
+ # Load default config
50
+ default_path = Path(config_path) if config_path else DEFAULT_CONFIG_PATH
51
+ if default_path.exists():
52
+ try:
53
+ with open(default_path, "r") as f:
54
+ config_data = yaml.safe_load(f) or {}
55
+ logger.debug(f"Loaded default config from: {default_path}")
56
+ except Exception as e:
57
+ logger.warning(f"Failed to load default config: {e}")
58
+
59
+ # Load and merge user config
60
+ user_path = Path(user_config_path) if user_config_path else USER_CONFIG_PATH
61
+ if user_path.exists():
62
+ try:
63
+ with open(user_path, "r") as f:
64
+ user_data = yaml.safe_load(f) or {}
65
+ config_data = _deep_merge(config_data, user_data)
66
+ logger.debug(f"Loaded user config from: {user_path}")
67
+ except Exception as e:
68
+ logger.warning(f"Failed to load user config: {e}")
69
+
70
+ # Create settings from merged config
71
+ _settings = Settings.from_dict(config_data)
72
+
73
+ return _settings
74
+
75
+
76
+ def get_config() -> Settings:
77
+ """
78
+ Get the current configuration.
79
+
80
+ Loads default configuration if not already loaded.
81
+
82
+ Returns:
83
+ Current Settings instance
84
+ """
85
+ global _settings
86
+
87
+ if _settings is None:
88
+ _settings = load_config()
89
+
90
+ return _settings
91
+
92
+
93
+ def reset_config():
94
+ """Reset configuration to force reload on next access."""
95
+ global _settings
96
+ _settings = None
97
+
98
+
99
+ def _deep_merge(base: dict, override: dict) -> dict:
100
+ """
101
+ Deep merge two dictionaries.
102
+
103
+ Values from override take precedence over base.
104
+ Nested dictionaries are merged recursively.
105
+
106
+ Args:
107
+ base: Base dictionary
108
+ override: Override dictionary
109
+
110
+ Returns:
111
+ Merged dictionary
112
+ """
113
+ result = base.copy()
114
+
115
+ for key, value in override.items():
116
+ if key in result and isinstance(result[key], dict) and isinstance(value, dict):
117
+ result[key] = _deep_merge(result[key], value)
118
+ else:
119
+ result[key] = value
120
+
121
+ return result
122
+
123
+
124
+ def save_user_config(settings: Settings, path: Optional[str] = None):
125
+ """
126
+ Save settings to user config file.
127
+
128
+ Args:
129
+ settings: Settings to save
130
+ path: Optional custom path (uses default user path if None)
131
+ """
132
+ config_path = Path(path) if path else USER_CONFIG_PATH
133
+
134
+ # Ensure parent directory exists
135
+ config_path.parent.mkdir(parents=True, exist_ok=True)
136
+
137
+ with open(config_path, "w") as f:
138
+ yaml.dump(settings.to_dict(), f, default_flow_style=False)
139
+
140
+ logger.info(f"Saved user config to: {config_path}")
src/config/settings.py ADDED
@@ -0,0 +1,144 @@
1
+ """
2
+ Settings dataclass for typed configuration.
3
+
4
+ Provides a typed interface for accessing configuration values
5
+ with sensible defaults.
6
+ """
7
+
8
+ from dataclasses import dataclass, field
9
+ from typing import Optional
10
+
11
+
12
+ @dataclass
13
+ class WhisperSettings:
14
+ """Whisper-related settings."""
15
+
16
+ default_model: str = "base"
17
+ threads: int = 4
18
+ processors: int = 1
19
+ binary_path: str = "./binary/whisper-cli"
20
+
21
+
22
+ @dataclass
23
+ class PathSettings:
24
+ """Path-related settings."""
25
+
26
+ models_dir: str = "./models"
27
+ data_dir: str = "./data"
28
+ output_dir: str = "./output"
29
+
30
+
31
+ @dataclass
32
+ class FormatSettings:
33
+ """Format-related settings."""
34
+
35
+ default: str = "vtt"
36
+ supported: list[str] = field(default_factory=lambda: ["vtt", "srt", "txt", "json", "lrc"])
37
+
38
+
39
+ @dataclass
40
+ class VideoSettings:
41
+ """Video processing settings."""
42
+
43
+ video_codec: str = "copy"
44
+ audio_codec: str = "copy"
45
+ subtitle_codec: str = "mov_text"
46
+ overwrite_output: bool = True
47
+
48
+
49
+ @dataclass
50
+ class LoggingSettings:
51
+ """Logging settings."""
52
+
53
+ level: str = "INFO"
54
+ format: str = "%(asctime)s - %(levelname)s - %(message)s"
55
+
56
+
57
+ @dataclass
58
+ class Settings:
59
+ """
60
+ Main settings container.
61
+
62
+ Holds all configuration values with typed access and defaults.
63
+ """
64
+
65
+ whisper: WhisperSettings = field(default_factory=WhisperSettings)
66
+ paths: PathSettings = field(default_factory=PathSettings)
67
+ formats: FormatSettings = field(default_factory=FormatSettings)
68
+ video: VideoSettings = field(default_factory=VideoSettings)
69
+ logging: LoggingSettings = field(default_factory=LoggingSettings)
70
+
71
+ @classmethod
72
+ def from_dict(cls, data: dict) -> "Settings":
73
+ """
74
+ Create Settings from a dictionary.
75
+
76
+ Args:
77
+ data: Configuration dictionary
78
+
79
+ Returns:
80
+ Settings instance
81
+ """
82
+ whisper_data = data.get("whisper", {})
83
+ paths_data = data.get("paths", {})
84
+ formats_data = data.get("formats", {})
85
+ video_data = data.get("video", {})
86
+ logging_data = data.get("logging", {})
87
+
88
+ return cls(
89
+ whisper=WhisperSettings(
90
+ default_model=whisper_data.get("default_model", "base"),
91
+ threads=whisper_data.get("threads", 4),
92
+ processors=whisper_data.get("processors", 1),
93
+ binary_path=whisper_data.get("binary_path", "./binary/whisper-cli"),
94
+ ),
95
+ paths=PathSettings(
96
+ models_dir=paths_data.get("models_dir", "./models"),
97
+ data_dir=paths_data.get("data_dir", "./data"),
98
+ output_dir=paths_data.get("output_dir", "./output"),
99
+ ),
100
+ formats=FormatSettings(
101
+ default=formats_data.get("default", "vtt"),
102
+ supported=formats_data.get("supported", ["vtt", "srt", "txt", "json", "lrc"]),
103
+ ),
104
+ video=VideoSettings(
105
+ video_codec=video_data.get("video_codec", "copy"),
106
+ audio_codec=video_data.get("audio_codec", "copy"),
107
+ subtitle_codec=video_data.get("subtitle_codec", "mov_text"),
108
+ overwrite_output=video_data.get("overwrite_output", True),
109
+ ),
110
+ logging=LoggingSettings(
111
+ level=logging_data.get("level", "INFO"),
112
+ format=logging_data.get("format", "%(asctime)s - %(levelname)s - %(message)s"),
113
+ ),
114
+ )
115
+
116
+ def to_dict(self) -> dict:
117
+ """Convert settings to dictionary."""
118
+ return {
119
+ "whisper": {
120
+ "default_model": self.whisper.default_model,
121
+ "threads": self.whisper.threads,
122
+ "processors": self.whisper.processors,
123
+ "binary_path": self.whisper.binary_path,
124
+ },
125
+ "paths": {
126
+ "models_dir": self.paths.models_dir,
127
+ "data_dir": self.paths.data_dir,
128
+ "output_dir": self.paths.output_dir,
129
+ },
130
+ "formats": {
131
+ "default": self.formats.default,
132
+ "supported": self.formats.supported,
133
+ },
134
+ "video": {
135
+ "video_codec": self.video.video_codec,
136
+ "audio_codec": self.video.audio_codec,
137
+ "subtitle_codec": self.video.subtitle_codec,
138
+ "overwrite_output": self.video.overwrite_output,
139
+ },
140
+ "logging": {
141
+ "level": self.logging.level,
142
+ "format": self.logging.format,
143
+ },
144
+ }
src/core/__init__.py ADDED
@@ -0,0 +1,20 @@
1
+ """Core module - Transcription and video processing."""
2
+
3
+ from .transcriber import TranscriberStrategy, WhisperCppTranscriber
4
+ from .subtitle_gen import SubtitleGenerator
5
+ from .video_processor import VideoProcessor
6
+ from .batch_processor import BatchProcessor, BatchState, BatchSummary, BatchFileResult
7
+ from .async_processor import AsyncProcessor
8
+
9
+ __all__ = [
10
+ "TranscriberStrategy",
11
+ "WhisperCppTranscriber",
12
+ "SubtitleGenerator",
13
+ "VideoProcessor",
14
+ "BatchProcessor",
15
+ "BatchState",
16
+ "BatchSummary",
17
+ "BatchFileResult",
18
+ "AsyncProcessor",
19
+ ]
20
+
@@ -0,0 +1,290 @@
1
+ """
2
+ Async processor module for concurrent video processing.
3
+
4
+ Provides asynchronous video processing using asyncio and ProcessPoolExecutor
5
+ for true parallel subtitle generation across multiple videos.
6
+ """
7
+
8
+ import asyncio
9
+ import logging
10
+ import time
11
+ from concurrent.futures import ProcessPoolExecutor
12
+ from dataclasses import dataclass
13
+ from functools import partial
14
+ from pathlib import Path
15
+ from typing import Callable, List, Optional
16
+
17
+ from .batch_processor import BatchFileResult, BatchSummary
18
+ from ..utils.exceptions import SubtitleError
19
+
20
+ logger = logging.getLogger(__name__)
21
+
22
+
23
+ def _process_single_video(
24
+ file_path: str,
25
+ output_dir: str,
26
+ model: str,
27
+ output_format: str,
28
+ ) -> BatchFileResult:
29
+ """
30
+ Process a single video file in a separate process.
31
+
32
+ This function is designed to be called via ProcessPoolExecutor.
33
+ Must be at module level for pickling.
34
+
35
+ Args:
36
+ file_path: Path to the video file
37
+ output_dir: Directory for output
38
+ model: Whisper model to use
39
+ output_format: Output subtitle format
40
+
41
+ Returns:
42
+ BatchFileResult with processing outcome
43
+ """
44
+ start_time = time.time()
45
+
46
+ try:
47
+ # Import here to avoid issues with process spawning
48
+ from .transcriber import WhisperCppTranscriber
49
+ from .subtitle_gen import SubtitleGenerator
50
+ from ..models import ModelManager
51
+ from ..utils.file_handler import get_file_basename
52
+ import os
53
+
54
+ # Create generator
55
+ transcriber = WhisperCppTranscriber()
56
+ model_manager = ModelManager()
57
+ generator = SubtitleGenerator(transcriber, model_manager)
58
+
59
+ # Generate subtitles
60
+ result = generator.generate(
61
+ input_path=file_path,
62
+ model_name=model,
63
+ output_format=output_format,
64
+ output_dir=output_dir,
65
+ )
66
+
67
+ duration = time.time() - start_time
68
+
69
+ if result.success:
70
+ # Rename output to match input filename
71
+ base_name = get_file_basename(file_path)
72
+ final_output = os.path.join(output_dir, f"{base_name}.{output_format}")
73
+
74
+ if result.output_path and os.path.exists(result.output_path):
75
+ if result.output_path != final_output:
76
+ os.rename(result.output_path, final_output)
77
+ return BatchFileResult(
78
+ file_path=file_path,
79
+ success=True,
80
+ output_path=final_output,
81
+ duration_seconds=duration,
82
+ )
83
+ return BatchFileResult(
84
+ file_path=file_path,
85
+ success=True,
86
+ output_path=result.output_path,
87
+ duration_seconds=duration,
88
+ )
89
+ else:
90
+ return BatchFileResult(
91
+ file_path=file_path,
92
+ success=False,
93
+ error=result.error or "Transcription failed",
94
+ duration_seconds=duration,
95
+ )
96
+
97
+ except Exception as e:
98
+ duration = time.time() - start_time
99
+ logger.error(f"Error processing {file_path}: {e}")
100
+ return BatchFileResult(
101
+ file_path=file_path,
102
+ success=False,
103
+ error=str(e),
104
+ duration_seconds=duration,
105
+ )
106
+
107
+
108
+ class AsyncProcessor:
109
+ """
110
+ Asynchronous video processing with concurrent execution.
111
+
112
+ Uses ProcessPoolExecutor for true parallelism since video transcription
113
+ is CPU-bound. Integrates with asyncio for non-blocking concurrent operations.
114
+
115
+ Example:
116
+ async def main():
117
+ processor = AsyncProcessor(max_workers=4)
118
+ try:
119
+ results = await processor.process_multiple(
120
+ video_paths=["video1.mp4", "video2.mp4"],
121
+ model="base",
122
+ )
123
+ for result in results:
124
+ print(f"{result.file_path}: {'✓' if result.success else '✗'}")
125
+ finally:
126
+ processor.shutdown()
127
+ """
128
+
129
+ def __init__(self, max_workers: int = 4):
130
+ """
131
+ Initialize the async processor.
132
+
133
+ Args:
134
+ max_workers: Maximum number of parallel workers (default: 4)
135
+ """
136
+ self.max_workers = max(1, max_workers)
137
+ self._executor: Optional[ProcessPoolExecutor] = None
138
+
139
+ @property
140
+ def executor(self) -> ProcessPoolExecutor:
141
+ """Lazily create the executor to defer process spawning."""
142
+ if self._executor is None:
143
+ self._executor = ProcessPoolExecutor(max_workers=self.max_workers)
144
+ return self._executor
145
+
146
+ async def process_single(
147
+ self,
148
+ video_path: str,
149
+ model: str = "base",
150
+ output_format: str = "vtt",
151
+ output_dir: Optional[str] = None,
152
+ ) -> BatchFileResult:
153
+ """
154
+ Process a single video asynchronously.
155
+
156
+ Args:
157
+ video_path: Path to the video file
158
+ model: Whisper model to use
159
+ output_format: Output subtitle format
160
+ output_dir: Directory for output (default: same as input)
161
+
162
+ Returns:
163
+ BatchFileResult with processing outcome
164
+ """
165
+ if not Path(video_path).exists():
166
+ return BatchFileResult(
167
+ file_path=video_path,
168
+ success=False,
169
+ error=f"File not found: {video_path}",
170
+ )
171
+
172
+ output_dir = output_dir or str(Path(video_path).parent)
173
+
174
+ loop = asyncio.get_event_loop()
175
+ result = await loop.run_in_executor(
176
+ self.executor,
177
+ _process_single_video,
178
+ video_path,
179
+ output_dir,
180
+ model,
181
+ output_format,
182
+ )
183
+
184
+ return result
185
+
186
+ async def process_multiple(
187
+ self,
188
+ video_paths: List[str],
189
+ model: str = "base",
190
+ output_format: str = "vtt",
191
+ output_dir: Optional[str] = None,
192
+ progress_callback: Optional[Callable[[str, int, int, str], None]] = None,
193
+ ) -> BatchSummary:
194
+ """
195
+ Process multiple videos concurrently.
196
+
197
+ Args:
198
+ video_paths: List of paths to video files
199
+ model: Whisper model to use
200
+ output_format: Output subtitle format
201
+ output_dir: Directory for output (default: same as input for each file)
202
+ progress_callback: Callback(file_path, current, total, status) for progress
203
+
204
+ Returns:
205
+ BatchSummary with all processing results
206
+ """
207
+ if not video_paths:
208
+ return BatchSummary(
209
+ total_files=0,
210
+ successful=0,
211
+ failed=0,
212
+ skipped=0,
213
+ total_duration_seconds=0.0,
214
+ results=[],
215
+ )
216
+
217
+ start_time = time.time()
218
+ total = len(video_paths)
219
+ results: List[BatchFileResult] = []
220
+
221
+ # Create tasks for all videos
222
+ loop = asyncio.get_event_loop()
223
+ tasks = []
224
+
225
+ for video_path in video_paths:
226
+ file_output_dir = output_dir or str(Path(video_path).parent)
227
+ task = loop.run_in_executor(
228
+ self.executor,
229
+ _process_single_video,
230
+ video_path,
231
+ file_output_dir,
232
+ model,
233
+ output_format,
234
+ )
235
+ tasks.append((video_path, task))
236
+
237
+ # Process with progress tracking
238
+ for idx, (video_path, task) in enumerate(tasks, 1):
239
+ if progress_callback:
240
+ progress_callback(video_path, idx, total, "processing")
241
+
242
+ try:
243
+ result = await task
244
+ results.append(result)
245
+
246
+ if progress_callback:
247
+ status = "complete" if result.success else f"failed: {result.error}"
248
+ progress_callback(video_path, idx, total, status)
249
+
250
+ except Exception as e:
251
+ logger.error(f"Task failed for {video_path}: {e}")
252
+ results.append(BatchFileResult(
253
+ file_path=video_path,
254
+ success=False,
255
+ error=str(e),
256
+ ))
257
+ if progress_callback:
258
+ progress_callback(video_path, idx, total, f"failed: {e}")
259
+
260
+ total_duration = time.time() - start_time
261
+ successful = sum(1 for r in results if r.success)
262
+ failed = sum(1 for r in results if not r.success)
263
+
264
+ return BatchSummary(
265
+ total_files=total,
266
+ successful=successful,
267
+ failed=failed,
268
+ skipped=0,
269
+ total_duration_seconds=total_duration,
270
+ results=results,
271
+ )
272
+
273
+ def shutdown(self, wait: bool = True) -> None:
274
+ """
275
+ Shut down the executor and release resources.
276
+
277
+ Args:
278
+ wait: If True, wait for pending tasks to complete
279
+ """
280
+ if self._executor is not None:
281
+ self._executor.shutdown(wait=wait)
282
+ self._executor = None
283
+
284
+ async def __aenter__(self) -> "AsyncProcessor":
285
+ """Async context manager entry."""
286
+ return self
287
+
288
+ async def __aexit__(self, exc_type, exc_val, exc_tb) -> None:
289
+ """Async context manager exit with cleanup."""
290
+ self.shutdown(wait=True)