Last active
April 30, 2026 15:52
-
-
Save rathorevaibhav/2a3fb09d9cab9e05604cd923bc9b9269 to your computer and use it in GitHub Desktop.
audio-chunk
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 | |
| # Split an audio file into chunks of ~20MB each | |
| # Usage: ./audio-chunk.sh [-s size_mb] [-o output_dir] audio_file | |
| # -s <size> Chunk size in MB (default: 20) | |
| # -o <dir> Output directory (default: ./chunks/) | |
| chunk_size_mb=20 | |
| outdir="" | |
| while getopts "s:o:" opt; do | |
| case $opt in | |
| s) chunk_size_mb="$OPTARG" ;; | |
| o) outdir="$OPTARG" ;; | |
| *) echo "Usage: $0 [-s size_mb] [-o output_dir] audio_file"; exit 1 ;; | |
| esac | |
| done | |
| shift $((OPTIND - 1)) | |
| if [ $# -eq 0 ]; then | |
| echo "Usage: $0 [-s size_mb] [-o output_dir] audio_file" | |
| exit 1 | |
| fi | |
| file="$1" | |
| if [ ! -f "$file" ]; then | |
| echo "Error: '$file' not found" | |
| exit 1 | |
| fi | |
| # Default output dir | |
| if [ -z "$outdir" ]; then | |
| outdir="$(pwd)/chunks" | |
| fi | |
| mkdir -p "$outdir" | |
| base="$(basename "${file%.*}")" | |
| ext="${file##*.}" | |
| # Get file size and duration to calculate bitrate, then derive chunk duration | |
| file_size=$(stat -f%z "$file" 2>/dev/null || stat -c%s "$file" 2>/dev/null) | |
| duration=$(ffprobe -v error -show_entries format=duration -of csv=p=0 "$file" 2>/dev/null) | |
| if [ -z "$duration" ] || [ -z "$file_size" ]; then | |
| echo "Error: could not read file metadata" | |
| exit 1 | |
| fi | |
| chunk_size_bytes=$((chunk_size_mb * 1024 * 1024)) | |
| # If file is already small enough, just copy it | |
| if [ "$file_size" -le "$chunk_size_bytes" ]; then | |
| cp "$file" "$outdir/${base}_001.${ext}" | |
| echo "$outdir/${base}_001.${ext}" | |
| exit 0 | |
| fi | |
| # Calculate chunk duration from ratio: chunk_duration = duration * (chunk_size / file_size) | |
| chunk_duration=$(python3 -c "print(int(float($duration) * ($chunk_size_bytes / $file_size)))") | |
| echo "Splitting '$file' into ~${chunk_size_mb}MB chunks (~${chunk_duration}s each)..." >&2 | |
| ffmpeg -v warning -i "$file" -f segment -segment_time "$chunk_duration" \ | |
| -segment_start_number 1 -c copy -reset_timestamps 1 \ | |
| "$outdir/${base}_%03d.${ext}" | |
| # Print chunk paths (sorted) to stdout for callers to consume | |
| for chunk in "$outdir/${base}_"*."${ext}"; do | |
| echo "$chunk" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment