Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a directory of Radio Mystery Theatres, and I love them! But at 45 mins a piece, I always fall asleep somewhere in the one I'm listening to at night, and finding my way back the next day is very difficult!
Is there anything that could say "split all MP3s in xxx dir into 15 minute pieces"? I am pretty sure I can do them one at a time in the GUI, but if I could automate it, that would be real nice.
You could do it with ffmpeg, provided you compile it with lame (libmp3lame) support:
Code:
#!/bin/sh
# Simple script to split a long mp3 file in several SPLICE_MIN minutes splices.
# Splice duration (in minutes)
SPLICE_MIN=15
# Output directory for split files
OUTDIR=splitmp3s
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~ EDITABLE AREA END ~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate seconds from SPLICE_MIN
SPLICE=$((SPLICE_MIN*60))
# Arguments check
if [ $# -eq 0 ]
then
cat << EOF
Simple script to split a long mp3 file in several 15 minutes splices.
Usage:
$0 files
EOF
exit 1
fi
# Check for ffprobe/ffmpeg
if [ -z $(which ffprobe) -a -z $(which ffmpeg) ]
then
echo "Could not find ffprobe nor ffmpeg. Exiting"
exit 2
fi
PROBE=$(which ffprobe)
if [ -z "$PROBE" ]
then
PROBE="ffmpeg -i"
fi
# Create OUTDIR if it doesn't exist
mkdir -p "$OUTDIR"
# Process each file passed to the script
for FILE in "$@"
do
# Retrieve duration info (hh:mm:ss.ss) removing trailing ','
DURHMS=$($PROBE "$FILE" 2>&1 | awk '/Duration/ {print $2}')
DURHMS=${DURHMS%,}
# Convert DURHMS to seconds
DURSECS=$(echo $DURHMS | awk -F: '{seconds=($1*60)*60; seconds=seconds+($2*60); seconds=seconds+$3; printf("%.0f", seconds)}')
# Retrieve filename without extension
FILENAMEEXT="$(basename "$FILE")"
FILENAME="${FILENAMEEXT%.*}"
FILEEXT="${FILENAMEEXT##*.}"
if [ "$FILEEXT" != mp3 -a "$FILEEXT" != MP3 ]
then
ACODEC="libmp3lame -ab 128k -ac 2"
else
ACODEC=copy
fi
for ((offset=0; offset < $DURSECS; offset+=${SPLICE}))
do
ffmpeg -i "$FILE" -ss "$offset" -t ${SPLICE} -acodec $ACODEC "$OUTDIR"/"$FILENAME"-"$offset".mp3
done
done
echo "Finished splitting files."
This script splits given input audio files (also non-mp3 ones) in "$SPLICE_MIN"-long splices. The resulting files are placed inside OUTDIR directory for your convenience ;-)
If you're going to overwrite an existing file, ffmpeg will warn you and ask what to do.
You might check out mp3splt (note, no "i") instead. It can easily split files by fixed time, specified time points, or using silence detection. There's also a gui interface for it, if you're so inclined.
Edit: To split multiple files with it, just use a shell loop:
Code:
for filename in *.mp3 ; do
mp3splt -t 15.00 -d "${filename%.mp3}" "$filename"
done
I added the -d option to have it output the split files into subdirectories with the same name as the original file, minus the extension. You can instead/also use the -o option to specify an output file format. See the man page.
Last edited by David the H.; 08-03-2012 at 11:16 PM.
Reason: As stated
I am uber late reporting back, please lash me many times with some Cat3 cable if you can find any!
414N, that worked GREAT! At first it failed, but if your MP3s are bad, then "poop in gives poop out". You sometimes get that when you download an MP3 from the Net...
The only gripe I have, and it's minor, is it will break up the file like this:
The problem being that the 900 should be after the 0, not last. I am playing with awk and the script to see if instead of 900, I can have 0900, then it would be perfect!
David, I checked out that program...looks pretty spiffy, but I can't get it working (yet) on my Linux box. I use an odd distro (PCLinuxOS), so sometimes tweaks are necessary to make "make" happy!
Let's see if I can adjust the original script for this
Code:
#!/bin/sh
# Simple script to split a long mp3 file in several SPLICE_MIN minutes splices.
# Splice duration (in minutes)
SPLICE_MIN=15
# Output directory for split files
OUTDIR=splitmp3s
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~ EDITABLE AREA END ~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Calculate seconds from SPLICE_MIN
SPLICE=$((SPLICE_MIN*60))
# Arguments check
if [ $# -eq 0 ]
then
cat << EOF
Simple script to split a long mp3 file in several 15 minutes splices.
Usage:
$0 files
EOF
exit 1
fi
# Check for ffprobe/ffmpeg
if [ -z $(which ffprobe) -a -z $(which ffmpeg) ]
then
echo "Could not find ffprobe nor ffmpeg. Exiting"
exit 2
fi
PROBE=$(which ffprobe)
if [ -z "$PROBE" ]
then
PROBE="ffmpeg -i"
fi
# Create OUTDIR if it doesn't exist
mkdir -p "$OUTDIR"
# Process each file passed to the script
for FILE in "$@"
do
# Retrieve duration info (hh:mm:ss.ss) removing trailing ','
DURHMS=$($PROBE "$FILE" 2>&1 | awk '/Duration/ {print $2}')
DURHMS=${DURHMS%,}
# Convert DURHMS to seconds
DURSECS=$(echo $DURHMS | awk -F: '{seconds=($1*60)*60; seconds=seconds+($2*60); seconds=seconds+$3; printf("%.0f", seconds)}')
# Retrieve filename without extension
FILENAMEEXT="$(basename "$FILE")"
FILENAME="${FILENAMEEXT%.*}"
FILEEXT="${FILENAMEEXT##*.}"
if [ "$FILEEXT" != mp3 -a "$FILEEXT" != MP3 ]
then
ACODEC="libmp3lame -ab 128k -ac 2"
else
ACODEC=copy
fi
# Compute the number of digits of the number of the output files.
# This is used for a correct 0-padding of the output filenames.
# Example: DURSECS=224, SPLICE=15, DURSECS/SPLICE=15 (ceiling)
# ========> NUMDIGITS=2
NUMDIGITS=$(echo "scale=0;l(${DURSECS}/${SPLICE})/l(10)+1" | bc -l)
for ((offset=0, i=0; offset < $DURSECS; offset+=${SPLICE}, i++))
do
ffmpeg -i "$FILE" -ss "$offset" -t ${SPLICE} -acodec $ACODEC "$OUTDIR"/"$FILENAME"-$(printf %0${NUMDIGITS}d $i).mp3
done
done
echo "Finished splitting files."
Now every split file should have a progressive number starting from 0 and it should also be nicely padded.
Please try again against some test file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.