Added split audio bash script

This commit is contained in:
Samuel Ortion 2022-07-03 08:00:07 +02:00
parent 407a7b0477
commit 0d019d3b6f

53
src/split_audio.sh Normal file
View File

@ -0,0 +1,53 @@
#!/bin/bash
# Split audio file into multiple files
# By Samuel ORTION
# On: 2022-07-03
div() {
round=$(awk "BEGIN { rounded = sprintf(\"%.0f\", $1/$2); print rounded } ")
echo $round
}
set -e
PROGRAM_NAME=$0
usage()
{
echo "Usage: $PROGRAM_NAME <input_file> <size_limit>"
echo "Example: $PROGRAM_NAME input.wav 300"
echo "The size limit is in MB"
}
if [ $# -ne 2 ]; then
usage
exit 1
fi
# Get optionsLS
INPUT_FILE=$1
SIZE_LIMIT=$2
FILENAME=$(basename $INPUT_FILE .wav)
OUTPUT_DIR=$FILENAME
# Check if input file exists
if [ ! -f $INPUT_FILE ]; then
echo "Input file $INPUT_FILE does not exist"
exit 1
fi
# Create an output directory
mkdir -p $OUTPUT_DIR
# Get file size and duration and compute chunk number
FILE_SIZE=$(stat -c%s "$INPUT_FILE")
FILE_SIZE_MB=$(($FILE_SIZE / 1024 / 1024))
AUDIO_DURATION=$(soxi -D "$INPUT_FILE")
CHUNK_NUMBER=$(div $FILE_SIZE_MB $SIZE_LIMIT)
CHUNK_DURATION=$(div $AUDIO_DURATION $CHUNK_NUMBER)
echo $CHUNK_DURATION
# CHUNK_DURATION=$(($AUDIO_DURATION / $CHUNK_NUMBER))
ffmpeg -i $INPUT_FILE -f segment -segment_time $CHUNK_DURATION -c copy $OUTPUT_DIR/${FILENAME}_%03d.wav