forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBba75450Tahinli5 days ago3985 commits

aac-encode-score.sh69 lines, 2.7 KiBHistoryRawDownload
1#!/usr/bin/env bash
2# aac-encode-score.sh: encode reference clips with ffmpeg's native AAC encoder and with ec-aac,
3# decode both with ffmpeg, and print objective metrics (scripts/aac_score.py).
4#
5# Usage:
6# scripts/aac-encode-score.sh clips <dir> <src>... # make 10 s, 48 kHz stereo f32le clips
7# scripts/aac-encode-score.sh encode <dir> # encode + decode every clip (runs on a server)
8# scripts/aac-encode-score.sh score <dir> # print the metrics table (local, numpy)
9#
10# Clip files are <dir>/clips/<name>.f32le (stereo) and <dir>/clips/<name>-51.f32le (5.1).
11# Outputs go to <dir>/out/. Configs: stereo 64, 128 and 256 kbps; 5.1 at 384 kbps.
12set -euo pipefail
13
14here=$(cd "$(dirname "$0")/.." && pwd)
15cmd=${1:?usage: aac-encode-score.sh clips|encode|score <dir> ...}
16dir=${2:?missing <dir>}
17mkdir -p "$dir/clips" "$dir/out"
18
19case $cmd in
20clips)
21 shift 2
22 i=0
23 for src in "$@"; do
24 i=$((i + 1))
25 name=clip$i
26 ffmpeg -v error -y -ss 30 -t 10 -i "$src" -ac 2 -ar 48000 -f f32le "$dir/clips/$name.f32le"
27 # Synthetic 5.1 from the stereo clip: front L/R, centre, LFE from the mix, rears from L/R.
28 ffmpeg -v error -y -f f32le -ar 48000 -ac 2 -i "$dir/clips/$name.f32le" \
29 -af "pan=5.1|FL=c0|FR=c1|FC=0.5*c0+0.5*c1|LFE=0.1*c0+0.1*c1|BL=0.3*c0|BR=0.3*c1" \
30 -ac 6 -ar 48000 -f f32le "$dir/clips/$name-51.f32le"
31 done
32 ;;
33encode)
34 # Runs on the server through remote-run; needs ffmpeg and cargo, not numpy.
35 cd "$here"
36 cargo build --release -q -p ec-aac --example aacenc_file
37 bin=$(cargo metadata --format-version 1 --no-deps | python3 -c 'import json,sys;print(json.load(sys.stdin)["target_directory"])')/release/examples/aacenc_file
38 for clip in "$dir"/clips/*.f32le; do
39 base=$(basename "$clip" .f32le)
40 case $base in *-51) ch=6; configs="384" ;; *) ch=2; configs="64 128 256" ;; esac
41 for k in $configs; do
42 tag=$base-$k
43 ffmpeg -v error -y -f f32le -ar 48000 -ac $ch -i "$clip" -c:a aac -b:a "${k}k" -f adts "$dir/out/$tag.ff.aac"
44 "$bin" "$clip" $ch 48000 "$k" "$dir/out/$tag.ec.aac"
45 for enc in ff ec; do
46 ffmpeg -v error -y -i "$dir/out/$tag.$enc.aac" -f f32le -ar 48000 -ac $ch "$dir/out/$tag.$enc.dec.f32le"
47 done
48 echo "encoded $tag"
49 done
50 done
51 ;;
52score)
53 for clip in "$dir"/clips/*.f32le; do
54 base=$(basename "$clip" .f32le)
55 case $base in *-51) ch=6; configs="384" ;; *) ch=2; configs="64 128 256" ;; esac
56 for k in $configs; do
57 tag=$base-$k
58 echo "== $tag"
59 python3 "$here/scripts/aac_score.py" "$clip" $ch \
60 "ffmpeg=$dir/out/$tag.ff.dec.f32le:$dir/out/$tag.ff.aac" \
61 "ec-aac=$dir/out/$tag.ec.dec.f32le:$dir/out/$tag.ec.aac"
62 done
63 done
64 ;;
65*)
66 echo "unknown command: $cmd" >&2
67 exit 2
68 ;;
69esac