Wednesday, May 11, 2016

mp3split - How to split huge MP3 files in several parts

Let's say that you have to split an MP3 file in several MP3 files equally long,
for example to split an MP3 file long 60 minutes, in 6 MP3 files 10 minutes each.

To help you there is mp3split that is a script initially written by Alec Jacbson, later updated and published in github.

#!/bin/bash
echo "----------------------------------------------"
big="$1"
#minutes_block=20
minutes_block="$2"
duration_stamp=$(ffmpeg -i "$big" 2>&1 | grep Duration | sed 's/^.*Duration: *\([^ ,]*\),.*/\1/g')
title=$(ffmpeg -i "$big" 2>&1  | grep "title *:" | sed 's/^.*title *: *\(.*\)/\1/g')
# get minutes as a raw integer number (rounded up)
prefix=$(basename "$big" .mp3)
echo "Total audio duration =" $duration_stamp
echo "----------------------------------------------"
mins=$(echo "$duration_stamp" | sed 's/\([0-9]*\):\([0-9]*\):\([0-9]*\)\.\([0-9]*\)/\1*60+\2+\3\/60+\4\/60\/100/g' | bc -l | python -c "import math; print int(math.ceil(float(raw_input())))")
echo "Total audio duration in minutes =" $mins
echo "----------------------------------------------"
ss="0"
count="1"
total_count=$(echo "$mins/$minutes_block"+1 | bc)
echo $total_count
while [ "$ss" -lt "$mins" ]
do
  zcount=$(printf "%05d" $count)
  ss_hours=$(echo "$ss/60" | bc)
  ss_mins=$(echo "$ss%60" | bc)
  ss_stamp=$(printf "%02d:%02d:00" $ss_hours $ss_mins)
  ffmpeg -i "$big" -acodec copy -t 00:$minutes_block:00 -ss $ss_stamp -metadata track="$count/$total_count" -metadata title="$title $zcount" "$prefix-$zcount.mp3" 
  echo $zcount $ss_hour $ss_mins $ss_stamp
  ss=$[$ss+$minutes_block]
  count=$[$count+1]
done 
echo "----------------------------------------------" 
echo "SPLIT PROCESS COMPLETED"
echo "----------------------------------------------" 

Dependencies: python ffmpeg

How to execute: 
./mp3split.sh NAME_OF_THE_FILE MINUTES_CHUNK

Example: 
./mp3split.sh mysixtyminutesfile.mp3 10

It will produce several files like (keeping the original MP3):
mysixtyminutesfile-00001.mp3
mysixtyminutesfile-00002.mp3
mysixtyminutesfile-00003.mp3
...

AddToAny