Skip to content

Commit

Permalink
Support FPS for video transformations (#146)
Browse files Browse the repository at this point in the history
Add video `fps` transformation parameter
  • Loading branch information
cloudinaraz authored and const-cloudinary committed Oct 15, 2018
1 parent d7db89b commit 13ff888
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/Cloudinary.php
Expand Up @@ -576,6 +576,7 @@ public static function generate_transformation_string(&$options = array())
}

$video_codec = Cloudinary::process_video_codec_param(Cloudinary::option_consume($options, "video_codec"));
$fps = Cloudinary::process_fps(Cloudinary::option_consume($options, "fps"));

$overlay = Cloudinary::process_layer(Cloudinary::option_consume($options, "overlay"), "overlay");
$underlay = Cloudinary::process_layer(Cloudinary::option_consume($options, "underlay"), "underlay");
Expand All @@ -602,6 +603,7 @@ public static function generate_transformation_string(&$options = array())
"eo" => $end_offset,
"fl" => $flags,
"fn" => $custom_function,
"fps" => $fps,
"h" => self::normalize_expression($height),
"l" => $overlay,
"o" => self::normalize_expression($opacity),
Expand Down Expand Up @@ -907,10 +909,22 @@ private static function process_if($if)
return $if;
}

private static function float_to_string($value) {
if (!is_float($value)) {
return $value;
}

$locale = localeconv();
$string = strval($value);
$string = str_replace($locale['decimal_point'], '.', $string);

return $string;
}

private static function normalize_expression($exp)
{
if (is_float($exp)) {
return number_format($exp, 1);
return self::float_to_string($exp);
}
if (preg_match('/^!.+!$/', $exp)) {
return $exp;
Expand Down Expand Up @@ -1027,6 +1041,23 @@ private static function process_video_codec_param($param)
return $out_param;
}

/**
* Serializes fps transformation parameter
*
* @param mixed $fps A single number, an array of mixed type, a string, including open-ended and closed range values
* Examples: '24-29.97', 24, 24.973, '-24', [24, 29.97]
*
* @return string
*/
private static function process_fps($fps)
{
if (!is_array($fps)) {
return strval($fps);
}

return implode("-", array_map("self::normalize_expression", $fps));
}

// Warning: $options are being destructively updated!
public static function cloudinary_url($source, &$options = array())
{
Expand Down
25 changes: 25 additions & 0 deletions tests/CloudinaryTest.php
Expand Up @@ -1044,6 +1044,31 @@ public function test_video_codec()
CloudinaryTest::VIDEO_UPLOAD_PATH . "vc_h264:basic:3.1/video_id");
}

/**
* Should support a single number, an array of mixed type and a string, including open-ended and closed range values
*/
public function test_fps()
{
$fps_test_values = [
['24-29.97', 'fps_24-29.97'],
[24, 'fps_24'],
[24.973, 'fps_24.973'],
['24', 'fps_24'],
['-24', 'fps_-24'],
['$v', 'fps_$v'],
[[24, 29.97], 'fps_24-29.97'],
[['24', '$v'], 'fps_24-$v']
];

foreach ($fps_test_values as $value) {
$this->cloudinary_url_assertion(
"video_id",
array('resource_type' => 'video', 'fps' => $value[0]),
CloudinaryTest::VIDEO_UPLOAD_PATH . $value[1] . "/video_id"
);
}
}

public function test_audio_codec()
{
// should support a string value
Expand Down

0 comments on commit 13ff888

Please sign in to comment.