65 lines
2.1 KiB
Python
65 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import shutil
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from video_render.config import Settings
|
|
from video_render.ffmpeg import extract_audio_to_wav
|
|
from video_render.utils import ensure_workspace, remove_paths, sanitize_filename
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass
|
|
class VideoWorkspace:
|
|
original_filename: str
|
|
sanitized_name: str
|
|
workspace_dir: Path
|
|
output_dir: Path
|
|
source_path: Path
|
|
working_video_path: Path
|
|
audio_path: Path
|
|
|
|
|
|
class MediaPreparer:
|
|
def __init__(self, settings: Settings) -> None:
|
|
self.settings = settings
|
|
|
|
def prepare(self, filename: str) -> VideoWorkspace:
|
|
source_path = self.settings.videos_dir / filename
|
|
if not source_path.exists():
|
|
raise FileNotFoundError(f"Arquivo de vídeo não encontrado: {source_path}")
|
|
|
|
sanitized_name = sanitize_filename(Path(filename).stem)
|
|
workspace_dir = ensure_workspace(self.settings.videos_dir, sanitized_name)
|
|
|
|
existing_children = list(workspace_dir.iterdir())
|
|
if existing_children:
|
|
logger.info("Limpando workspace existente para %s", sanitized_name)
|
|
remove_paths(existing_children)
|
|
|
|
destination_name = f"{sanitized_name}{source_path.suffix.lower()}"
|
|
working_video_path = workspace_dir / destination_name
|
|
shutil.copy2(source_path, working_video_path)
|
|
logger.info("Cópia do vídeo criada em %s", working_video_path)
|
|
|
|
output_dir = ensure_workspace(self.settings.outputs_dir, sanitized_name)
|
|
existing_outputs = list(output_dir.iterdir())
|
|
if existing_outputs:
|
|
remove_paths(existing_outputs)
|
|
|
|
audio_path = workspace_dir / "audio.wav"
|
|
extract_audio_to_wav(working_video_path, audio_path)
|
|
|
|
return VideoWorkspace(
|
|
original_filename=filename,
|
|
sanitized_name=sanitized_name,
|
|
workspace_dir=workspace_dir,
|
|
output_dir=output_dir,
|
|
source_path=source_path,
|
|
working_video_path=working_video_path,
|
|
audio_path=audio_path,
|
|
)
|