is there a faceless way to do this? set delay based on audio duration

The goal of this script is to select a random audio file from a pre-determined folder, calculate the duration of the audio file, slowly fade down iTunes, play the announcement and then fade iTunes back up.

I can play the audio files in the background using apps other than quicktime player, but using qt player is the only way i know of to calculate the duration of the audio file and if i have to open it anyway then i might as well play the file using QT.

any thoughts or opinions?

		tell application "Finder"
			set audio_announcements to some file of folder ((path to home folder) & "announcements:" as Unicode text) as string
		end tell
		tell application "QuickTime Player 7"
			activate
			open file audio_announcements
			set the time_scale to the time scale of document 1
			set the movie_duration to the duration of document 1
			set volume_delay to (movie_duration / time_scale) as integer
		end tell

		tell application "iTunes"
			set the sound volume to 90
			delay 0.25
			set the sound volume to 80
			delay 0.25
			set the sound volume to 70
			delay 0.25
			set the sound volume to 60
			delay 0.25
			set the sound volume to 50
			delay 0.25
			set the sound volume to 40
		end tell

		tell application "QuickTime Player 7"
			activate
			play document 1
			delay volume_delay
			close document 1 without saving
		end tell

		tell application "iTunes"
			set the sound volume to 50
			delay 0.25
			set the sound volume to 60
			delay 0.25
			set the sound volume to 70
			delay 0.25
			set the sound volume to 80
			delay 0.25
			set the sound volume to 90
			delay 0.25
			set the sound volume to 100
		end tell

Have you tried mdls? Gives the duration of a mpeg-4 file. Maybe it works with other audio files as well.


do shell script "mdls -name kMDItemDurationSeconds " & quoted form of posix path of audio_announcements

There’s a command line tool…

set f to choose file
set durationWords to words of (do shell script "afinfo " & quoted form of (POSIX path of f) & " | grep -i duration")

set theSeconds to missing value
repeat with aWord in durationWords
	try
		aWord as real
		set theSeconds to aWord as real
		exit repeat
	end try
end repeat

return theSeconds

Or System Events has a QuickTime File Suite:

tell application "System Events"
	set audio_announcements to (some file of folder "announcements" of home folder) as alias as Unicode text
	tell contents of QuickTime file audio_announcements to set volume_delay to duration / time scale
end tell

wow. both of those work really well. i like that both approaches were so different, too. i’m going to have to wrap my head around Regulus’ for a bit to see how it works, but DJ Bazzie Wazzie’s was pretty straightforward.

in one of life’s amusing ironies, learning how to calculate the time outside of quicktime encouraged me to adopt a non-quicktime player method to play the audio files… and APLAY apparently locks up the script until it’s done playing so it doesn’t need a delay. I’m not sure this is a good thing yet. Time will tell. it might be better to use something other than AFPLAY.

here’s what the modified script looks like (before i discovered that the delay was potentially unnecessary.

tell application "Finder"
set audio_announcements to some file of folder ((path to home folder) & "announcements:" as Unicode text) as string
end tell
set volume_delay to ((text 26 thru 31) of (do shell script "mdls -name kMDItemDurationSeconds " & quoted form of POSIX path of audio_announcements)) as integer
tell application "iTunes"
	set the sound volume to 90
	delay 0.25
	set the sound volume to 80
	delay 0.25
	set the sound volume to 70
	delay 0.25
	set the sound volume to 60
	delay 0.25
	set the sound volume to 50
	delay 0.25
	set the sound volume to 40
end tell
do shell script "afplay " & quoted form of POSIX path of audio_announcements
delay volume_delay
tell application "iTunes"
	set the sound volume to 50
	delay 0.25
	set the sound volume to 60
	delay 0.25
	set the sound volume to 70
	delay 0.25
	set the sound volume to 80
	delay 0.25
	set the sound volume to 90
	delay 0.25
	set the sound volume to 100
end tell

one nice thing I just noticed about each of these methods is that they seem to be agnostic to the version of QuickTime Player. My original method required QuickTime Player 7 due to the “duration / time scale” function not working in Quicktime Player X.

It’s better to change

set volume_delay to ((text 26 thru 31) of (do shell script "mdls -name kMDItemDurationSeconds " & quoted form of POSIX path of audio_announcements)) as integer

to

set volume_delay to (do shell script "mdls -raw -name kMDItemDurationSeconds " & quoted form of posix path of audio_announcements) as integer

Because it doesn’t matter the lengths of kMDItemDurationSeconds’s value anymore now because mdls will now only output it’s value and not the key/value pair.

ah, yes. that is better. I was initially worried about it clipping a digit off the front of the number so i ran tests with files of various durations to confirm that the correct number would be used.

the method i used to get the digit did the job, but yours is cleaner. thanks again.