Check for corrupted movie files with QuickTime

Hi there!

My HD broke a few days ago and I’d like to test if all videos are still intact. I’d like to do that by playing a few seconds from the beginning, the middle and the end. And then checking if they were really playing. This is because when a video is corrupted and you skip to a piece where it isn’t working anymore, mostly it will play the piece till where it’s working. Which in this case wouldn’t be the same as the point where I want to play it and if this is the case, move it to the “BrokenFiles” list. This is what I got, it doesn’t work actually and is rather slow. Any improvements? Thanks!

property ProcessList : {}
property BrokenFiles : {}
property VideoExtensions : {"avi", "mov", "mpg", "mpeg"}

tell application "Finder" to set FolderContents to (get files of folder (path to desktop folder))

repeat with i in FolderContents
	tell application "Finder" to set y to name extension of i
	if VideoExtensions contains y then
		set end of ProcessList to i as alias
	end if
end repeat

repeat with x in ProcessList
	tell application "QuickTime Player"
		open x
		tell document 1
			set muted to true --I rather don't want to hear the audio of a couple of hundred files
			set theDuration to duration
			start
			set current time to 10000
			if current time is not 10000 then
				set end of BrokenFiles to x
			else
				set current time to (theDuration / 2)
				if current time is not (theDuration / 2) then
					set end of BrokenFiles to x
				else
					set current time to (theDuration - 10000)
					if current time is not (theDuration - 1000) then
						set end of BrokenFiles to x
					end if
				end if
			end if
			stop
		end tell
		close document 1
	end tell
end repeat
BrokenFiles

Thanks!

Here’s how I would do it…

property secondsToTestPlay : 2 -- how long should the movie be played to test if it is good
property marginOfError : 0.1 -- a small number in seconds to make sure the movie was played for the proper amount of time
property VideoExtensions : {"avi", "mov", "mpg", "mpeg"}

-- these shouldn't be properties because properties are remembered
-- between script launches and we need them to start out as {}
set ProcessList to {}
set BrokenFiles to {}

-- get all the movie files on the desktop
tell application "Finder" to set FolderContents to (get files of (path to desktop folder))
repeat with i in FolderContents
	tell application "Finder" to set y to name extension of i
	if VideoExtensions contains y then set end of ProcessList to i as alias
end repeat

-- make sure there are some movie files to test
if ProcessList is {} then
	tell me
		activate
		display dialog "No movie files were found! Try again." buttons {"OK"} default button 1 with icon stop
		return
	end tell
end if

-- test the movie files
repeat with x in ProcessList
	try
		tell application "QuickTime Player"
			open x
			tell document 1
				set muted to true --I rather don't want to hear the audio of a couple of hundred files
				set theDuration to duration
				set timeScale to time scale
				
				-- try playing from the beginning and validate stop time
				set beginTime to 0
				set current time to beginTime
				play
				delay secondsToTestPlay
				stop
				if not (my validateStopTime(beginTime, current time, timeScale)) then error
				
				-- try playing from the middle and validate stop time
				set middleTime to theDuration / 2
				set current time to middleTime
				play
				delay secondsToTestPlay
				stop
				if not (my validateStopTime(middleTime, current time, timeScale)) then error
				
				-- try playing from the end and validate stop time
				set endTime to (theDuration - ((2 * secondsToTestPlay) * timeScale)) -- double the secondsToTestPlay from the end
				set current time to endTime
				play
				delay secondsToTestPlay
				stop
				if not (my validateStopTime(endTime, current time, timeScale)) then error
				
				-- close the movie
				close
			end tell
		end tell
	on error
		try
			close document 1
		end try
		set end of BrokenFiles to contents of x
	end try
end repeat
return BrokenFiles


(*=============== SUBROUTINES ===============*)
-- we compare when the movie actually stopped to when it should have stopped
-- and make sure that those times are within a margin of error
on validateStopTime(startTime, stopTime, tScale)
	set timeDiffIsGood to true
	set timeShouldBe to (startTime + (secondsToTestPlay * tScale)) -- calculate when the movie should have stopped
	if timeShouldBe is greater than stopTime then
		set timeDiff to timeShouldBe - stopTime
	else
		set timeDiff to stopTime - timeShouldBe
	end if
	if timeDiff is greater than (marginOfError * tScale) then set timeDiffIsGood to false
	return timeDiffIsGood
end validateStopTime

What a MASTERPIECE ! :smiley: Really Thanks!! I hope you didn’t put too much time in this.

I took it for a run on three files, one of them I know was corrupted and it happened to find the corrupted one !

Really, I really really ( x 4) have to thank you for it. If I could ever help you (I’m little more than basic AppleScript programmer, speaking fluent English & Dutch (comes handy for localizations)) drop my a note !

You should put this in Code Exchange my friend :slight_smile:

Thanks again!

I’m glad you like it. :smiley:

No, it didn’t take too much time at all. It was all your idea of what to do to detect a corrupt movie so it didn’t take much thought at all on my part. All I really did was show you how to use the time scale. Normally when you get the current time or the duration the result is an integer and it measures some sort of frame time of the movie. In order to make sense of that we need to convert it into time in seconds and that’s where the time scale factors in.

For example, to get the duration of the movie in seconds you divide the duration by the time scale. Any time you can convert that into seconds which makes it usable. So it wasn’t complicated at all if you knew about the time scale. Anyway, good luck.

Yes, it was my idea but that was all, you did it, not me. I wish I could do it but I didn’t use QuickTime in AppleScript before. I generally prefer to avoid AppleScript to work with other applications. I even try to avoid using the Finder & System Events.

I don’t know why but if you remove the try block when closing a corrupt video, it gives an error it can’t close because the script is till running. I don’t see why it wouldn’t, the good ones close. -Slips into my head just seconds ago: maybe I can tell it to press “command-w” and that would close it, but I don’t know if I’m going to use it because, If I’m lucky only a few vids are broken and there are no 100 windows open in QuickTime.-

Anyway, It doesn’t matter, I’m going to test this on about 1200 movie files later on.

This is a really good script regulus6633, that’s really helpful for me, now I don’t have to open up all these movie files myself. So again, thank you :slight_smile: