Capturing Desktop Video with AppleScript and QuickTime

While trying to improve my Sikuli testing pipeline I decided it would be really handy to be able to capture videos of the running tests. For the time being this is kind of naive and just captures the entire screen (these are going to be really big when there are errors that take time to be resolved...) but I decided I can live with that for now just to have the capability.

I was pointed to a post on superuser by another Sikuli user and used that as a basis to develop my own script. The example on superuser actually turned out to not work and I needed some additional capabilities anyway.

The first major change I made was to split the stop and start of recording into two separate scripts. This allows me to start the recording at the beginning of a [testrun] and then stop it later. This actually turned out to show another problem so I needed to add a timeout to the start script so it wouldn't quit QuickTime before the stop script could be run.


tell application "QuickTime Player"
  with timeout of 1600 seconds
    --activate new screen recording
    start document 1
  end timeout  
end tell

This can be executed using the applescript command line tool using:

osascript startrecord.applescript

For stopping I wanted the ability to pass in a parameter with the filename... but again the script is pretty simple.


on run argv
  tell application "QuickTime Player"
    stop document 1
    tell document 1 to
    save in item 1 of argv
    close every document saving no
  end tell
end run

This can be run like this:

osascript startrecord.applescript /tmp/

Just remember to pass it a file name (not sure if an absolute path is required).

And that's pretty much the basics. There's some other stuff I did as far as where I save files and gathering artifacts inside Jenkins. But that's more specific to the way I have my Sikuli testing environment set up.

Hopefully this will help someone.

Update: So when I went to actually deploy this on OS X 10.7 I've run into a whole pile of issues. It seems that when you try and call save in 10.7 quicktime just ignores the call. Or something like that. The script will hang there forever and then time out. I still haven't found a solution for this. But this is making me really annoyed. Might have to find another solution entirely.

Update 2: #@^&%!@^#%$#!@! After spending the whole day on this... failure. I can't seem to get QuickTime's save or export capabilities to work correctly via AppleScript in Lion. Save hangs and export requires a preset file which I can't seem to generate. WTF. So I went and figured out how to do this whole thing using VLC... only to discover that screen capture doesn't work on Lion. F'you Apple.

If anyone is interested here's how I solved the problem on 10.6. First you need to start VLC in daemon mode. This starts it and save the output to /tmp/vlc.mp4 and the pid of the daemon process to /tmp/

/Applications/ screen:// --screen-fps=25 --quiet --sout "#transcode{vcodec=h264,vb=3072}:standard{access=file,mux=mp4,dst=/tmp/vlc.mp4}" --no-interact --pidfile /tmp/ -d

Then when you're ready to finish recording... you just send it a SIGTERM like this:

kill -15 $(cat /tmp/

But suffice to say... gah! What an annoying day. I'll probably spend some more time on this in the future... but this is enough hair pulling for one day.