/** * Creates a video title * @global string $ffmpegpath * @param absolute_path $movieout * @param assoc_array $title_elements * @param integer $width * @param integer $height * @param integer $duration * @param absolute_path $overlay_movie * @return false|error_string */ function movie_title($movieout, $title_elements, $encoder, $duration = 8) { global $ffmpegpath, $fontfile, $encoders_path, $aac_experimental; $movieout = escape_path($movieout); $encoder_values = explode('_', $encoder); $codec = $encoder_values[0]; $quality = $encoder_values[1]; $resolution_values = explode('x', $encoder_values[2]); $width = $resolution_values[0]; $height = $resolution_values[1]; $encoder = $encoders_path . '/' . $codec . '_' . $quality . '.ffpreset'; // checks if the encoder file exists if (!is_file($encoder)) { return "encoder not found {$encoder}"; } $drawtext_filter = ''; // draw album name if (isset($title_elements['album'])) { // drawtext filter cannot handle ':' $album = str_replace(":", "\\:", escape_path($title_elements['album'], false)); /** * fontfile : the font to use * text : the text to display * fontsize : the font size (determined following the video height) * x : the horizontal position (determined following the total width (w) and the text width (text_w)) * y : the vertical position (determined following the total height (h) and the text height (text_h)) * fontcolor : the color of the text */ $drawtext_filter .= "drawtext=fontfile={$fontfile}: text='{$album}':fontsize=({$height}/19): x=(w-text_w)/2: y=(h)/2-(6.5*text_h):fontcolor=white, "; } // draw title - if title is longer than 35 char, it is split in several lines if (isset($title_elements['title'])) { include_once 'lib_gd.php'; $title = split_title(str_replace(":", "\\:", escape_path($title_elements['title'], false))); foreach ($title as $index => $title_line) { $drawtext_filter .= "drawtext=fontfile={$fontfile}: text='{$title_line}':fontsize=({$height}/11.5): x=(w-text_w)/2: y=(h)/2-((3-{$index})*({$height}/18.5)):fontcolor=white, "; } } // draw the author if (isset($title_elements['author'])) { $author = str_replace(":", "\\:", escape_path($title_elements['author'], false)); $drawtext_filter .= "drawtext=fontfile={$fontfile}: text='{$author}': x=(w-text_w)/2: fontsize=({$height}/13): y=(h-text_h)/2+(2*text_h):fontcolor=white, "; } // draw the date - except if it is not required if (isset($title_elements['date']) && $title_elements['hide_date'] != true) { $date = str_replace(":", "\\:", escape_path($title_elements['date'], false)); $drawtext_filter .= "drawtext=fontfile={$fontfile}: text='{$date}': x=(w-text_w)/2: fontsize=({$height}/19): y=(h-text_h)/2+(6.5*text_h):fontcolor=white, "; } // draw copyrights if (isset($title_elements['copyright'])) { $copyright = str_replace(":", "\\:", escape_path($title_elements['copyright'], false)); $drawtext_filter .= "drawtext=fontfile={$fontfile}: text='{$copyright}': fontsize=({$height}/28.8): x=(w-text_w)/2: y=(h)-(4.5*text_h):fontcolor=white, "; } // draw organization name if (isset($title_elements['organization'])) { $organization = str_replace(":", "\\:", escape_path($title_elements['organization'], false)); $drawtext_filter .= "drawtext=fontfile={$fontfile}: text='{$organization}': fontsize=({$height}/28.8): x=(w-text_w)/2: y=(h)-(4*text_h):fontcolor=white, "; } // removes trailing ', ' from the last line if (strlen($drawtext_filter) > 2) { $drawtext_filter = substr($drawtext_filter, 0, -2); } if ($aac_experimental) { $aac_codec = ' -acodec aac -strict experimental '; // overwrites audio codec from encoders file } /* * generates input silent audio stream for titling: * -ar : audio sample rate * -ac : audio channels * -f : force audio input format (s161e required to generate the audio stream) * -t : duration in seconds * -i : input source * generates input video stream for titling : * -f : force video input format (lavfi required to generate the video stream) * -t : duration in secondes * -i : a colored screen with color (c) 0x... , size (s) $widthx$height and framerate (r) 25/1 * applies filters for output file * -vf : video filters to apply * -vpre : video preset file (settings used to encode the video) */ $cmd = $ffmpegpath . ' -ar 44100 -ac 2 -f s16le -t ' . $duration . ' -i /dev/zero ' . '-f lavfi -t ' . $duration . ' -i color=c=0x2578B6:s=' . $width . 'x' . $height . ':r=25 ' . '-vf "' . $drawtext_filter . '" -fpre ' . $encoder . ' ' . $aac_codec . ' -y ' . $movieout; exec($cmd, $cmdoutput, $returncode); print "\n{$cmd}\n"; //check returncode if ($returncode) { return join("\n", $cmdoutput); } return false; }
function gd_image_create($title_array, $width, $height, $file) { global $fontfile; global $quality; global $fontratio; global $linelength; // creates the image $img = imagecreate($width, $height); // defines the background color $bg_color = imagecolorallocate($img, 8, 89, 155); // setup the text color $txt_color['white'] = imagecolorallocate($img, 255, 255, 255); $txt_color['blue'] = imagecolorallocate($img, 105, 139, 205); if (isset($title_array['album'])) { // font size relative to the global height of the image $fontsize = $height / (32 * $fontratio); // gd_push_text($img, $fontsize, $txt_color['blue'], wordwrap($title_array['album'], 45,"\n"), ($height * 0.25)); $dimensions = imagettfbbox($fontsize, 0, $fontfile, $title_array['album']); $txt_height = $dimensions[1] - $dimensions[7]; // splits the string in multiple strings if its size is too long $album = split_title($title_array['album'], $linelength * 1.27); switch (count($album)) { case 1: $pct_height = 0.25; break; case 2: $pct_height = 0.2; break; case 3: $pct_height = 0.15; break; default: $pct_height = 0.25; break; } // adds each value of the array in the generated image foreach ($album as $index => $string) { gd_push_text($img, $fontsize, $txt_color['blue'], $string, $height * $pct_height + $index * ($txt_height + 5)); } } if (isset($title_array['title'])) { $fontsize = $height / (25 * $fontratio); $dimensions = imagettfbbox($fontsize, 0, $fontfile, $title_array['title']); $txt_height = $dimensions[1] - $dimensions[7]; // splits the string in multiple strings if its size is too long $title = split_title($title_array['title'], $linelength); // adds each value of the array in the generated image foreach ($title as $index => $string) { gd_push_text($img, $fontsize, $txt_color['white'], $string, $height * 0.4 + $index * ($txt_height + 5)); } } if (isset($title_array['author'])) { $fontsize = $height / (32 * $fontratio); gd_push_text($img, $fontsize, $txt_color['white'], $title_array['author'], $height * 0.7); } if (isset($title_array['date']) && $title_array['hide_date'] != true) { $fontsize = $height / (42 * $fontratio); gd_push_text($img, $fontsize, $txt_color['white'], $title_array['date'], $height * 0.75); } if (isset($title_array['copyright']) || isset($title_array['organization'])) { $string = $title_array['copyright'] . (isset($title_array['copyright']) && isset($title_array['organization']) ? " - " : "") . $title_array['organization']; $fontsize = $height / (55 * $fontratio); gd_push_text($img, $fontsize, $txt_color['blue'], escape_path($string), $height * 0.95); } // generates the image return imagejpeg($img, $file, $quality); }