Skip to content

Commit 2e3be75

Browse files
committed
perf: improve conversion to AVIF
1 parent 97b5b04 commit 2e3be75

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

src/balatrollm/benchmark.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import subprocess
55
import time
66
from collections import Counter
7+
from concurrent.futures import ThreadPoolExecutor
78
from dataclasses import asdict, dataclass
89
from pathlib import Path
910

11+
from tqdm import tqdm
12+
1013
from balatrollm.config import Config
1114
from balatrollm.data_collection import Stats
1215

@@ -228,26 +231,43 @@ def compute_models_leaderboard(
228231
entries.append(ModelStats(**stats_dict))
229232
return ModelsLeaderboard(generated_at=int(time.time()), entries=entries)
230233

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."""
233236
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+
],
237246
capture_output=True,
238247
text=True,
239-
shell=True,
248+
check=True,
240249
)
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+
)
244270
)
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}")
251271
except FileNotFoundError:
252272
print("Warning: cavif not found, keeping PNG format")
253273
except Exception as e:

0 commit comments

Comments
 (0)