static function video_consolidator($file, $new_width, $new_height) { $ffmpeg = new ffmpeg_wrapper(); $ffmpeg->add_input($file); $ffmpeg->video->qscale(0); // initialize flags $info = ffmpeg_wrapper::video_stream_info($file); $input_width = intval($info["width"]); $input_heigth = intval($info["height"]); $aspect_ratio = round(floatval($input_width / $input_heigth), 3); $new_aspect_ratio = round(floatval($new_width / $new_height), 3); /** * fix letterbox videos */ $ffmpeg->fix_letterbox($file, $new_width, $new_height); /** * convert vertical videos */ $ffmpeg->fix_vertical($file); /** * check if padding is needed */ $ffmpeg->fix_aspect_ratio_normal($aspect_ratio, $new_aspect_ratio); /** * scale to new given sizes */ if ($input_width != $new_width || $input_heigth != $new_height) { if (FFMPEG_WRAPPER_DEBUG_PRINTS) { echo ">> scale needed" . PHP_EOL; } $ffmpeg->video_filter->scale($new_width, $new_height); } else { if (FFMPEG_WRAPPER_DEBUG_PRINTS) { echo ">> no scale needed" . PHP_EOL; } } return $ffmpeg; }
public static function analyze_letterbox_video($file) { $info = ffmpeg_wrapper::video_stream_info($file); $duration = $info["duration"]; $ffmpeg = new ffmpeg_wrapper(); // $ffmpeg->add_global_param("ss",floor($duration/2)); $ffmpeg->add_input($file); $ffmpeg->audio->disable(); $ffmpeg->video->length(50); $ffmpeg->video->format("rawvideo"); $ffmpeg->video_filter->cropdetect(16, 2, 0); $ffmpeg->set_output("/dev/null"); $ffmpeg->run(); $crop_string_array = array(); foreach ($ffmpeg->response_string as $string) { // if the string include if (strpos($string, 'crop') !== false) { $splitString = explode(" crop=", $string); array_push($crop_string_array, $splitString[1]); } } // find most common crop recommendation $crop_map = array(); $common_crop = $crop_string_array[0]; $common_crop_count = 1; for ($i = 0; $i < sizeof($crop_string_array); $i++) { $current = $crop_string_array[$i]; if (!isset($crop_map[$current])) { $crop_map[$current] = 1; } else { $crop_map[$current]++; } if ($crop_map[$current] > $common_crop_count) { $common_crop = $current; $common_crop_count = $crop_map[$current]; } } if (FFMPEG_WRAPPER_DEBUG_PRINTS) { // echo "most common crop: " . $common_crop . PHP_EOL; } // check if crop is needed: $frame_size = explode(":", $common_crop); $aspect_ratio = explode(":", $info["display_aspect_ratio"]); $ratio = $aspect_ratio[0] / $aspect_ratio[1]; $crop_ratio = $frame_size[0] / $frame_size[1]; if (FFMPEG_WRAPPER_DEBUG_PRINTS) { // echo "input - width = " . $info["width"] . PHP_EOL; // echo "input - height = " . $info["height"] . PHP_EOL; // echo "input - aspect ratio = " . $ratio . PHP_EOL; // echo "crop frame - width = " . $frame_size[0] . PHP_EOL; // echo "crop frame - heigth = " . $frame_size[1] . PHP_EOL; // echo "crop frame - aspect ratio = " . $crop_ratio . PHP_EOL; } if ($frame_size[0] != $info["width"] || $frame_size[1] != $info["height"]) { return explode(":", $common_crop); } return null; }