Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

enhance: throw runtime error on absent ffmpeg binary (#3850)

Ahmed Awelkair A7584cda4191a
diff --git a/camel/toolkits/video_download_toolkit.py b/camel/toolkits/video_download_toolkit.py
index d7ea8c422..06df19163 100644
--- a/camel/toolkits/video_download_toolkit.py
+++ b/camel/toolkits/video_download_toolkit.py
@@ -16,6 +16,7 @@
from __future__ import annotations
import io
+import shutil
import tempfile
from pathlib import Path
from typing import List, Optional
@@ -31,6 +32,27 @@ from camel.utils import dependencies_required
logger = get_logger(__name__)
+def _check_ffmpeg_installed() -> None:
+ r"""Check if FFmpeg is installed and available on the system.
+
+ Raises:
+ RuntimeError: If FFmpeg is not installed or not in the system PATH.
+ """
+ ffmpeg_path = shutil.which('ffmpeg')
+ if ffmpeg_path is None:
+ raise RuntimeError(
+ "FFmpeg is not installed or not found in your system PATH. "
+ "Please install FFmpeg:\n"
+ " - Windows: 'winget install ffmpeg' or download "
+ "from https://ffmpeg.org/download.html\n"
+ " - macOS: 'brew install ffmpeg'\n"
+ " - Linux: 'sudo apt install ffmpeg' or 'sudo "
+ "yum install ffmpeg'\n"
+ "After installation, restart your terminal for the "
+ "changes to take effect."
+ )
+
+
def _capture_screenshot(video_file: str, timestamp: float) -> Image.Image:
r"""Capture a screenshot from a video file at a specific timestamp.
@@ -42,6 +64,7 @@ def _capture_screenshot(video_file: str, timestamp: float) -> Image.Image:
Returns:
Image.Image: The captured screenshot in the form of Image.Image.
"""
+ _check_ffmpeg_installed()
import ffmpeg
try:
@@ -184,6 +207,7 @@ class VideoDownloaderToolkit(BaseToolkit):
Returns:
List[Image.Image]: A list of screenshots as Image.Image.
"""
+ _check_ffmpeg_installed()
import ffmpeg
parsed_url = urlparse(video_path)
diff --git a/test/toolkits/test_video_download_function.py b/test/toolkits/test_video_download_function.py
index 49dea1f4e..9db601451 100644
--- a/test/toolkits/test_video_download_function.py
+++ b/test/toolkits/test_video_download_function.py
@@ -17,7 +17,10 @@ from unittest.mock import MagicMock, mock_open, patch
import pytest
-from camel.toolkits.video_download_toolkit import VideoDownloaderToolkit
+from camel.toolkits.video_download_toolkit import (
+ VideoDownloaderToolkit,
+ _check_ffmpeg_installed,
+)
sys.modules['dotenv'] = MagicMock()
sys.modules['dotenv.main'] = MagicMock()
@@ -77,3 +80,68 @@ def test_video_screenshots_download(mock_downloader):
2,
)
assert len(screenshots) == 2
+
+
+def test_check_ffmpeg_installed_when_present():
+ r"""Test that _check_ffmpeg_installed passes when FFmpeg is installed."""
+ # Mock shutil.which to return a path (FFmpeg installed)
+ with patch('shutil.which', return_value='/usr/bin/ffmpeg'):
+ # Should not raise any exception
+ _check_ffmpeg_installed()
+
+
+def test_check_ffmpeg_installed_when_missing():
+ r"""Test that _check_ffmpeg_installed raises RuntimeError when FFmpeg is
+ missing.
+ """
+ # Mock shutil.which to return None (FFmpeg not installed)
+ with patch('shutil.which', return_value=None):
+ with pytest.raises(RuntimeError) as exc_info:
+ _check_ffmpeg_installed()
+
+ error_msg = str(exc_info.value)
+ # Verify the error message contains key information
+ assert "FFmpeg is not installed" in error_msg
+ assert "Windows" in error_msg
+ assert "winget install ffmpeg" in error_msg
+ assert "macOS" in error_msg
+ assert "brew install ffmpeg" in error_msg
+ assert "Linux" in error_msg
+ assert (
+ "apt install ffmpeg" in error_msg
+ or "yum install ffmpeg" in error_msg
+ )
+ assert "https://ffmpeg.org/download.html" in error_msg
+
+
+def test_video_screenshots_raises_error_when_ffmpeg_missing():
+ r"""Test that get_video_screenshots raises error when FFmpeg is missing."""
+ # Mock shutil.which to return None (FFmpeg not installed)
+ # Mock ffmpeg module to avoid import errors
+ with (
+ patch('shutil.which', return_value=None),
+ patch.dict('sys.modules', {'ffmpeg': MagicMock()}),
+ ):
+ toolkit = VideoDownloaderToolkit()
+ with pytest.raises(RuntimeError) as exc_info:
+ toolkit.get_video_screenshots("test.mp4", 2)
+
+ error_msg = str(exc_info.value)
+ assert "FFmpeg is not installed" in error_msg
+
+
+def test_capture_screenshot_raises_error_when_ffmpeg_missing():
+ r"""Test that _capture_screenshot raises error when FFmpeg is missing."""
+ from camel.toolkits.video_download_toolkit import _capture_screenshot
+
+ # Mock shutil.which to return None (FFmpeg not installed)
+ # Mock ffmpeg module to avoid import errors
+ with (
+ patch('shutil.which', return_value=None),
+ patch.dict('sys.modules', {'ffmpeg': MagicMock()}),
+ ):
+ with pytest.raises(RuntimeError) as exc_info:
+ _capture_screenshot("test.mp4", 1.0)
+
+ error_msg = str(exc_info.value)
+ assert "FFmpeg is not installed" in error_msg