Nel tutorial di oggi vi mostreremo come prendere un filmato e convertirlo in filmato ASCII (è richiesto FFMPEG).
Attenzione, il file convertito sarà circa 10 volte più grande dell'originale ma il risultato è davvero fantastico!
Convert.php
< ?php /* * __Construct() $movieLocation: The complete file location of the movie to convert * $ffmpegLocation: The location where ffmpeg is installed */ class Convert{ public $movies = 'movies'; // folder where the movies are stored public $images = 'images'; // folder where the images are stored public $html = 'html'; // folder where final html is stored public $frameRate = '30'; // default frame rate public $XPixel = 2; // default x aspect ratio public $YPixel = 2; // default y aspect ratio public $tones = array("#", "=", "l", ";", " "); //tones darkest to lightest private $movieL = ''; private $ffmpegL = ''; private $fileName = ''; private $fileExt = ''; public function __construct($movieLocation, $ffmpegLocation = '/usr/bin/ffmpeg'){ $this->movieL = $movieLocation; $this->ffmpegL = $ffmpegLocation; } public function convert(){ $this->gatherInfo(); $this->movie2jpg(); $this->jpg2ascii(); } private function gatherInfo(){ $info = pathinfo($this->movieL); $this->fileName = $info['filename']; $this->fileExt = $info['extension']; return true; } private function movie2jpg(){ shell_exec($cmd = 'cd "' . getcwd() . '"; "' . $this->ffmpegL . '" -i "' . $this->movies . '/' . $this->fileName . '.' . $this->fileExt . '" -r ' . $this->frameRate . ' -f image2 "' . $this->images . '/' . $this->fileName . '-%03d.jpg"'); return true; } private function jpg2ascii(){ $images = glob(getcwd() . '/' . $this->images . '/' . $this->fileName . '*.jpg'); if(is_file(getcwd() . '/' . $this->html . '/' . $this->fileName . '.html')){ unlink(getcwd() . '/' . $this->html . '/' . $this->fileName . '.html'); } $handle = fopen(getcwd() . '/' . $this->html . '/' . $this->fileName . '.html', 'ab'); $interval = (int) 1000 / $this->frameRate; $js = '<script> var interval = ' . $interval . '; var inter; var cFrame = 0; var pre = document.getElementsByTagName("pre"); function playVideo(){ inter = setInterval("loop()", interval); } function loop(){ if(pre[cFrame+1]){ pre[cFrame].style.display = "none"; pre[cFrame+1].style.display = "block"; cFrame++; }else{ pre[cFrame].style.display = "none"; cFrame = 1; } } </script>'; $head = '<html><head>' . $js . '</head><body onload="playVideo();"> <style>body{line-height:2px;font-size:2px;letter-spacing:1px;text-align:center;}</style> '; fwrite($handle, $head); $i = 1; foreach($images as $image){ $img = imagecreatefromjpeg($image); $width = imagesx($img); $height = imagesy($img); $opt = ''; for($h = 0; $h < $height; $h+=$this->YPixel){ for($w = 0; $w < = $width; $w+=$this->XPixel){ $rgb = ImageColorAt($img, $w, $h); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> <img src='http://www.sastgroup.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> & 0xFF; $b = $rgb & 0xFF; if($w == $width){ $opt .= "\n"; }else{ // max = 768: white // min = 000: black $col = $r + $g + $b; if($col > 605) $txt = $this->tones[4]; elseif($col > 442) $txt = $this->tones[3]; elseif($col > 279) $txt = $this->tones[2]; elseif($col > 116) $txt = $this->tones[1]; else $txt = $this->tones[0]; $opt .= $txt; } } } imagedestroy($img); $style = ''; if($i != 1) $style = ' style="display:none;"'; fwrite($handle, ' <pre ' . $style . ' id="_' . $i . '">' . $opt . '
');
unlink($image);
$i++;
}
fwrite($handle, '

