[PHP] Convertire un filmato in filmato ASCII

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, '');
fclose($handle);
}

}

index.php

 
< ?php
set_time_limit(0);
ini_set('memory_limit', '1024M');
if (isset($_POST['submit'])) {
    require_once 'Convert.php';
    if (!empty($_FILES['movie']['name'])) {
        // Upload the file
        $temp = $_FILES['movie']['tmp_name'];
        $pathinfo = pathinfo($_FILES['movie']['name']);
        $name = md5($_FILES['movie']['name']) . '.' . $pathinfo['extension'];
        move_uploaded_file($temp, getcwd() . '/movies/' . $name);
        // Create an instance of convert
        $convert = new Convert('movies/' . $name);
        // Set the framerate
        if ($_POST['frameRate'] > 30)
            $convert->frameRate = 30;
        elseif ($_POST['frameRate'] < 1)
            $convert->frameRate = 1;
        else
            $convert->frameRate = (int) $_POST['frameRate'];
        // Set ascii video's height/width
        $convert->XPixel = (int) $_POST['X'];
        $convert->YPixel = (int) $_POST['Y'];
        // Convert it!
        $convert->convert();
        // Redirect to the newly created ascii video
        if ($_POST['autostart'] == 1) {
            header("Location: html/" . md5($_FILES['movie']['name']) . ".html");
            exit;
        }
        echo "
 
Complete! <a href='html/" . md5($_FILES['movie']['name']) . ".html'>Your HTML ASCII Video</a>
 
";
    } else {
        echo "
<p style='font-weight:bold;color: #990000;'>You need to select a video to upload
 
";
    }
}
?> 
<form action="" method="post" enctype="multipart/form-data">
 
 
        Video:
<input type="file" name="movie" />
        Autostart after upload:
<input type="checkbox" name="autostart" value="1" />
 
 
        <b>Note:</b> It is recommended that you only upload a 5MB file, (7MB is max)
 
<hr />
<h2>Other parameters</h2>
 
 
        The larger your file size the more you need to change these. Bring the frame rate down and/or increase the aspect ratio
 
 
 
        Frame Rate:
<input type="text" name="frameRate" size="2" value="15" /> (max: 30, min: 1)
 
 
 
        Aspect Ratio:
<input type="text" name="X" size="2" value="2" />:
<input type="text" name="Y" size="2" value="2" />
        (1:1 = full size, 2:2 = half size, 4:4 = quarter size, etc.)
 
<input type="submit" name="submit" value="Upload" />
</form>