Skip to content

Instantly share code, notes, and snippets.

@jmather
Created March 18, 2011 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmather/875800 to your computer and use it in GitHub Desktop.
Save jmather/875800 to your computer and use it in GitHub Desktop.
This is before trying to make this code testable. This is one small piece of a larger class...
<?php
// ... snip ...
public function process(majaxMediaFileInfo $file_info, $new_width = null, $new_height = null, $crop_method = 'fit', $aspect_ratio = '16:9')
{
$name = $file_info->getName();
$sha1 = $file_info->getSha1();
$path = $this->path_builder->render($sha1);
$full_path = sfConfig::get('app_majax_media_cache_dir').DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$name;
if (!file_exists(sfConfig::get('app_majax_media_cache_dir').DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$name))
{
$this->ensurePath($path, sfConfig::get('app_majax_media_cache_dir'));
$this->file_helper->write($full_path, $file_info->getData());
}
$src_width = $file_info->getVideoWidth();
$src_height = $file_info->getVideoHeight();
if ($new_width !== null || $new_height !== null)
{
list($new_width, $new_height) = $this->getRatioDimensions($src_width, $src_height, $new_width, $new_height, $aspect_ratio);
} else {
list($new_width, $new_height) = array($src_width, $src_height);
}
$args = array('-i', $full_path, '-ar', '22050', '-b', '409600');
$translator_class = sfConfig::get('app_majax_media_video_transformation_builder', 'majaxMediaFFMpegVideoTransformationBuilder');
$translator_fit_class = sfConfig::get('app_majax_media_video_transformation_fit_builder', 'majaxMediaFFMpegVideoTransformationFitBuilder');
if ($c_m == 'fit')
{
$translator = new $translator_fit_class();
list($new_width, $new_height) = $translator->render($s_w, $s_h, $new_width, $new_height, $c_m);
} else {
$translator = new $translator_class();
$new_args = $translator->render($s_w, $s_h, $new_width, $new_height, $c_m);
$args = array_merge($args, $new_args);
}
$new_width = (ceil($new_width / 2) * 2);
$new_height = (ceil($new_height / 2) * 2);
$new_filename = $this->filename_builder->render($new_width, $new_height, $crop_method, $name, 'flv');
$new_partial_path = $path.DIRECTORY_SEPARATOR.$new_filename;
// start the transformation code...
$args[] = '-s';
$args[] = $new_width.'x'.$new_height;
$ffmpeg = sfConfig::get('app_majax_media_ffmpeg_path', '/usr/bin/ffmpeg');
// now we need to figure out the cropping/padding
$new_full_path = sfConfig::get('app_majax_media_cache_dir').DIRECTORY_SEPARATOR.$new_partial_path;
$args[] = $new_full_path;
if ($ffmpeg == false || !file_exists($ffmpeg))
{
trigger_error('FFMPEG Not installed. Video source will not be resized', E_WARNING);
$new_partial_path = $path.DIRECTORY_SEPARATOR.$name;
}
if (($ffmpeg != false && file_exists($ffmpeg)) && !file_exists($new_full_path))
{
foreach ($args as $i => $arg)
$args[$i] = escapeshellarg ($arg);
//echo($ffmpeg.' '.join(' ', $args));
$count = 0;
while ($this->file_helper->hasFileLock($full_path, false) || $this->file_helper->hasFileLock($new_full_path, false) == false)
{
usleep(500);
$count++;
if ($count == 10)
break;
}
if (!$this->file_helper->hasFileLock($full_path) && $this->file_helper->getFileLock($new_full_path))
{
$this->executor->setExecutable($ffmpeg);
$this->executor->setArguments($args);
$this->executor->execute();
$this->file_helper->removeFileLock($new_full_path);
}
}
$new_partial_path = '/'.sfConfig::get('majax_media_dir_name').'/'.$new_partial_path;
if ($path_only)
return $new_partial_path;
$render_class = sfConfig::get('app_majaxMedia_video_render', 'majaxMediaVideoRender');
$render = new $render_class();
return $render->render($this, $new_partial_path);
}
// ... snip ...
<?php
// ... snip ...
public function videoToString($path_only = false)
{
$name = $this->getVideoName();
$sha1 = $this->getVideoSha1();
$path = self::sha1ToPath($sha1);
$full_path = sfConfig::get('majax_media_dir').DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$name;
if (!file_exists(sfConfig::get('majax_media_dir').DIRECTORY_SEPARATOR.$path.DIRECTORY_SEPARATOR.$name))
{
self::ensurePath($path, sfConfig::get('majax_media_dir'));
$data = $this->getVideoData();
if (majaxMediaToolbox::getFileLock($full_path))
{
file_put_contents($full_path, $data);
majaxMediaToolbox::removeFileLock($full_path);
}
}
if ($this->get('width') !== null || $this->get('height') !== null)
{
$dims = $this->getRatioDimensions($this->get('width'), $this->get('height'), $this->getVideoWidth(), $this->getVideoHeight(), $this->get('aspect_ratio'));
$new_width = $dims[0];
$new_height = $dims[1];
} else {
$new_width = $this->getVideoWidth();
$new_height = $this->getVideoHeight();
}
$name_bits = explode('.', $name);
unset($name_bits[(count($name_bits) - 1)]);
$new_name = implode('.', $name_bits).'.flv';
$args = array('-i', $full_path, '-ar', '22050', '-b', '409600');
switch($this->get('crop_method'))
{
case 'center':
$ratio = min($new_height / $this->getVideoHeight(), $new_width / $this->getVideoWidth());
$height_check = round($this->getVideoHeight() * $ratio);
if ($height_check != $new_height)
{
$diff = (ceil(abs($new_height - $height_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-cropright';
$args[] = $diff_split;
$args[] = '-cropleft';
$args[] = $diff_split;
}
$width_check = round($this->getVideoWidth() * $ratio);
if ($width_check != $new_width)
{
$diff = (ceil(abs($new_width - $width_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-croptop';
$args[] = $diff_split;
$args[] = '-cropbottom';
$args[] = $diff_split;
}
break;
case 'left':
$ratio = min($new_height / $this->getVideoHeight(), $new_width / $this->getVideoWidth());
$height_check = round($this->getVideoHeight() * $ratio);
if ($height_check != $new_height)
{
$diff = (ceil(abs($new_height - $height_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-cropright';
$args[] = $diff;
}
$width_check = round($this->getVideoWidth() * $ratio);
if ($width_check != $new_width)
{
$diff = (ceil(abs($new_width - $width_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-croptop';
$args[] = $diff_split;
$args[] = '-cropbottom';
$args[] = $diff_split;
}
break;
case 'right':
$ratio = min($new_height / $this->getVideoHeight(), $new_width / $this->getVideoWidth());
$height_check = round($this->getVideoHeight() * $ratio);
if ($height_check != $new_height)
{
$diff = (ceil(abs($new_height - $height_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-cropleft';
$args[] = $diff;
}
$width_check = round($this->getVideoWidth() * $ratio);
if ($width_check != $new_width)
{
$diff = (ceil(abs($new_width - $width_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-croptop';
$args[] = $diff_split;
$args[] = '-cropbottom';
$args[] = $diff_split;
}
break;
case 'top':
$ratio = min($new_height / $this->getVideoHeight(), $new_width / $this->getVideoWidth());
$height_check = round($this->getVideoHeight() * $ratio);
if ($height_check != $new_height)
{
$diff = (ceil(abs($new_height - $height_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-cropright';
$args[] = $diff_split;
$args[] = '-cropleft';
$args[] = $diff_split;
}
$width_check = round($this->getVideoWidth() * $ratio);
if ($width_check != $new_width)
{
$diff = (ceil(abs($new_width - $width_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-cropbottom';
$args[] = $diff;
}
break;
case 'bottom':
$ratio = min($new_height / $this->getVideoHeight(), $new_width / $this->getVideoWidth());
$height_check = round($this->getVideoHeight() * $ratio);
if ($height_check != $new_height)
{
$diff = (ceil(abs($new_height - $height_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-cropright';
$args[] = $diff_split;
$args[] = '-cropleft';
$args[] = $diff_split;
}
$width_check = round($this->getVideoWidth() * $ratio);
if ($width_check != $new_width)
{
$diff = (ceil(abs($new_width - $width_check) / 2) * 2);
$diff_split = $diff / 2;
$args[] = '-croptop';
$args[] = $diff;
}
break;
case 'fit':
// fit
$ratio = min($new_height / $this->getVideoHeight(), $new_width / $this->getVideoWidth());
$height_check = round($this->getVideoHeight() * $ratio);
if ($height_check != $new_height)
{
$diff = $new_height - $height_check;
$diff_top = floor($diff / 2);
$diff_bot = $diff - $diff_top;
$new_height = $new_height - abs($diff);
}
$width_check = round($this->getVideoWidth() * $ratio);
if ($width_check != $new_width)
{
$diff = $new_width - $width_check;
$diff_l = floor($diff / 2);
$diff_r = $diff - $diff_l;
$new_width = $new_width - abs($diff);
}
}
$new_width = (ceil($new_width / 2) * 2);
$new_height = (ceil($new_height / 2) * 2);
$new_filename = $new_width.'x'.$new_height;
$new_filename .= '_'.$this->get('crop_method', 'fit').'_'.$new_name;
$new_partial_path = $path.DIRECTORY_SEPARATOR.$new_filename;
// start the transformation code...
$args[] = '-s';
$args[] = $new_width.'x'.$new_height;
$ffmpeg = sfConfig::get('app_majaxMedia_ffmpeg_path', '/usr/bin/ffmpeg');
// now we need to figure out the cropping/padding
$new_full_path = sfConfig::get('majax_media_dir').DIRECTORY_SEPARATOR.$new_partial_path;
$args[] = $new_full_path;
if ($ffmpeg == false || !file_exists($ffmpeg))
{
trigger_error('FFMPEG Not installed. Video source will not be resized', E_WARNING);
$new_partial_path = $path.DIRECTORY_SEPARATOR.$name;
}
if (($ffmpeg != false && file_exists($ffmpeg)) && !file_exists($new_full_path))
{
foreach ($args as $i => $arg)
$args[$i] = escapeshellarg ($arg);
//echo($ffmpeg.' '.join(' ', $args));
$count = 0;
while (majaxMediaToolbox::hasFileLock($full_path) && !majaxMediaToolbox::hasFileLock($new_full_path))
{
usleep(500);
$count++;
if ($count == 10)
break;
}
if (!majaxMediaToolbox::hasFileLock($full_path) && majaxMediaToolbox::getFileLock($new_full_path))
{
exec ($ffmpeg . " " . join (" ", $args));
majaxMediaToolbox::removeFileLock($new_full_path);
}
}
$new_partial_path = '/'.sfConfig::get('majax_media_dir_name').'/'.$new_partial_path;
if ($path_only)
return $new_partial_path;
$render_class = sfConfig::get('app_majaxMedia_video_render', 'majaxMediaVideoRender');
$render = new $render_class();
return $render->render($this, $new_partial_path);
}
// ... snip ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment