LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 08-01-2012, 07:00 PM   #1
dougp23
Member
 
Registered: Oct 2006
Posts: 43

Rep: Reputation: 15
Split MANY MP3 files


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.

Thanks.
 
Old 08-02-2012, 07:45 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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.
 
Old 08-03-2012, 07:22 AM   #3
dougp23
Member
 
Registered: Oct 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Thanks 414N!

Sheesh, reading that, I feel like you must work for NASA or something....impressive!

Thank you again!
 
Old 08-03-2012, 07:53 AM   #4
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
No problem
Please tell me if it works as you'd have expected.
 
1 members found this post helpful.
Old 08-03-2012, 11:04 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.

http://mp3splt.sourceforge.net/mp3splt_page/home.php


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
 
1 members found this post helpful.
Old 09-13-2012, 07:23 PM   #6
dougp23
Member
 
Registered: Oct 2006
Posts: 43

Original Poster
Rep: Reputation: 15
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:

CBSRMT_1974-01-08_0003_TheBullet-0.mp3
CBSRMT_1974-01-08_0003_TheBullet-1800.mp3
CBSRMT_1974-01-08_0003_TheBullet-2700.mp3
CBSRMT_1974-01-08_0003_TheBullet-900.mp3

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!

thank you both!

Last edited by dougp23; 09-13-2012 at 07:28 PM.
 
Old 09-14-2012, 02:13 AM   #7
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
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.
 
1 members found this post helpful.
Old 09-14-2012, 07:09 PM   #8
dougp23
Member
 
Registered: Oct 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Awesome. I didn't even think of numbering the way you did....makes perfect sense!

Thanks a million!
 
Old 09-15-2012, 04:05 PM   #9
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Glad to be of help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not able to play mp3 files and install vlc & mp3 players in fedora 8 harit agarwal Linux - Software 5 12-22-2008 04:50 PM
PHP/Java/Anything that can split files, while files stay on Server? Lamborghini Kid Linux - Software 1 04-27-2007 04:53 PM
how does one use DD to recombine files from using pipe to split files originally? nerdful1 Linux - General 3 03-28-2006 07:46 AM
XMMS fails to open some mp3 files, while opening some other mp3's ditch* Linux - Software 3 03-05-2005 03:35 AM
Is there any mp3 split tools? laomie Linux - Software 2 01-19-2004 07:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration