|
4 | 4 | import subprocess |
5 | 5 | import time |
6 | 6 | from collections import Counter |
| 7 | +from concurrent.futures import ThreadPoolExecutor |
7 | 8 | from dataclasses import asdict, dataclass |
8 | 9 | from pathlib import Path |
9 | 10 |
|
| 11 | +from tqdm import tqdm |
| 12 | + |
10 | 13 | from balatrollm.config import Config |
11 | 14 | from balatrollm.data_collection import Stats |
12 | 15 |
|
@@ -228,26 +231,43 @@ def compute_models_leaderboard( |
228 | 231 | entries.append(ModelStats(**stats_dict)) |
229 | 232 | return ModelsLeaderboard(generated_at=int(time.time()), entries=entries) |
230 | 233 |
|
231 | | - def convert_pngs_to_avif(self, directory: Path) -> None: |
232 | | - """Convert all PNG files in directory to AVIF using cavif.""" |
| 234 | + def _convert_single_png(self, png_file: Path) -> None: |
| 235 | + """Convert a single PNG file to AVIF.""" |
233 | 236 | try: |
234 | | - result = subprocess.run( |
235 | | - "find . -name 'screenshot.png' | xargs -P 4 -I {} cavif --overwrite --quality=60 --speed=1 --quiet {}", |
236 | | - cwd=directory, |
| 237 | + subprocess.run( |
| 238 | + [ |
| 239 | + "cavif", |
| 240 | + "--overwrite", |
| 241 | + "--quality=60", |
| 242 | + "--speed=1", |
| 243 | + "--quiet", |
| 244 | + str(png_file), |
| 245 | + ], |
237 | 246 | capture_output=True, |
238 | 247 | text=True, |
239 | | - shell=True, |
| 248 | + check=True, |
240 | 249 | ) |
241 | | - if result.returncode != 0: |
242 | | - print( |
243 | | - f"Warning: cavif conversion failed in {directory}: {result.stderr}" |
| 250 | + png_file.unlink() |
| 251 | + except subprocess.CalledProcessError as e: |
| 252 | + print(f"Warning: cavif conversion failed for {png_file}: {e.stderr}") |
| 253 | + except OSError as e: |
| 254 | + print(f"Warning: Could not remove {png_file}: {e}") |
| 255 | + |
| 256 | + def convert_pngs_to_avif(self, directory: Path) -> None: |
| 257 | + """Convert all PNG files in directory to AVIF using cavif with parallelization.""" |
| 258 | + try: |
| 259 | + png_files = list(directory.rglob("screenshot.png")) |
| 260 | + if not png_files: |
| 261 | + return |
| 262 | + |
| 263 | + with ThreadPoolExecutor(max_workers=8) as executor: |
| 264 | + list( |
| 265 | + tqdm( |
| 266 | + executor.map(self._convert_single_png, png_files), |
| 267 | + total=len(png_files), |
| 268 | + desc="Converting to AVIF", |
| 269 | + ) |
244 | 270 | ) |
245 | | - else: |
246 | | - for png_file in directory.rglob("screenshot.png"): |
247 | | - try: |
248 | | - png_file.unlink() |
249 | | - except OSError as e: |
250 | | - print(f"Warning: Could not remove {png_file}: {e}") |
251 | 271 | except FileNotFoundError: |
252 | 272 | print("Warning: cavif not found, keeping PNG format") |
253 | 273 | except Exception as e: |
|
0 commit comments