Apple Script to get the total duration of audio files in hours and minutes.

I've found some scripts that produce the total minutes and seconds of a combined selection of audio files but not in hours and minutes. Any help would be appreciated.

Mac mini, macOS 14.4

Posted on Apr 2, 2024 9:28 AM

Reply
Question marked as Top-ranking reply

Posted on Apr 2, 2024 11:51 PM

This worked although, I removed the 'Get Selected Finder Items as it multiplied the result by 2. I assume workflow receiving current audio files was enough to pass to the script.


Here is the working code.

It would have been nice to limit the decimal calculation to two points i.e. 19.44.

on run {input, patamters}
	
	set audioFiles to input
	
	-- Initialize total duration
	set totalDuration to 0
	
	-- Iterate through selected files
	repeat with audioFile in audioFiles
		-- Get the duration using ffprobe (requires ffmpeg installation)
		set durationInSeconds to do shell script "/usr/local/bin/ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 " & quoted form of POSIX path of audioFile
		set totalDuration to totalDuration + durationInSeconds as number
	end repeat
	
	-- Convert total duration to hours and minutes
	set totalHours to totalDuration div 3600
	set remainingSeconds to totalDuration mod 3600
	set totalMinutes to remainingSeconds div 60
	
	-- Convert total duration to decimal format
	set totalDecimalHours to totalDuration / 3600.0
	
	-- Display the result
	display dialog "Total duration of selected audio files: " & totalHours & " hours and " & totalMinutes & " minutes (approximately " & totalDecimalHours & " hours)"
	
end run
6 replies
Question marked as Top-ranking reply

Apr 2, 2024 11:51 PM in response to Luis Sequeira1

This worked although, I removed the 'Get Selected Finder Items as it multiplied the result by 2. I assume workflow receiving current audio files was enough to pass to the script.


Here is the working code.

It would have been nice to limit the decimal calculation to two points i.e. 19.44.

on run {input, patamters}
	
	set audioFiles to input
	
	-- Initialize total duration
	set totalDuration to 0
	
	-- Iterate through selected files
	repeat with audioFile in audioFiles
		-- Get the duration using ffprobe (requires ffmpeg installation)
		set durationInSeconds to do shell script "/usr/local/bin/ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 " & quoted form of POSIX path of audioFile
		set totalDuration to totalDuration + durationInSeconds as number
	end repeat
	
	-- Convert total duration to hours and minutes
	set totalHours to totalDuration div 3600
	set remainingSeconds to totalDuration mod 3600
	set totalMinutes to remainingSeconds div 60
	
	-- Convert total duration to decimal format
	set totalDecimalHours to totalDuration / 3600.0
	
	-- Display the result
	display dialog "Total duration of selected audio files: " & totalHours & " hours and " & totalMinutes & " minutes (approximately " & totalDecimalHours & " hours)"
	
end run

Apr 2, 2024 10:29 AM in response to John Galt

I've created a script that works as need, It would be nice to turn it into a Finder Service action using automator, but I don't know how to adjust to script to suit this need. I would assume that the 'audioFiles' variable wouldn't work in automator. Ideally, I'd like the script to just run when I select the files in finder. Currently using the folder action I've get a pop up to select the files again.


-- Prompt the user to select audio files
set audioFiles to choose file with multiple selections allowed


-- Initialize total duration
set totalDuration to 0


-- Iterate through selected files
repeat with audioFile in audioFiles
	-- Get the duration using ffprobe (requires ffmpeg installation)
	set durationInSeconds to do shell script "/usr/local/bin/ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 " & quoted form of POSIX path of audioFile
	set totalDuration to totalDuration + durationInSeconds as number
end repeat


-- Convert total duration to hours and minutes
set totalHours to totalDuration div 3600
set remainingSeconds to totalDuration mod 3600
set totalMinutes to remainingSeconds div 60


-- Convert total duration to decimal format
set totalDecimalHours to totalDuration / 3600.0


-- Display the result
display dialog "Total duration of selected audio files: " & totalHours & " hours and " & totalMinutes & " minutes (approximately " & totalDecimalHours & " hours)"

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Apple Script to get the total duration of audio files in hours and minutes.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.