Created
December 13, 2025 10:50
-
-
Save illuzor/de90216b016a90777823102e61c29c9c to your computer and use it in GitHub Desktop.
Simple script to convert mp3 files to aac with size limit (generated by chatgpt)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| LIMIT_MB=199 | |
| LIMIT_BYTES=$((LIMIT_MB * 1024 * 1024)) | |
| JOBS=8 | |
| mkdir -p out | |
| convert_file() { | |
| local f="$1" | |
| local size_bytes | |
| size_bytes=$(stat -f%z "$f") | |
| if (( size_bytes <= LIMIT_BYTES )); then | |
| echo "OK (copy): $f" | |
| cp "$f" "out/$f" | |
| return | |
| fi | |
| local duration | |
| duration=$(ffprobe -v error -show_entries format=duration \ | |
| -of default=noprint_wrappers=1:nokey=1 "$f") | |
| local bitrate | |
| bitrate=$(echo "$LIMIT_BYTES * 8 / $duration / 1000 * 0.95" | bc) | |
| local out_file="out/${f%.mp3}.m4a" | |
| while true; do | |
| echo | |
| echo "Encode: $f → ${bitrate} kbps" | |
| ffmpeg -y -i "$f" \ | |
| -map_metadata 0 -vn \ | |
| -c:a aac_at \ | |
| -b:a ${bitrate}k \ | |
| -stats -nostdin \ | |
| "$out_file" | |
| local out_size | |
| out_size=$(stat -f%z "$out_file") | |
| if (( out_size <= LIMIT_BYTES )); then | |
| echo "OK: $f ($(echo "$out_size/1024/1024" | bc) MB)" | |
| break | |
| fi | |
| echo "Too big, retry..." | |
| bitrate=$(echo "$bitrate * 0.95" | bc) | |
| done | |
| } | |
| export -f convert_file | |
| export LIMIT_BYTES | |
| running=0 | |
| for f in *.mp3; do | |
| [ -f "$f" ] || continue | |
| convert_file "$f" & | |
| ((running++)) | |
| if (( running >= JOBS )); then | |
| wait -n | |
| ((running--)) | |
| fi | |
| done | |
| wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment