Last active
March 7, 2024 18:02
-
-
Save hernamesbarbara/78f08e035ffde31a55fe639943683cc2 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
#!/usr/bin/env bash | |
# split a long mp3 into separate mp3s of equal length | |
# args: | |
# <input_mp3>: filename of the long mp3 | |
# <n_parts>: how many shorter mp3s you want to cut the long one into | |
# ./splitmp3 long-mp3-audio.mp4 3 # -> gives you 3 output mp3s of equal length | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <input_mp3> <n_parts>" | |
exit 1 | |
fi | |
# Assign command line arguments to variables | |
input_mp3="$1" | |
n_parts="$2" | |
# Check if the file exists | |
if [ ! -f "$input_mp3" ]; then | |
echo "File does not exist: $input_mp3" | |
exit 1 | |
fi | |
# Calculate the duration of the input file | |
duration=$(ffprobe -i "$input_mp3" -show_entries format=duration -v quiet -of csv="p=0") | |
if [ $? -ne 0 ]; then | |
echo "Error determining file duration." | |
exit 1 | |
fi | |
# Calculate the duration of each part | |
part_duration=$(echo "$duration / $n_parts" | bc -l) | |
# Loop to split the file | |
for (( i=0; i<n_parts; i++ )) | |
do | |
start_time=$(echo "$i * $part_duration" | bc) | |
output_file=$(printf "%s_part%03d.mp3" "${input_mp3%.*}" $((i+1))) | |
ffmpeg -i "$input_mp3" -ss "$start_time" -t "$part_duration" -acodec copy "$output_file" | |
if [ $? -ne 0 ]; then | |
echo "Error splitting file at part $((i+1))" | |
exit 1 | |
fi | |
done | |
echo "Splitting complete." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment