Script to sum durations of QuickTime movies in Folder

I just found this script that does a lot of what I need:

set MovieFolder to (choose folder without invisibles)
tell application "Finder"
	try
		set M to files of MovieFolder as alias list
	on error
		set M to {first file of MovieFolder as alias}
	end try
end tell

set MList to {"Movies and Durations", ""}
set padding to tab & tab
repeat with aF in M
	tell application "QuickTime Player"
		open aF
		set {name:N, duration:movie_duration, time scale:time_scale} to front document
		close front document
	end tell
	set running_time to movie_duration / time_scale
	-- To round the seconds instead of truncating, change 'div 1' in the next line to 'as integer'.
	tell (1000000 + running_time div hours * 10000 + running_time mod hours div minutes * 100 + running_time mod minutes div 1) as string
		set hhmmss to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7
	end tell
	set end of MList to N & padding & hhmmss
end repeat

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set {text:MList} to MList as string
set AppleScript's text item delimiters to astid

set dataFile to (open for access ((path to desktop as Unicode text) & "Movies&Durations.txt") with write permission)
try
	set eof dataFile to 0 -- erase anything there
	write MList to dataFile
end try
close access dataFile

However, the difference I would like to have is to generate a TOTAL runtime for all movies. The sum of the movie durations in the format: HH:MM:SS. If we could tag this onto a final (additional) line at the end of the text doc. that’s generated”that would be great.

Thanks in advance, for any guidance. This forum, and it’s contributors, never cease to amaze me!

All the best, Kevin

Hi,

this should do it.
System Events can read metadata of QuickTime files without opening them.
Sorry, I don’t like one-letter or other unreadable variable names


set movieFolder to (choose folder without invisibles)
tell application "Finder" to set fileList to files of movieFolder

set totalDuration to 0
set movieList to {"Movies and Durations", ""}
set padding to tab & tab
repeat with aFile in fileList
	tell application "System Events"
		set quickTimeFile to QuickTime file (aFile as text)
		set {name:fileName, duration:movie_duration, time scale:time_scale} to quickTimeFile
	end tell
	set running_time to movie_duration / time_scale
	set totalDuration to totalDuration + running_time
	set end of movieList to fileName & padding & convert_hhmmss(running_time)
end repeat

set end of movieList to (return & return & "total duration: " & convert_hhmmss(totalDuration))

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set movieList to movieList as text
set AppleScript's text item delimiters to astid

set textFile to ((path to desktop as text) & "Movies&Durations.txt")
set dataPointer to open for access file textFile with write permission
try
	set eof dataPointer to 0 -- erase anything there
	write movieList to dataPointer
	close access dataPointer
on error
	try
		close access file textFile
	end try
end try

on convert_hhmmss(runTime)
	tell (1000000 + runTime div hours * 10000 + runTime mod hours div minutes * 100 + runTime mod minutes div 1) as string
		set hhmmss to text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7
	end tell
	return hhmmss
end convert_hhmmss


Stefan,

Thanks so much for the response. Brilliant! My apologies for the long delay in showing my appreciation.

Again, this forum continues to amaze me. SO helpful!!

All the best, Kevin