Extracting an Audio Track from a QuickTime Movie

Today I built a quick Automator Script with the goal of extracting the audio tracks from a bunch of QuickTime movies. Here is the AppleScript portion:


        on run {input, parameters}
	set basePath to ((path to desktop) as string) & "audio-extracts:"
	tell application id "com.apple.quicktimeplayer"
		activate
		open input
		set docs to (count of documents)
		repeat docs times
			set docName to name of document 1
			tell document 1
				set trackList to (the name of every track whose type is "soun") as list
				if the trackList is {} then
					error "There are no audio tracks in the front movie."
				end if
				if (count of trackList) is greater than 1 then
					error "There is more than 1 audio track in the front movie."
				end if
				set audioTrack to item 1 of trackList
				set fileName to basePath & docName
				export track audioTrack to file fileName as QuickTime movie
			end tell
			close document 1
		end repeat
	end tell
end run

Basically, the script opens all of movies that Automator throws at it, then one by one, export the audio track of each QuickTime movie as a separate movie. This works fine, except that the process of exporting the audio track re-encodes the track - which isn’t necessary.

So my question is, how do I simply save the audio track as a standalone QuickTime movie without re-encoding it? I’ve been trying to create a new QuickTime document, add the audio track to it, then “save self contained”, but I haven’t had any success yet.

Thanks.

I wrote this script awhile ago so you can have it. Save it as an application and then you can either double-click it run it normally or you can drop a bunch of files on it. NOTE: I’m using MacOSX 10.6 so if you’re using something earlier then change “QuickTime Player 7” to “QuickTime Player”

You’ll see that I delete any track that is not an audio track, and then I save that to a new file using “save self contained”. This prevents the audio from being re-encoded.

-- this will extract the audio from a movie and save it to a new file
-- the new file is saved it in the same folder as the movie with "_audio" appended to it
-- you can double-click this or drop some movies on it

on run
	-- get the movies
	set fileList to (choose file with prompt "Choose movie files to extract the audio." with multiple selections allowed) as list
	open fileList
end run

on open theFiles
	-- disable some features of quicktime to avoid problems
	tell application "QuickTime Player 7"
		launch
		set ignoresAutoPlay to ignore auto play
		set ignoresAutoPresent to ignore auto present
		if not ignoresAutoPlay then set ignore auto play to true
		if not ignoresAutoPresent then set ignore auto present to true
	end tell
	
	set cantSaveList to {} -- a list of files that couldn't be saved
	set multipleAudioTracksList to {} -- a list of files that have more than 1 audio track
	set otherErrorsList to {}
	repeat with aMovie in theFiles
		-- get the movie name and container folder so we can save the audio file
		set movieName to item 1 of (getName_andExtension(aMovie))
		tell application "Finder" to set outputFolder to container of aMovie
		
		tell application "QuickTime Player 7"
			try
				open aMovie
				tell document 1
					-- make sure we can save this file, some files are protected
					if saveable is not true then error "cantSaveList"
					
					-- remove any track that's not an audio track
					-- we iterate backwards to avoid getting i and the track number out-of-sync
					set allTracks to every track
					repeat with i from (count of allTracks) to 1 by -1
						set thisTrack to item i of allTracks
						if kind of thisTrack is not "Sound" then delete thisTrack
					end repeat
					
					-- make sure we have only 1 track left
					if (count of tracks) is not 1 then error "multipleAudioTracksList"
					
					-- save it and close it
					set outPath to (outputFolder as text) & movieName & "_audio.mov"
					save self contained in file outPath
					close saving no
				end tell
			on error theError
				try
					tell document 1 to close saving no
				end try
				
				set thisFile to POSIX path of (contents of aMovie)
				if theError is "cantSaveList" then
					set end of cantSaveList to thisFile
				else if theError is "multipleAudioTracksList" then
					set end of multipleAudioTracksList to thisFile
				else
					set end of otherErrorsList to thisFile
				end if
			end try
		end tell
	end repeat
	
	-- put quicktime back the way it was
	tell application "QuickTime Player 7"
		if not ignoresAutoPlay then set ignore auto play to false
		if not ignoresAutoPresent then set ignore auto present to false
	end tell
	
	-- display the status
	set dText to ""
	if cantSaveList is not {} then
		set cantSaveString to listToString(cantSaveList)
		set dText to dText & "The following files could not be saved:" & return & cantSaveString & return & return
	end if
	
	if multipleAudioTracksList is not {} then
		set multipleAudioString to listToString(multipleAudioTracksList)
		set dText to dText & "The following files have multiple audio tracks:" & return & multipleAudioString & return & return
	end if
	
	if otherErrorsList is not {} then
		set otherErrorsString to listToString(otherErrorsList)
		set dText to dText & "The following files errored:" & return & otherErrorsString & return & return
	end if
	
	if dText is "" then
		tell me
			activate
			display dialog "Extracted audio with no errors!" buttons {"OK"} default button 1 with icon note with title "QT7 Extract Audio"
		end tell
	else
		set dText to text 1 thru -3 of dText -- remove the ending return characters
		tell me
			activate
			display dialog dText buttons {"OK"} default button 1 with title "QT7 Extract Audio"
		end tell
	end if
end open


(*====================== SUBROUTINES ======================*)
on listToString(theList)
	set AppleScript's text item delimiters to return
	set theString to theList as text
	set AppleScript's text item delimiters to ""
	return theString
end listToString

on getName_andExtension(f)
	set f to f as Unicode text
	set {name:Nm, name extension:Ex} to info for file f without size
	if Ex is missing value then set Ex to ""
	if Ex is not "" then
		set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set Ex to "." & Ex
	end if
	return {Nm, Ex}
end getName_andExtension