You are here

public function video_ffmpeg::dimensions in Video 7

Same name and namespace in other branches
  1. 6.4 transcoders/video_ffmpeg.inc \video_ffmpeg::dimensions()
1 call to video_ffmpeg::dimensions()
video_ffmpeg::convert_video in transcoders/video_ffmpeg.inc

File

transcoders/video_ffmpeg.inc, line 539

Class

video_ffmpeg

Code

public function dimensions($video) {

  //lets setup our dimensions.  Make sure our aspect ratio matches the dimensions to be used, if not lets add black bars.
  $aspect_ratio = _video_aspect_ratio(drupal_realpath($video->uri));
  $ratio = $aspect_ratio['ratio'];
  $width = $aspect_ratio['width'];
  $height = $aspect_ratio['height'];
  $wxh = explode('x', $video->dimensions);
  $output_width = $wxh[0];
  $output_height = $wxh[1];
  $output_ratio = number_format($output_width / $output_height, 4);
  if ($output_ratio != $ratio && $width && $height) {
    $options = array();

    // Figure out our black bar padding.
    if ($ratio < $output_width / $output_height) {
      $end_width = $output_height * $ratio;
      $end_height = $output_height;
    }
    else {
      $end_height = $output_width / $ratio;
      $end_width = $output_width;
    }

    // We need to get back to an even resolution and maybe compare with our defaults?
    // @TODO Make this more exact on actual video dimensions instead of making sure the wxh are even numbers
    if ($end_width == $output_width) {

      // We need to pad the top/bottom of the video
      $padding = round($output_height - $end_height);
      $pad1 = $pad2 = floor($padding / 2);
      if ($pad1 % 2 !== 0) {
        $pad1++;
        $pad2--;
      }
      if (variable_get('video_ffmpeg_pad_method', 0)) {
        $options[] = '-vf "pad=' . round($output_width) . ':' . round($output_height) . ':0:' . $pad1 . '"';
      }
      else {
        $options[] = '-padtop ' . $pad1;
        $options[] = '-padbottom ' . $pad2;
      }
    }
    else {

      // We are padding the left/right of the video.
      $padding = round($output_width - $end_width);
      $pad1 = $pad2 = floor($padding / 2);

      //@todo does padding need to be an even number?
      if ($pad1 % 2 !== 0) {
        $pad1++;
        $pad2--;
      }
      if (variable_get('video_ffmpeg_pad_method', 0)) {
        $options[] = '-vf "pad=' . round($output_width) . ':' . round($output_height) . ':' . $pad1 . ':0"';
      }
      else {
        $options[] = '-padleft ' . $pad1;
        $options[] = '-padright ' . $pad2;
      }
    }
    $end_width = round($end_width) % 2 !== 0 ? round($end_width) + 1 : round($end_width);
    $end_height = round($end_height) % 2 !== 0 ? round($end_height) + 1 : round($end_height);

    //add our size to the beginning to make sure it hits our -s
    array_unshift($options, $end_width . 'x' . $end_height);
    return implode(' ', $options);
  }
  else {
    return $video->dimensions;
  }
}