|
4 | 4 | import os |
5 | 5 | import time |
6 | 6 | import sys |
| 7 | +import subprocess |
| 8 | +import shutil |
| 9 | +import tempfile |
7 | 10 | import threading |
8 | 11 | import whisper # Whisper for speech-to-text processing |
9 | 12 | import queue |
|
35 | 38 | os.environ["WHISPER_DOWNLOAD_DIR"] = os.path.join(base_path, "models") |
36 | 39 | os.environ["WHISPER_CACHE_DIR"] = os.path.join(base_path, "models") |
37 | 40 |
|
| 41 | +def extract_ffmpeg(): |
| 42 | + """Extracts the correct FFmpeg binary for the OS and returns its path.""" |
| 43 | + temp_dir = tempfile.mkdtemp() |
| 44 | + |
| 45 | + # Determine OS and archive filename |
| 46 | + if sys.platform.startswith("win"): |
| 47 | + archive_name = "ffmpeg_windows.zip" |
| 48 | + extracted_binary = "ffmpeg.exe" |
| 49 | + elif sys.platform.startswith("darwin"): |
| 50 | + archive_name = "ffmpeg_mac.zip" |
| 51 | + extracted_binary = "ffmpeg" |
| 52 | + elif sys.platform.startswith("linux"): |
| 53 | + archive_name = "ffmpeg_ubuntu.zip" |
| 54 | + extracted_binary = "ffmpeg" |
| 55 | + else: |
| 56 | + raise RuntimeError("Unsupported OS") |
| 57 | + |
| 58 | + # Paths |
| 59 | + archive_path = os.path.join(os.path.dirname(__file__), "ffmpeg", archive_name) |
| 60 | + extracted_path = os.path.join(temp_dir, extracted_binary) |
| 61 | + |
| 62 | + # Extract using shutil (built-in support for ZIP) |
| 63 | + try: |
| 64 | + print(f"Extracting {archive_name}...") |
| 65 | + shutil.unpack_archive(archive_path, temp_dir) |
| 66 | + |
| 67 | + # Ensure correct executable permissions (Linux/macOS) |
| 68 | + if not sys.platform.startswith("win"): |
| 69 | + os.chmod(extracted_path, 0o755) |
| 70 | + |
| 71 | + print(f"FFmpeg extracted to: {extracted_path}") |
| 72 | + return extracted_path |
| 73 | + |
| 74 | + except Exception as e: |
| 75 | + print(f"Error extracting FFmpeg: {e}") |
| 76 | + sys.exit(1) |
| 77 | + |
| 78 | +# Extract and set the FFmpeg path |
| 79 | +FFMPEG_PATH = extract_ffmpeg() |
| 80 | + |
| 81 | +# Test if FFmpeg works |
| 82 | +try: |
| 83 | + subprocess.run([FFMPEG_PATH, "-version"], check=True) |
| 84 | + print("FFmpeg is working correctly.") |
| 85 | +except Exception as e: |
| 86 | + print(f"Error running FFmpeg: {e}") |
| 87 | + |
38 | 88 | # Create if it doesn't exist |
39 | 89 | if not os.path.exists(os.environ["WHISPER_DOWNLOAD_DIR"]): |
40 | 90 | os.makedirs(os.environ["WHISPER_DOWNLOAD_DIR"]) |
|
0 commit comments