refactor: Improve ffmpeg_downloader module

- Enhanced Docker compatibility with proper permissions
- Better error handling and logging
- Improved download and extraction process
- Added detailed type hints and documentation
This commit is contained in:
pacnpal
2024-11-14 22:37:42 +00:00
parent 2f449e6120
commit 2ba89edbd9

View File

@@ -10,6 +10,8 @@ import subprocess
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from contextlib import contextmanager from contextlib import contextmanager
from typing import Optional
from .exceptions import DownloadError from .exceptions import DownloadError
logger = logging.getLogger("VideoArchiver") logger = logging.getLogger("VideoArchiver")
@@ -62,6 +64,7 @@ class FFmpegDownloader:
} }
def __init__(self, system: str, machine: str, base_dir: Path): def __init__(self, system: str, machine: str, base_dir: Path):
"""Initialize FFmpeg downloader"""
self.system = system self.system = system
self.machine = machine.lower() self.machine = machine.lower()
if self.machine == "arm64": if self.machine == "arm64":
@@ -112,14 +115,17 @@ class FFmpegDownloader:
archive_path = Path(temp_dir) / f"ffmpeg_archive{'.zip' if self.system == 'Windows' else '.tar.xz'}" archive_path = Path(temp_dir) / f"ffmpeg_archive{'.zip' if self.system == 'Windows' else '.tar.xz'}"
logger.info(f"Downloading FFmpeg from {url}") logger.info(f"Downloading FFmpeg from {url}")
response = requests.get(url, stream=True, timeout=30) try:
response.raise_for_status() response = requests.get(url, stream=True, timeout=30)
response.raise_for_status()
with open(archive_path, "wb") as f:
for chunk in response.iter_content(chunk_size=8192): with open(archive_path, "wb") as f:
f.write(chunk) for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return archive_path
return archive_path
except Exception as e:
raise DownloadError(f"Failed to download FFmpeg: {str(e)}")
def _extract_binary(self, archive_path: Path, temp_dir: str): def _extract_binary(self, archive_path: Path, temp_dir: str):
"""Extract FFmpeg binary from archive""" """Extract FFmpeg binary from archive"""