Create an AppleScript application to concatenate media files using ffmpeg.

Please read the following, or you’ll probably reach the conclusion that this doesn’t work right, and then you’ll just be mad at me for wasting your time.

The AppleScript below is configured to exploit a special relationship between AppleScript and the Mac Finder. Here are two suggested methods for using it to concatenate media files by passing the appropriate commands to ffmpeg. First, the media files must be compatible for concatenation. The documentation for ffmpeg can help with that. The AppleScript will create the concatenated output file in the same folder as the source files. The ffmpeg command generated by the AppleScript is ultimately placed in the trash, so you can retrieve it from there and view it if something doesn’t work as expected.

Method 1

I suggest this as the most straightforward method for using the AppleScript. Paste the AppleScript into a Script Editor window, then save it as an Application with any name you choose. The Application allows dropping multiple items onto its icon. Make a new folder in Finder, and place only the files to be concatenated into it. Ensure that the files in the folder are displayed in list view (as List in the Finder’s View menu), then sort the list as desired with the first file at the top of the window (name them so that they can be correctly sorted). Select all of the files (Select All from the Finder’s Edit menu), then click and drag the first file (the file at the top of the list) to the application’s icon (all of the selected files will be included in the drag). Accept the default name for the output or enter a new one and press OK.

Method 2

This method may be more useful if there are only a few files to concatenate. Paste the AppleScript into a Script Editor window and press RUN (The triangle pointing right near the top of the Script Editor window), or open the Application created from the AppleScript in a Script Editor window and press RUN. Navigate to the folder containing the source files in the Open dialog, then hold the Command key and click the files one at a time in the order in which you want them concatenated. While the clicked files remain selected in the Open dialog window, click OK. The output file will have a name based on the first file you clicked in the Open dialog.

It’s possible that you’ll need to provide a full path name for the ffmpeg executable rather than just the name “ffmpeg” (line 47, right after “sleep 3;”). To determine where your ffmpeg executable resides, you can run the following command in Terminal:

which ffmpeg

If you don’t have a compatible version of ffmpeg installed, install it using Homebrew. Here’s one recommended command line for installing the most recently released ffmpeg:

brew install homebrew-ffmpeg/ffmpeg/ffmpeg --HEAD

Here’s an example for including non-default options when installing ffmpeg

brew install homebrew-ffmpeg/ffmpeg/ffmpeg –HEAD –with-fdk-aac –with-two-lame

To update the installation of ffmpeg performed this way, use the following command.

brew update && brew upgrade homebrew-ffmpeg/ffmpeg/ffmpeg --fetch-HEAD

--begin AppleScript--
property temppath : "/private/tmp/"
property startnum : 0
property tmpfile : "/tmp/execme.command"
property outname : ""

on open the_items
	my ffmpeg_cat(the_items)
end open

on ffmpeg_cat(the_items)
	set theshellscript to ""
	repeat with i from 1 to (count of the_items)
		set itemcount to (count of the_items)
		set the_item to item i of the_items as alias
			tell application "Finder"
				set sost to (container of the_item) as string
			end tell
			set pos_filepath to POSIX path of sost
		end try
		set this_filepath to (the_item as string)
		if last character of this_filepath is ":" then
			tell me to set it_is_a_folder to true
			set it_is_a_folder to false
		end if
		set thesourcename to (name of (info for the_item))
		set namepart to (name extension of (info for the_item))
		set the_source_file to POSIX path of this_filepath
		if outname is "" then
			set outname to (text returned of (display dialog "Enter a name for the concatenated output file:" default answer thesourcename))
		end if
		set finalname to replace_chars(outname, "." & namepart, "")
			if i = 1 then
				set the filelistbody to "# comment" & return & "file" & space & (quoted form of the_source_file) & return
				set the filelistbody to filelistbody & "file" & space & (quoted form of the_source_file) & return
			end if
		on error onerr
			display dialog onerr
		end try
	end repeat
	set fileData to "echo" & space & "\"" & filelistbody & "\"" & space & "> ffmpegCatList"
	set theshellscript to fileData & ";sleep 3;ffmpeg -f concat -safe 0 -i ffmpegCatList -c copy" & space
	set shellExec to space & (quoted form of (pos_filepath & finalname & "_1-" & i & "." & namepart))
	set theshellscript to the theshellscript & shellExec & ";sleep 3" & return
	set the clipboard to theshellscript
	set theshellscript to theshellscript & ";/bin/echo '
			" & finalname & "_1-" & i & "." & namepart & space & "FINISHED!" & "
			';mv" & space & (quoted form of tmpfile) & space & (quoted form of (POSIX path of (path to trash))) & ";mv ffmpegCatList" & space & (quoted form of (POSIX path of (path to trash)))
	do shell script "echo " & quoted form of theshellscript & " > " & tmpfile
			do shell script "chmod +x " & tmpfile
			do shell script "open -a" & space & tmpfile
			exit repeat
		on error
			delay 1
		end try
	end repeat
end ffmpeg_cat

on replace_chars(this_text, _bad, _good)
	set AppleScript's text item delimiters to the _bad
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the _good as string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on run
	set the_items to ((choose file with multiple selections allowed) as list)
end run--end AppleScript--


3 thoughts on “Create an AppleScript application to concatenate media files using ffmpeg.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s