quicktime save as image for 6000 video files

First a little background…

I have 6000 video files from which I need to extract a set frame to make a thumbnail. I have the quicktime player scripts from Apple. After combining the “move to X in the front movie” and “Save current frame as JPEG” I actually got a script that does what I need it to do for each video.

The problem is that the existing script requires that a dialogue box opens where I pick the file to save and the file name. Obviously if I have to do this 6000 times its about the same as manually saving out an image file so it doesn’t really get me anywhere.

Whats the best way to extend the script below to work on multiple files at a time or to integrate with automator? I’d basically like to get the “target folder” to be the same as the “current folder” that the video file exists in.



tell application "QuickTime Player"
	activate
	
	try
		if not (exists movie 1) then error "No movies are open."
		
		-- set movie to correct frame
		
		set this_movie to the name of movie 1
		set the time_scale to the time scale of movie 1
		set the play_point to the current time of movie 1
		set the high_number to the duration of movie 1
		set the low_number to 0
		repeat
			try
				set the this_time to 30
				if the this_time is greater than or equal to the low_number and ¬
					the this_time is less than or equal to the high_number and ¬
					the this_time mod 1 is equal to 0 then
					exit repeat
				end if
			end try
		end repeat
		set the current time of movie 1 to this_time
		
		-- setup for export
		
		set the new_file to "image.jpg"
		-- set the new_file to the name of movie 1
		my status_dialog(new_file)
		set AppleScript's text item delimiters to ":"
		set the new_name to the last text item of (the new_file as string)
		-- set the destination_folder to path to folder of (movie 1)
		set the destination_folder to path to movie 1
		-- set the destination_folder to ((text items 1 thru -2 of (the new_file as string)) as string) & ":"
		set AppleScript's text item delimiters to ""
		
		my status_dialog("Extracting the current frame.")
		set the current_time to current time of movie 1
		select none of movie 1
		copy movie 1
		make new movie
		paste movie 1
		
		my status_dialog("Creating temp folder.")
		-- create a unique temp folder
		repeat
			set this_name to (random number from 100000 to 999999) as string
			tell application "Finder"
				if not (exists folder this_name of the desktop) then
					set the temp_folder to (make new folder at desktop with properties {name:this_name}) as alias
					exit repeat
				end if
			end tell
		end repeat
		
		my status_dialog("Exporting frame image as JPEG.")
		set the tempfile_name to "TEMP_IMAGE"
		set the target_file to ((temp_folder as string) & tempfile_name)
		-- export the image as a JPEG
		export movie 1 to file target_file as image sequence using settings preset "JPEG, 25 fps"
		close movie 1 saving no
		
		tell application "Finder"
			--rename the image and move it to the desktop or other folder
			set (the name of the first file of the temp_folder whose name ends with "1.jpg") to the new_name
			my status_dialog(destination_folder)
			move file new_name of the temp_folder to folder destination_folder with replacing
		end tell
		my status_dialog("Deleting the temp files.")
		tell application "Finder"
			-- delete the temp folder and its contents
			delete temp_folder
			empty trash
		end tell
		
	on error error_message number error_number
		if the error_number is not -128 then
			beep
			display dialog error_message buttons {"Cancel"} default button 1
		end if
	end try
	quit
end tell

on status_dialog(this_message)
	tell application "QuickTime Player"
		activate
		display dialog this_message buttons {"¢"} default button 1 giving up after 2
	end tell
end status_dialog


I haven’t done a lot of applescript but i’m very familiar with javascript/actionscript/etc so any help would be much appreciated.

This is COMPLETELY untested, but give it a shot

set base_folder to choose folder
tell application "Finder" to set QTfiles to every file of entire contents of base_folder as alias list

tell application "QuickTime Player"
	activate
	
	repeat with aMovie in QTfiles
		open aMovie
		set this_movie to the name of movie 1
		set the time_scale to the time scale of movie 1
		set the play_point to the current time of movie 1
		set the high_number to the duration of movie 1
		set the low_number to 0
		repeat
			try
				set the this_time to 30
				if the this_time is greater than or equal to the low_number and ¬
					the this_time is less than or equal to the high_number and ¬
					the this_time mod 1 is equal to 0 then
					exit repeat
				end if
			end try
		end repeat
		set the current time of movie 1 to this_time
		
		set the new_file to this_movie & "_image.jpg"
		set the destination_folder to path to movie 1
		set the current_time to current time of movie 1
		select none of movie 1
		copy movie 1
		make new movie
		paste movie 1
		repeat
			set this_name to (random number from 100000 to 999999) as string
			tell application "Finder"
				if not (exists folder this_name of the desktop) then
					set the temp_folder to (make new folder at desktop with properties {name:this_name}) as alias
					exit repeat
				end if
			end tell
		end repeat
		set the tempfile_name to "TEMP_IMAGE"
		set the target_file to ((temp_folder as string) & tempfile_name)
		-- export the image as a JPEG
		export movie 1 to file target_file as image sequence using settings preset "JPEG, 25 fps"
		close movie 1 saving no
		tell application "Finder"
			--rename the image and move it to the desktop or other folder
			set (the name of the first file of the temp_folder whose name ends with "1.jpg") to the new_name
			move file new_name of the temp_folder to folder destination_folder with replacing
		end tell
		tell application "Finder"
			-- delete the temp folder and its contents
			delete temp_folder
			empty trash
		end tell
	end repeat
end tell

Thanks James, It does work very nicely. I had to change the default path to your base path and I made it close the qt when it was done with each one, but it is happily converting video to thumbs. Quick question though…

How can I take your “choose a folder” command

set base_folder to choose folder

and change it so I can run the script inside an automator workflow and get it to take a folder thats passed to it from the previous block.

For example. IF I place the above applescript inside the “run applescript” action below “ask for finder itmes” action. I would want the var base_folder to equal the output from the “ask for finder items” action.

Do I actually have to fire up xCode and build an action to get this to work?

This should work…

(1) Ask for Finder Items
Type: Folders
Start at: Wherever
Prompt: Whatever
Allow Multiple Selections: False
Output: Files/Folders

(2) Run AppleScript
Accept: Anything (Use Results From Previous Action)
Code:

on run {input, parameters}
	set base_folder to item 1 of input
	tell application "Finder" to set QTfiles to every file of entire contents of base_folder as alias list
	tell application "QuickTime Player"
		activate
		repeat with aMovie in QTfiles
			open aMovie
			set this_movie to the name of movie 1
			set the time_scale to the time scale of movie 1
			set the play_point to the current time of movie 1
			set the high_number to the duration of movie 1
			set the low_number to 0
			repeat
				try
					set the this_time to 30
					if the this_time is greater than or equal to the low_number and ¬
						the this_time is less than or equal to the high_number and ¬
						the this_time mod 1 is equal to 0 then
						exit repeat
					end if
				end try
			end repeat
			set the current time of movie 1 to this_time
			
			set the new_file to this_movie & "_image.jpg"
			set the destination_folder to path to movie 1
			set the current_time to current time of movie 1
			select none of movie 1
			copy movie 1
			make new movie
			paste movie 1
			repeat
				set this_name to (random number from 100000 to 999999) as string
				tell application "Finder"
					if not (exists folder this_name of the desktop) then
						set the temp_folder to (make new folder at desktop with properties {name:this_name}) as alias
						exit repeat
					end if
				end tell
			end repeat
			set the tempfile_name to "TEMP_IMAGE"
			set the target_file to ((temp_folder as string) & tempfile_name)
			-- export the image as a JPEG
			export movie 1 to file target_file as image sequence using settings preset "JPEG, 25 fps"
			close movie 1 saving no
			tell application "Finder"
				--rename the image and move it to the desktop or other folder
				set (the name of the first file of the temp_folder whose name ends with "1.jpg") to the new_name
				move file new_name of the temp_folder to folder destination_folder with replacing
			end tell
			tell application "Finder"
				-- delete the temp folder and its contents
				delete temp_folder
				empty trash
			end tell
		end repeat
	end tell
end run

Is that what you’re looking for?

Yup I think thats what I need. I just didn’t know what the input number was for that data location.

Thanks much for the help. If you ever need any html/css/actionscript help let me know i may be able to return the favor :slight_smile: