 libraries/phpvideotoolkit/phpvideotoolkit.php5.php | 49 +++++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

diff --git a/libraries/phpvideotoolkit/phpvideotoolkit.php5.php b/libraries/phpvideotoolkit/phpvideotoolkit.php5.php
index 6463e50..e4d8549 100644
--- a/libraries/phpvideotoolkit/phpvideotoolkit.php5.php
+++ b/libraries/phpvideotoolkit/phpvideotoolkit.php5.php
@@ -280,6 +280,14 @@ class PHPVideoToolkit {
   protected $_log_file = NULL;
 
   /**
+   * Set to non-zero to adjust frame size to be a multiple of.
+   * @access public
+   * @var integer
+   */
+  public $width_multiple = 0;
+  public $height_multiple = 0;
+
+  /**
    * Determines if when outputting image frames if the outputted files should have the %d number
    * replaced with the frames timecode.
    * @var boolean If TRUE then the files will be renamed.
@@ -1435,6 +1443,15 @@ class PHPVideoToolkit {
 // <-		   		exits
       }
     }
+    if ($video_codec == 'libtheora') {
+      // Special case for libtheora which demands the frame size to be multiples of 16x16
+      $this->width_multiple = 16;
+      $this->height_multiple = 16;
+    }
+    else {
+      $this->width_multiple = 0;
+      $this->height_multiple = 0;
+    }
     return $this->addCommand('-strict experimental -vcodec', $video_codec);
   }
 
@@ -1582,6 +1599,22 @@ class PHPVideoToolkit {
   }
 
   /**
+   * Adjust size to be a multiple of a given integer.
+   * @param integer $size
+   * @param integer $multiple
+   * @return integer How much to pad on each end to make the adjustment.
+   */
+  private function checkVideoSize($size, $multiple) {
+    $extra = 0;
+    if ($multiple > 0 && $size % $multiple != 0) {
+      // Round up
+      $size_adj = ((int)(($size + $multiple - 1) / $multiple)) * $multiple;
+      $extra = ((int)(($size_adj - $size) / 4)) * 2;
+    }
+    return $extra;
+  }
+
+  /**
    * Sets the video output dimensions (in pixels)
    *
    * @access public
@@ -1636,7 +1669,11 @@ class PHPVideoToolkit {
           return $this->_raiseError('setVideoOutputDimensions_sas_dim');
         }
         else {
-          $width = $info['video']['dimensions']['width'] . 'x' . $info['video']['dimensions']['height'];
+          $w = $info['video']['dimensions']['width'];
+          $h = $info['video']['dimensions']['height'];
+          $hor_pad = $this->checkVideoSize($w, $this->width_multiple);
+          $ver_pad = $this->checkVideoSize($h, $this->height_multiple);
+          $width = $w . 'x' . $h;
         }
       }
     }
@@ -1647,6 +1684,8 @@ class PHPVideoToolkit {
         return $this->_raiseError('setVideoOutputDimensions_valid_integer');
 // <-				exits
       }
+      $hor_pad = $this->checkVideoSize($width, $this->width_multiple);
+      $ver_pad = $this->checkVideoSize($height_split[0], $this->height_multiple);
       $width = $width . 'x' . $height_split[0];
     }
     $this->addCommand('-s', $width);
@@ -1665,6 +1704,14 @@ class PHPVideoToolkit {
         }
       }
     }
+    if ($hor_pad) {
+      $this->addCommand('-padleft', $hor_pad);
+      $this->addCommand('-padright', $hor_pad);
+    }
+    if ($ver_pad) {
+      $this->addCommand('-padtop', $ver_pad);
+      $this->addCommand('-padbottom', $ver_pad);
+    }
     return TRUE;
   }
 
