Create a mosaic of movie clips in QuickTime Player

May 18, '09 07:30:04AM

Contributed by: yossie

Like many out there, I like to download clips from various video sites, as well as make my own. Being a child of the modern age, with attention spans being what they are, I like watching many clips at once in some cases.

I make this easier, I wrote this script as a "front end" for QuickTime Player. It will accept drag-and-drop files and open them in the player. It can also be double-clicked to run on all open files in the player. Either way, it will figure out the best use of the space available on the display (actually it uses the Desktop bounds; not quite what I want, but it works). It then distributes the windows into a mosaic (matrix) and starts them playing. It also sets looping on, and turns off player controls (wish I could get rid of title bars, too!).

Here's the code:

on open filelist
  tell application "QuickTime Player" to open filelist
  run
end open
on run
  tell application "QuickTime Player"
    set ratio to 4 / 3
    
    tell application "Finder" to set display_bounds to bounds of window of desktop
    set display_width to (item 3 of display_bounds)
    set display_height to (item 4 of display_bounds) - 42 -- menu height + title bar
    set window_count to count of windows
    set max_pixels to 0
    repeat with potential_cols from 1 to window_count -- try all possibilities - hardly optimal but who cares.
      set potential_rows to round (window_count - 1) / potential_cols + 1 rounding toward zero
      set {potential_window_width, potential_window_height} to {round display_width / potential_cols rounding toward zero, round display_height / potential_rows rounding toward zero}
      if potential_window_width / potential_window_height < ratio then
        set {potential_window_width, potential_window_height} to {potential_window_width, round potential_window_width / ratio rounding toward zero}
      else
        set {potential_window_width, potential_window_height} to {potential_window_height * ratio, potential_window_height}
      end if
      set used_pixels to potential_window_width * potential_window_height * window_count
      if used_pixels > max_pixels then
        set {window_width, window_height, cols, rows} to {potential_window_width, potential_window_height, potential_cols, potential_rows}
        set max_pixels to used_pixels
      end if
    end repeat
    
    set {x, y} to {0, 0}
    set wins to (get every window)
    repeat with win in wins
      set doc to document of win
      set controller type of doc to none
      set looping of doc to true
      set {wi, hi} to natural dimensions of doc
      if wi / window_width > hi / window_height then
        set dimensions of doc to {window_width, hi / (wi / window_width)}
      else
        set dimensions of doc to {wi / (hi / window_height), window_height}
      end if
      set x to x + 1
      if x = cols then set {x, y} to {0, y + 1}
    end repeat
    
    set {x, y} to {0, 0}
    set wins to (get every window)
    repeat with win in wins
      set {wi, hi} to natural dimensions of doc
      if wi / window_width > hi / window_height then
        set bounds of win to {window_width * x, 22 + window_height * y, window_width * x + window_width, 22 + window_height * y + hi / (wi / window_width)}
      else
        set bounds of win to {window_width * x, 22 + window_height * y, window_width * x + wi / (hi / window_height), 22 + window_height * y + window_height}
      end if
      set x to x + 1
      if x = cols then set {x, y} to {0, y + 1}
    end repeat
    set wins to (get every window)
    repeat with win in wins
      play document of win
    end repeat
    activate
  end tell
end run
Copy and paste the above into Script Editor, and save it as an Application. That's it. To use it, drag/drop movies onto the app, or just double-click it to run.

I opened about 50 files this way as a test, and while it took a bit of time to complete the task, they all played mostly at full FPS on my MacBook Pro -- kind of impressive, actually.

[robg adds: This worked when I tested it, though I used substantially less than 50 different clips.]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090515094704521