-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_ffmpeg.py
More file actions
226 lines (184 loc) · 8.09 KB
/
setup_ffmpeg.py
File metadata and controls
226 lines (184 loc) · 8.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import hashlib
import os
import platform
import shutil
import tarfile
import tempfile
import urllib.request
import zipfile
from pathlib import Path
from typing import Optional
ALLOW_UNVERIFIED_DOWNLOADS = os.getenv(
"PIXEL_FORGE_ALLOW_UNVERIFIED_DOWNLOADS",
"",
).strip().lower() in {"1", "true", "yes", "on"}
WINDOWS_FFMPEG_URL = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"
WINDOWS_FFMPEG_SHA256_URL = f"{WINDOWS_FFMPEG_URL}.sha256"
LINUX_FFMPEG_URL = "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz"
LINUX_FFMPEG_MD5_URL = f"{LINUX_FFMPEG_URL}.md5"
MACOS_FFMPEG_URL = "https://evermeet.cx/ffmpeg/getrelease/zip"
MACOS_FFPROBE_URL = "https://evermeet.cx/ffprobe/getrelease/zip"
def _download_text(url: str) -> str:
with urllib.request.urlopen(url) as response:
return response.read().decode("utf-8")
def _extract_expected_digest(checksum_text: str, algorithm: str, filename: Optional[str] = None) -> str:
digest_lengths = {"sha256": 64, "md5": 32}
digest_length = digest_lengths[algorithm]
for line in checksum_text.splitlines():
if filename and filename not in line:
continue
tokens = line.replace("*", " ").split()
for token in tokens:
normalized = token.strip().lower()
if len(normalized) == digest_length and all(ch in "0123456789abcdef" for ch in normalized):
return normalized
raise ValueError(f"Unable to parse {algorithm} digest for {filename or 'download'}")
def _calculate_file_digest(file_path: Path, algorithm: str) -> str:
digest = hashlib.new(algorithm)
with open(file_path, "rb") as source:
while chunk := source.read(1024 * 1024):
digest.update(chunk)
return digest.hexdigest()
def _verify_download(
destination: Path,
*,
algorithm: str,
checksum_url: Optional[str] = None,
expected_digest: Optional[str] = None,
) -> None:
resolved_digest = expected_digest.lower() if expected_digest else None
if resolved_digest is None and checksum_url:
checksum_text = _download_text(checksum_url)
resolved_digest = _extract_expected_digest(checksum_text, algorithm, destination.name)
if resolved_digest is None:
if ALLOW_UNVERIFIED_DOWNLOADS:
print(f"Warning: integrity verification skipped for {destination.name}")
return
raise ValueError(
f"No integrity metadata available for {destination.name}. "
"Set a checksum or PIXEL_FORGE_ALLOW_UNVERIFIED_DOWNLOADS=1 to bypass."
)
actual_digest = _calculate_file_digest(destination, algorithm)
if actual_digest != resolved_digest:
raise ValueError(
f"Integrity verification failed for {destination.name}: "
f"expected {resolved_digest}, got {actual_digest}"
)
print(f"Verified {algorithm} for {destination.name}")
def _download_file(url: str, destination: Path) -> None:
print(f"Downloading from {url}...")
urllib.request.urlretrieve(url, str(destination))
def _extract_from_zip(archive_path: Path, targets: dict[str, Path]) -> set[str]:
extracted: set[str] = set()
with zipfile.ZipFile(archive_path, "r") as zip_ref:
for member in zip_ref.namelist():
normalized = member.rstrip("/")
for binary_name, output_path in targets.items():
if normalized == binary_name or normalized.endswith(f"/{binary_name}"):
print(f"Extracting {normalized} -> {output_path.name}")
with zip_ref.open(member) as source, open(output_path, "wb") as target:
shutil.copyfileobj(source, target)
extracted.add(binary_name)
return extracted
def _extract_from_tar(archive_path: Path, targets: dict[str, Path]) -> set[str]:
extracted: set[str] = set()
with tarfile.open(archive_path, "r:xz") as tar_ref:
for member in tar_ref.getmembers():
for binary_name, output_path in targets.items():
if member.name.endswith(f"/{binary_name}"):
print(f"Extracting {member.name} -> {output_path.name}")
source = tar_ref.extractfile(member)
if source is None:
continue
with source, open(output_path, "wb") as target:
shutil.copyfileobj(source, target)
extracted.add(binary_name)
return extracted
def _mark_executable_if_needed(system: str, *files: Path) -> None:
if system == "Windows":
return
for file_path in files:
if file_path.exists():
st = os.stat(file_path)
os.chmod(file_path, st.st_mode | 0o111)
def download_ffmpeg():
system = platform.system()
machine = platform.machine()
print(f"Detecting system: {system} {machine}")
base_dir = Path(__file__).parent
bin_dir = base_dir / "bin"
bin_dir.mkdir(exist_ok=True)
ffmpeg_exe = bin_dir / "ffmpeg.exe"
ffprobe_exe = bin_dir / "ffprobe.exe"
ffmpeg_binary_name = "ffmpeg.exe"
ffprobe_binary_name = "ffprobe.exe"
if system != "Windows":
ffmpeg_exe = bin_dir / "ffmpeg"
ffprobe_exe = bin_dir / "ffprobe"
ffmpeg_binary_name = "ffmpeg"
ffprobe_binary_name = "ffprobe"
if ffmpeg_exe.exists() and ffprobe_exe.exists():
print(f"FFmpeg and FFprobe already exist in {bin_dir}")
return
try:
with tempfile.TemporaryDirectory(prefix="pixel-forge-ffmpeg-") as temp_dir:
temp_dir_path = Path(temp_dir)
if system == "Windows":
archive = temp_dir_path / "ffmpeg-win.zip"
_download_file(WINDOWS_FFMPEG_URL, archive)
_verify_download(
archive,
algorithm="sha256",
checksum_url=WINDOWS_FFMPEG_SHA256_URL,
)
_extract_from_zip(archive, {
ffmpeg_binary_name: ffmpeg_exe,
ffprobe_binary_name: ffprobe_exe,
})
elif system == "Linux":
archive = temp_dir_path / "ffmpeg-linux.tar.xz"
_download_file(LINUX_FFMPEG_URL, archive)
_verify_download(
archive,
algorithm="md5",
checksum_url=LINUX_FFMPEG_MD5_URL,
)
_extract_from_tar(archive, {
ffmpeg_binary_name: ffmpeg_exe,
ffprobe_binary_name: ffprobe_exe,
})
elif system == "Darwin":
ffmpeg_archive = temp_dir_path / "ffmpeg-macos.zip"
ffprobe_archive = temp_dir_path / "ffprobe-macos.zip"
# evermeet publishes ffmpeg and ffprobe in separate archives
_download_file(MACOS_FFMPEG_URL, ffmpeg_archive)
_verify_download(
ffmpeg_archive,
algorithm="sha256",
expected_digest=os.getenv("PIXEL_FORGE_FFMPEG_MACOS_SHA256"),
)
_extract_from_zip(ffmpeg_archive, {ffmpeg_binary_name: ffmpeg_exe})
_download_file(MACOS_FFPROBE_URL, ffprobe_archive)
_verify_download(
ffprobe_archive,
algorithm="sha256",
expected_digest=os.getenv("PIXEL_FORGE_FFPROBE_MACOS_SHA256"),
)
_extract_from_zip(ffprobe_archive, {ffprobe_binary_name: ffprobe_exe})
else:
print(f"Unsupported system: {system}")
return
_mark_executable_if_needed(system, ffmpeg_exe, ffprobe_exe)
missing = []
if not ffmpeg_exe.exists():
missing.append("ffmpeg")
if not ffprobe_exe.exists():
missing.append("ffprobe")
if missing:
print(f"FFmpeg installation incomplete. Missing: {', '.join(missing)}")
else:
print(f"FFmpeg tools installed to {bin_dir}")
except Exception as e:
print(f"Error installing FFmpeg: {e}")
if __name__ == "__main__":
download_ffmpeg()