Created
March 4, 2026 01:39
-
-
Save kirkegaard/769d3f589060a88c075ae1e980077b64 to your computer and use it in GitHub Desktop.
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
| ffcompress () { | |
| local TARGET_SIZE="$1" | |
| local INPUT="$2" | |
| if [[ -z "$TARGET_SIZE" || -z "$INPUT" ]]; then | |
| echo "Usage: ffcompress 10mb input.mp4" | |
| return 1 | |
| fi | |
| # Strip non-numeric characters | |
| local SIZE_MB=${TARGET_SIZE//[^0-9.]/} | |
| # Get duration | |
| local DURATION=$(ffprobe -v error -show_entries format=duration \ | |
| -of default=noprint_wrappers=1:nokey=1 "$INPUT") | |
| # Target slightly under requested size | |
| local TARGET_MB=$(echo "$SIZE_MB * 0.98" | bc) | |
| # Total bitrate in kbps | |
| local TOTAL_BITRATE=$(echo "($TARGET_MB * 8192) / $DURATION" | bc) | |
| # Bitrate split | |
| local AUDIO_BITRATE=96 | |
| local VIDEO_BITRATE=$(echo "$TOTAL_BITRATE - $AUDIO_BITRATE" | bc) | |
| local OUTPUT="compressed_${INPUT:t}" | |
| ffmpeg -i "$INPUT" \ | |
| -c:v libx264 -preset slow \ | |
| -b:v "${VIDEO_BITRATE}k" \ | |
| -maxrate "${VIDEO_BITRATE}k" \ | |
| -bufsize "$((VIDEO_BITRATE * 2))k" \ | |
| -c:a aac -b:a "${AUDIO_BITRATE}k" \ | |
| "$OUTPUT" | |
| echo "Done → $OUTPUT" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment