election-game/art/music-opus.sh

16 lines
982 B
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# 背景音樂的 .ogg 改成 Opus 96 kbps(原本是 150–320 kbps 的 Vorbis,五首 10 MB → 5 MB;Chrome、Firefox 用這個,Safari 用 .mp3)。
# 先轉成 48 kHz 的 WAV 再壓:直接壓的話 ffmpeg 有時會在結尾多出幾十個取樣,迴圈會有小空隙。已經是 Opus 的檔案跳過。
# 用法:art/music-opus.sh(web/src/game/audio/engine.ts 依瀏覽器支不支援 Ogg Opus 挑 .ogg 或 .mp3)
set -eu
music="$(cd "$(dirname "$0")/../web/public/assets/music" && pwd)"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
for f in "$music"/*.ogg; do
[[ "$(ffprobe -v error -show_entries stream=codec_name -of csv=p=0 "$f")" == opus ]] && { echo "跳過(已經是 Opus)$(basename "$f")"; continue; }
ffmpeg -loglevel error -y -i "$f" -af aresample=48000 -c:a pcm_s16le "$tmp/a.wav"
ffmpeg -loglevel error -y -i "$tmp/a.wav" -c:a libopus -b:a 96k -vbr on "$tmp/a.ogg"
mv "$tmp/a.ogg" "$f"
echo "ok $(basename "$f")"
done