Script 1 of 4. Videos from a folder to quicktime format

My problem: I have a growing collection of videos that I’ve acquired over the years. My problem is that I have so many now that it’s difficult to remember what they all are, what they’re about, who is in each etc.

The solution: I developed a workflow of 4 applescripts to accomplish my task. The workflow ultimately places the information about each video in the video file, such that I can open a video and instantly see the data or use Spotlight to search on the data. A more detailed explanation of what each script does is explained inside each applescript.

This is script 1 of 4. Videos from a folder to quicktime format:
This script works on a folder full of files. It searches the folder and identifies video files that are not in a quicktime format appropriate for this workflow. Once the videos have been identified it will then convert the video to the proper format. The converted video is saved to a different folder so that the original video is not changed. It uses “label color” changing because I run this script on a folder full of many files. Changing the label colors helps me keep track of which videos need to be converted, which ones have been successfully converted, and which ones caused errors.

The 4 scripts are here:
http://bbs.applescript.net/viewtopic.php?pid=82570#p82570
http://bbs.applescript.net/viewtopic.php?pid=82571#p82571
http://bbs.applescript.net/viewtopic.php?pid=82572#p82572
http://bbs.applescript.net/viewtopic.php?pid=82573#p82573

(* I wrote this script because I found that the time scale of a video is important when you are creating chapters in a video using QuickTime 7. When the time scale is not 600 you can't create the chapters properly. See this thread for more information on the time scale issue: http://bbs.applescript.net/viewtopic.php?id=21357 *)

(* So the purpose of this script is to convert any type of video file that quicktime can understand to quicktime format with a time scale of 600 units of time per second. Normally you can just do a "save as" in quicktime to convert a video to quicktime format, but that won't change the time scale to 600. You have to copy the tracks of the video into a new window. Saving the new video creates a video with a time scale of 600 and by default saves the video in quicktime format. *)

-- this script does 2 things. Function 1) will check a folder full of files for videos and identify the video files that are not in quicktime format and/or do not have a time scale of 600. Function 2) will convert those identified files as explained above. Here's the details.

(* Function 1) will search every file of a chosen folder. The identified videos (ie. not quicktime format or not 600 time scale) will have their label color changed to blue so you can easily detect them. If an error occurrs during the check, the file's label color is turned red indicating that you will have to check this file by hand. Video files whose label color is not changed is already in quicktime format and has a time scale of 600. Nothing is needed to be done with those so they are left alone. *)

(* Function 2) will search every file of a chosen folder. Files whose label color is blue will be opened in quicktime and a new video file will be created from the tracks of the "blue" file. This new video file will be saved to another folder (chosen by you) and the new file's time scale will be 600. Upon successful conversion, the original "blue" colored file will have its label color changed to "green" indicating that the script successfully converted it. After the script is done you should manually verify that all of the original "blue" colored files were changed to "green" and that the newly created file works properly. *)

(* Note: annotations and Finder comments from the original video are copied to the new video *)

(* Note: if you only want to convert a few files from a folder, then you can just manually change their label color to blue and run function 2 on the folder. *)

property video_ext_list : {"3g2", "3gp", "3gp2", "3gpp", "3mm", "60d", "aep", "ajp", "amv", "asf", "asx", "avb", "avi", "avs", "bik", "bix", "box", "byu", "cvc", "dce", "dif", "dir", "divx", "dv", "dvr-ms", "dxr", "eye", "fcp", "flc", "fli", "flv", "flx", "gl", "grasp", "gvi", "gvp", "ifo", "imovieproject", "ivf", "ivs", "izz", "izzy", "lsf", "lsx", "m1v", "m2v", "m4e", "m4u", "m4v", "mjp", "mkv", "moov", "mov", "movie", "mp4", "mpe", "mpeg", "mpg", "mpv2", "msh", "mswmm", "mvb", "mvc", "nvc", "ogm", "omf", "prproj", "prx", "qt", "qtch", "rm", "rmvb", "rp", "rts", "sbk", "scm", "smil", "smv", "spl", "srt", "ssm", "svi", "swf", "swi", "tivo", "ts", "vdo", "vf", "vfw", "vid", "viewlet", "viv", "vivo", "vob", "vro", "wm", "wmd", "wmv", "wmx", "wvx", "yuv"} -- found here http://www.fileinfo.net/filetypes/video
global source_folder, destination_folder

-- setup the script
display dialog "As described in the script:" & return & return & "Function 1: change the label color of the files in the chosen folder which need to be converted." & return & return & "Function 2: convert the files with a \"blue\" label color to a new folder." buttons {"Cancel", "Function 2", "Function 1"} default button 3
set button_returned to button returned of the result

if button_returned is "Function 1" then
	-- choose the source folder and get all of its files
	set source_folder to choose folder with prompt "Choose the folder of movie files:"
	set source_folder to source_folder as string
	tell application "Finder" to set source_movies to every file of folder source_folder
	
	-- change the label colors of files whose file type in not quicktime and/or whose time scale is not 600
	repeat with i from 1 to count of source_movies
		set this_movie to item i of source_movies as string
		set qt_check to my ignore_turnBlue_qtCheck(this_movie)
		if qt_check then -- it's a possible quicktime file so check its time scale
			try -- first try to get the time scale using system events so we don't have to open it to check
				tell application "System Events" to set ts to time scale of contents of movie file this_movie
				if ts is not 600 then tell application "Finder" to set label index of file this_movie to 4
			on error
				tell application "Finder" to set label index of file this_movie to 2
			end try
		end if
	end repeat
	set the_dialog to "This script has finished." & return & return & "All video files in the chosen folder whose file type is not quicktime or whose time scale is not 600 have been turned blue. They are now ready to be processed with \"Function 2\"." & return & return & "Red colored files caused an error when they were checked, so check them by hand."
	
else if button_returned is "Function 2" then
	my getSource_destinationFolders()
	tell application "Finder" to set source_movies to every file of folder source_folder
	tell application "QuickTime Player"
		launch
		my supressAutoPlay(true) -- make sure movies set to auto play do not do that
	end tell
	with timeout of 86400 seconds -- ie. do not time out, this is 1 days worth of seconds
		repeat with i from 1 to (count of source_movies)
			set aMovie to item i of source_movies as string
			tell application "Finder" to if label index of file aMovie is 4 then
				try
					tell application "QuickTime Player"
						open aMovie
						rewind
						select none
						if saveable of movie 1 is true then
							-- get properties of the movie
							set window_bounds to bounds of window 1
							tell movie 1
								set orig_name to name
								set annNames to name of annotations
								set annValues to full text of annotations
								tell application "Finder" to set orig_comments to comment of file aMovie
							end tell
							
							-- get name of movie from file name
							set nmExt to my getName_andExtension(aMovie)
							set file_name to item 1 of nmExt
							
							-- copy and paste the movie into a new movie container
							tell application "QuickTime Player"
								tell movie orig_name
									rewind
									select all
									copy
								end tell
								make new movie
								tell movie 1
									paste
									rewind
									select at 0 to 0
								end tell
							end tell
							
							close movie orig_name saving no
							
							-- reposition the movie window to the original movie's position
							set bounds of window 1 to window_bounds
							
							-- set annotations to annotations from the original movie
							my dupAnns(1, annNames, annValues, file_name)
							
							-- save the movie to the destination folder
							set new_file to (destination_folder & file_name & ".mov") as string
							save self contained movie 1 in file new_file
							close movie 1 saving no
							
							-- set its comments to comments from original file
							tell application "Finder" to set comment of file new_file to orig_comments
							
							-- set the label color from blue to green indicating a successful operation
							tell application "Finder" to tell file aMovie to set label index to 6
						else
							display dialog "This movie has previously been set so that it cannot be copied, edited, or saved." & return & return & "Would you like to close this movie and continue processing the rest of the movies or stop the script?" buttons {"Close and Continue", "Stop"} default button 2
							set button_entered to button returned of result
							if button_entered is "Stop" then
								return
							else
								close movie 1 saving no
							end if
						end if
					end tell
				on error
					tell application "QuickTime Player" to if exists movie 1 then close every movie saving no
					tell application "Finder" to tell file aMovie to set label index to 2
				end try
			end if
		end repeat
	end timeout
	set the_dialog to "This script has finished converting the blue files in the chosen folder!"
end if

-- tell you that the script is finished
set frontApp to displayed name of (info for (path to frontmost application))
tell application frontApp to display dialog the_dialog buttons {"OK"} default button 1


(*================== SUBROUTINES ======================*)
on getName_andExtension(F)
	set F to F as string
	set {name:Nm, name extension:Ex} to info for file F
	if Ex is missing value then set Ex to ""
	if Ex is not "" then
		set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set Ex to "." & Ex
	end if
	return {Nm, Ex}
end getName_andExtension

on supressAutoPlay(status_flag)
	tell application "QuickTime Player"
		set ignore auto play to the status_flag
		set ignore auto present to the status_flag
	end tell
end supressAutoPlay

on getSource_destinationFolders()
	repeat
		set source_folder to choose folder with prompt "Choose the folder of movie files:"
		set destination_folder to choose folder with prompt "Choose a folder for the destination of the converted movies:"
		if source_folder is not destination_folder then
			set {source_folder, destination_folder} to {source_folder as string, destination_folder as string}
			exit repeat
		else
			display dialog "You cannot choose the same folder. Try again!"
		end if
	end repeat
end getSource_destinationFolders

on dupAnns(movie_name, annNames, annValues, file_name)
	tell application "QuickTime Player"
		tell movie movie_name
			repeat with i from 1 to (count of annNames)
				if exists annotation (item i of annNames) then
					set full text of annotation (item i of annNames) to (item i of annValues)
				else
					make new annotation with properties {name:(item i of annNames), full text:(item i of annValues)}
				end if
			end repeat
			if "Full Name" is not in annNames then
				make new annotation with properties {name:"Full Name", full text:file_name}
			end if
		end tell
	end tell
end dupAnns

on ignore_turnBlue_qtCheck(this_movie)
	set this_movie to this_movie as string
	set item_info to info for file this_movie
	set is_alias to alias of (info for file this_movie)
	tell item_info
		set is_folder to folder
		set ne to name extension
		set its_kind to kind
		set file_type to file type
	end tell
	if is_folder is true or is_alias is true then
		return false
	else
		if ne is missing value or ne is in video_ext_list then
			if its_kind is "QuickTime Movie" then
				return true
			else if file_type is "Moov" then
				return true
			else if ne is "mov" then
				return true
			else
				tell application "Finder" to set label index of file this_movie to 4
				return false
			end if
		else
			return false
		end if
	end if
end ignore_turnBlue_qtCheck

I updated the above script making changes to “Function 1”. Previously in order to check to time scale of a movie, it was required to first open the movie in quicktime player to make the check. I found that system events can perform this check without having to open the movie… so now the script uses system events. There’s also been some minor tweaks which don’t change anything other than making the script a little more streamlined.