Scripting Concatenation of Multiple MP3 Files

I frequently need to concatenate multiple MP3 files. Here is my workflow:

  1. Open folder containing the MP3 files
  2. Command/up arrow to go to containing folder
  3. Select folder and control click to bring up Services menu
  4. Choose “New Terminal at Folder” from Services menu
  5. Re-open folder containing MP3 files
  6. Select all MP3 files, copy their names to clipboard, paste into a text editor.
  7. Replace line breaks with spaces
  8. Copy resultant text to clipboard
  9. Type into Terminal: cat [file names] > newfile.mp3

Notes: The MP3 files always have unpredictable names and reside in unpredictable folders.

I likely can’t script this whole workflow, but are there sections I could streamline via scripting (or other means)?

Check out dougscripts.com

And his “join together”
Script

Won’t produce an MP3 (only m4a) and won’t create the file in same directory (like my terminal CAT command will). Instead it adds the file to iTunes.

There are many file-joining apps, but I’d still need to meta-script the process because I’m hoping to select files, run a script, and have them joined. So I might as well go through Terminal.

tell application id "com.apple.finder" -- Finder
	set theFiles to selection as alias list
end tell
set newFile to choose file name with prompt "Save concatenated file as:"
set fileRef to (open for access newFile with write permission)
repeat with aFile in theFiles
	set theData to read aFile as data
	write theData to fileRef
end repeat
close access fileRef

Hi. Is the intent to select individual music files or just a containing folder? This will merge files in the latter case and save the MP3 to the desktop. Saving the result file to the same location is problematic.

do shell script "find " & my (choose folder with prompt "Where are the MP3 files to merge?")'s POSIX path's quoted form & " -name '*.*' -prune -name '*.mp3' -print0 |  Xargs -0  cat > " & ((path to desktop folder as text)'s POSIX path & "/newfile.mp3")'s quoted form

I do not think the OP will understand your (or, Shane’s) code. He needs to at least understand why you are not using Terminal and text editor. In addition, the OP wants to get a list of names, and then glue it into a single line (string). Therefore, the OP needs to read some book about AppleScript.

NOTE: I tried both scripts. Both of them doesn’t concatenate mp3 files correctly…

Then, I tried the following code, which worked ALMOST correctly. 2 previous script doesn’t concatenate the timings. This last concatenates the timing, but, for some reason it adds 4 seconds and 3 milliseconds to proper summary timing. So, I assume, the cat command has problem to perform this task:


set aFile1 to quoted form of (POSIX path of (choose file))
set aFile2 to quoted form of (POSIX path of (choose file))

set aDesktop to POSIX path of (path to desktop folder)
set destFile to quoted form of (aDesktop & "NewFile.mp3")

do shell script "cat " & aFile1 & space & aFile2 & " > " & destFile

I think, the error is due to the fact that my 2 tested mp3 files have 2 different bitrates (128 kb/s and 320 kb/s) and cat command does some king of middle bitrate - the resulting mp3 file has 136 kb/s.

They both do what cat does. if the OP was happy with what he outlined, then either should be fine.

No, Shane, I did not mean to say that the cat command is not working. It works perfectly, but gives an accurate result only when mp3 files of the same bitrate are glued together. Which is quite logical, and should be taken into account.

And yes, as I see now, your codes works exactly the same way as my test code. It seems, when I tested 2 audio with 2 different bitrates, the total duration was depended on order of gluing. Your codes concatenates 2nd then 1st mp3, and my test code - 1st then 2nd. That is why I got different results. Well, then this is one another reason to glue only files with the same bitrate.

Test with my code and 2 mp3 files with different bitrate. then change the order of gluing. You should find strange thing - total duration should be different… :slight_smile:

Here, I edited your code little, in the case the OP want to select folder (or folders) instead of choosing files:


tell application "Finder"
	set chosenFolders to (get selection)
	repeat with aFolder in chosenFolders
		if class of aFolder is folder then
			set mp3Files to (every file of aFolder whose kind is "MP3 audio") as alias list
			if (count mp3Files) > 1 then
				set mp3shellSting to ""
				repeat with mp3File in mp3Files
					set mp3shellSting to mp3shellSting & (quoted form of (POSIX path of mp3File)) & space
				end repeat
				set newFilePath to POSIX path of (choose file name with prompt "Save concatenated file as:" default location (aFolder as alias))
				do shell script "cat " & mp3shellSting & "> " & quoted form of (newFilePath & ".mp3")
			end if
		end if
	end repeat
end tell

NOTE: the destination location should be not read-only folder system