Applescript save command for QuickTime Player

applescriptconversionquicktime

I've installed the Perian addon for Quicktime so it can open .flv files, and then I can save them as .m4v or .mov. I'm trying to make an Applescript to convert from .flv to .m4v automatically by using this tutorial and butchering their example applescript file, which normally converts ChemDraw files (.cdx, .cml, .mol) to .tiff, so that it instead uses Quicktime to save the .flv files as .m4v. When I try to use it, though, I get an error "QuickTime Player got an error: document 1 doesn't understand the save message". My save message is currently:

save first document in target_path as ".m4v"

which looks like the QuickTime dictionary's instructions:

save specifier : The document(s) or window(s) to save.

[as saveable file format] : The file format to use.

I've also tried "m4v", without the period, and still get the error.
Is my Save direction wrong, or is it probably an error from trying to use Quicktime instead of the original ChemDraw? I tried to change references to .cdx, .cml, .mol, .tiff, and ChemDraw to .flv, .m4v, and QuickTime respectively, but maybe it's more complicated than that?

Best Answer

This answer is hella long because your comments seem to indicate that your main interest is AppleScript itself, not converting videos from FLV to MP4. If this is the case, I strongly recommend starting with a good AppleScript reference, reading carefully and learning to use the tools available to you (especially Script Editor and application dictionaries). Trying to learn a new language by modifying existing applications is not good for debugging and can end in very bad things like cargo cult programming. That said,


I'd start by opening QuickTime Player's dictionary (File->Open Dictionary... in Script Editor) to see what commands are available. In my version of QuickTime Player (7.6.4), there's an export command in the QuickTime Player Suite:

export v : Export a movie or track to a file

   export reference : the movie or track to export
   to file : the destination file
   as AIFF/Apple TV/AVI/BMP/DV stream/Fast Start QTVR Movie/FLC/hinted
      movie/image sequence/interframe compressed VR object movie/iPhone/
      iPhone cellular/iTunes/MuLaw/MPEG2/MPEG4/picture/QuickTime media
      link/QuickTime movie/QuickTime TeXML/standard MIDI/System 7 sound/
      text file/ThreeGPP/wave : the desired file type
   [using default settings/most recent settings] : the export settings
      to use
   [using settings preset string] : the name of the export settings
      preset to use
   [using settings file] : the file containing the export settings
   [replacing boolean] : should the original file be deleted first?

A little Googling suggests that the "iPhone" file type refers to a .m4v file, so a first step might be replacing save first document in target_path as ".m4v" with export first document to target_path as iPhone. Looking through the dictionary a little more, though, shows that there's also a can export command:

can export v : Determine if a movie or track can be exported to the desired
   type

   can export reference : the movie or track to export
      as AIFF/Apple TV/AVI/BMP/DV stream/Fast Start QTVR Movie/FLC/hinted
         movie/image sequence/interframe compressed VR object movie/iPhone/
         iPhone cellular/iTunes/MuLaw/MPEG2/MPEG4/picture/QuickTime media
         link/QuickTime movie/QuickTime TeXML/standard MIDI/System 7 sound/
         text file/ThreeGPP/wave : the desired file type
      → boolean : is the export supported

So we should check that it's possible to export the movie in iPhone/.m4v format before actually doing so:

if (can export first document as iPhone) then
   export first document to target_path as iPhone
else
   error "Cannot export " & (source_file as string) & " in .m4v (iPhone) format."
end if

If we stop here, though, we may notice that some of the output files don't play correctly beyond a certain point, because QuickTime can load files asynchronously (i.e., not all at once). We should try to check that QuickTime Player has finished loading a movie before we tell it to export; by examining the full list of load states listed in the dictionary and assuming that every movie eventually ends up in a finished state or an error state, we can add this relatively easily.

set error_states to {load state unknown, load error}
set successful_states to {loaded, complete}
repeat until load state of first document is in (error_states & successful_states)
   delay 0.1
end repeat

if (load state of first document is in successful_states) then
   if (can export first document as iPhone) then
      export first document to target_path as iPhone
   else
      error "Cannot export " & (source_file as string) & " in .m4v (iPhone) format."
   end if
else
   error "File is not in a successful load state: " & (load state of first document as string)
end if
Related Question