Importing Movies and movie importers

Importing movies is similar to importing graphics, the differences are described below.

A movie importer cannot create a graphic exporter, to export movie frames they have to be drawn to a graphic/window document and then exported from there. A movie importer cannot specify the graphics mode to draw with, and only draws using the dither copy graphics mode. The dither copy mode is the default graphics mode of a graphic importer.

As well as Quicktime movie files, the movie importer imports flash, gif and pdf files.

Movie importers provide a mechanism for accessing the individual frames of a movie. Apart from these differences please refer to the Importing graphics documentation on how to use the features of a movie importer common to graphic and movie importers. To import a movie file use the import movie command, see the script below for an example.

Accessing frames of a movie

A movie importer gives you access to individual and sync frames of a movie. The drawing samples property of the movie importer returns all the times when the movie will draw. Because a movie can contain a very large number of frames the maximum samples property restricts the maximum number of frames that can be returned in one go. Its initial value is 1000, but it can be set to -1 which indicates that all drawing frame times should be returned by the drawing samples property. A 1 hour long movie at a frame rate of 20 frames per second will return a list of 72000 times, this may be more than what you would like to handle. Because the drawing samples may not return all the frames at once, it starts from the current movie time. The current time property allows you to set or find out the current movie time. All the movie times are measured in time scale units. Time scale is nearly always 600, and this value indicates that there are 600 time units per second. If the sample duration returns 30 and the time scale is 600, then the sample duration is 30/600 of a second which is equal to 1/20 of a second, or to think of it as a frame rate it is 20 frames per second. The current time property is interpreted in the same way, if the current time returns 6000 then the time in seconds from the beginning of the movie is 6000/600 which is 10.

The following script will display the number of frames in a movie file in the results section of Script Editor. On Panther the following script can be opened in Script Editor by clicking here.

tell application "iMagine Photo"
  
set thisFile to choose file with prompt "Select a movie file: "
  
set thisMovie to import movie thisFile
  
set maximum samples of thisMovie to -1
  
set frameTimes to the drawing samples of thisMovie
  
close thisMovie
end tell
count of frameTimes

To keep movie files as small as possible a number of compression techniques are used. One of the techniques commonly used is to keep track of what area of the movie view does not change between frames, this is called temporal compression. The result is that when this type of compression is used most frames will not draw correctly unless the frames before it have been drawn as well. Temporal compression relies on special frames called sync frames which redraw the whole of the movie view and the information about which frames are sync frames is stored in the movie file. The following script will display the number of sync frames in a movie file in the results section of Script Editor. On Panther the following script can be opened in Script Editor by clicking here.

tell application "iMagine Photo"
  
set thisFile to choose file with prompt "Select a movie file: "
  
set thisMovie to import movie thisFile
  
set maximum samples of thisMovie to -1
  
set frameTimes to the sync samples of thisMovie
  
close thisMovie
end tell
count of frameTimes

The following lines calculate the length of the movie in seconds.

set theDuration to the movie duration of thisMovie
set theTimeScale to the time scale of thisMovie
set theDurationInSeconds to theDuration / theTimeScale

As well as drawing samples and sync samples the movie importer provides a way to obtain the next or previous frame relative to the current frame. The following script demonstrates how to draw each frame one after the other, how to scale the frames of a movie, and how to position where the movie frames draw. On Panther to open this Script Editor window click here.

tell application "iMagine Photo"
  
set thisFile to choose file with prompt "Select a movie file: "
  
set theScale to the (text returned of (display dialog "Enter the movie scale (1.0 leave at original size): " default answer "2.0")) as real
  
set thisMovie to import movie thisFile
  
set {x, y, xDim, yDim} to the natural bounds of thisMovie
  
if xDim * theScale is greater than 1024 then
    
set theScale to 1024 / xDim
  
end if
  
set thisDocument to make new window document with properties {dimensions:{xDim * theScale, yDim * theScale}}
  
set the drawing destination of thisMovie to thisDocument
  
set the scale of thisMovie to {theScale, theScale}
  
set the top left point of thisMovie to {0, 0}
  set current time of thisMovie to the first video sample of thisMovie
  
repeat
    
draw thisMovie
    
set nextSampleTime to the next video sample of thisMovie
    
if nextSampleTime is equal to -1 then
      
exit repeat
    
end if
    
set current time of thisMovie to nextSampleTime
  
end repeat
  
close thisMovie
  
close thisDocument
end tell

Also note that next video sample returns -1 when the current time is already set to the end of the movie and I have used this in the above script to stop drawing movie frames.

Movies files can contain meta data like copyright and this can be obtained by using the exif data property described in the Read/Write Exif metadata documentation.

The ability to open scripts in a new Script Editor window is provided by an application called "Convert Script to Markup Code" and can be obtained from http://homepage.mac.com/jonn8/as/

keywords: AppleScript, Apple Script, jpeg, jpg, image processing, graphics, Macintosh, Quicktime, movies.