PHP Classes
elePHPant
Icontem

File: examples/chaining-processes.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Oliver Lillie  >  PHP Video Toolkit  >  examples/chaining-processes.php  >  Download  
File: examples/chaining-processes.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Video Toolkit
Manipulate and convert videos with ffmpeg program
Author: By
Last change:
Date: 1 year ago
Size: 5,326 bytes
 

Contents

Class file image Download
<?php

   
include_once './includes/bootstrap.php';
   
   
ob_start();
   
    echo
'<p>This is an example to chain processing on from one output to another to another and so on.</p><hr />';
    echo
'<p>.mov status: <strong id="status-1">---</strong></p><br />';
    echo
'<p>resize mov status: <strong id="status-2">---</strong></p><br />';
    echo
'<p>jpeg from resized mov status: <strong id="status-3">---</strong></p><br />';
   
   
ob_flush();
   
    try
    {
       
$video = new \PHPVideoToolkit\Video($example_video_path, $config);
       
       
$progress_handler = new \PHPVideoToolkit\ProgressHandlerNative(null, $config);

       
$process = $video->extractSegment(new \PHPVideoToolkit\Timecode(10), new \PHPVideoToolkit\Timecode(20))
                        ->
saveNonBlocking('./output/big_buck_bunny.mov', null, \PHPVideoToolkit\Media::OVERWRITE_EXISTING, $progress_handler);
       
       
$dot_count = -1;
        while(
$progress_handler->completed !== true)
        {
           
$dot_count += 1;
           
$data = $progress_handler->probe(true);
            if(
$data['started'] === true)
            {
                echo
'<script>document.getElementById("status-1").innerHTML = '.json_encode('Encoding to mov '.$data['percentage'].'% '.str_pad('', $dot_count, '.')).'</script>';
                echo
'&nbsp;';
               
ob_flush();
               
//sleep(1);
               
if($dot_count > 10)
                {
                   
$dot_count = -1;
                }
            }
            else
            {
                echo
'<script>document.getElementById("status-1").innerHTML = '.json_encode('Waiting for encoding to start').'</script>';
                echo
'&nbsp;';
               
ob_flush();
            }
           
        }

        if(
$process->hasError() === true)
        {
            echo
'<script>document.getElementById("status-1").innerHTML = '.json_encode('mov encoding encountered an error: '.$process->getErrorCode().'.').'</script>';
           
ob_flush();
           
// an error was encountered, do something with it.
       
}
        else
        {
           
$mov = $process->getOutput();
           
            echo
'<script>document.getElementById("status-1").innerHTML = '.json_encode('mov Encoded OK, output: "'.$mov->getMediaPath().'".').'</script>';
           
ob_flush();
           
// encoding has completed and no error was detected so
            // we can get the output from the process.
       
}
       
       
$progress_handler = new \PHPVideoToolkit\ProgressHandlerNative(null, $config);
       
$format = $mov->getDefaultFormat('output');
        \
PHPVideoToolkit\Trace::vars($format);
       
       
$format->setVideoDimensions(100, 100);
       
$process = $mov->saveNonBlocking('./output/big_buck_bunny_resized.mov', $format, \PHPVideoToolkit\Media::OVERWRITE_EXISTING, $progress_handler);
       
       
$dot_count = -1;
        while(
$progress_handler->completed !== true)
        {
           
$dot_count += 1;
           
$data = $progress_handler->probe(true);
            if(
$data['started'] === true)
            {
                echo
'<script>document.getElementById("status-2").innerHTML = '.json_encode('Encoding to mov '.$data['percentage'].'% '.str_pad('', $dot_count, '.')).'</script>';
                echo
'&nbsp;';
               
ob_flush();
               
//sleep(1);
               
if($dot_count > 10)
                {
                   
$dot_count = -1;
                }
            }
            else
            {
                echo
'<script>document.getElementById("status-2").innerHTML = '.json_encode('Waiting for encoding to start').'</script>';
                echo
'&nbsp;';
               
ob_flush();
            }
           
        }

        if(
$process->hasError() === true)
        {
            echo
'<script>document.getElementById("status-2").innerHTML = '.json_encode('Encoding encountered an error: '.$process->getErrorCode().'.').'</script>';
           
ob_flush();
           
// an error was encountered, do something with it.
       
}
        else
        {
           
$resized_mov = $process->getOutput();
           
            echo
'<script>document.getElementById("status-2").innerHTML = '.json_encode('Encoded OK, output: "'.$output->getMediaPath().'".').'</script>';
           
ob_flush();
           
// encoding has completed and no error was detected so
            // we can get the output from the process.
       
}
       
    }
    catch(\
PHPVideoToolkit\FfmpegProcessOutputException $e)
    {
        echo
'<h1>Error</h1>';
        \
PHPVideoToolkit\Trace::vars($e);

        if(
$process->isCompleted())
        {
            echo
'<hr /><h2>Executed Command</h2>';
            \
PHPVideoToolkit\Trace::vars($process->getExecutedCommand());
            echo
'<hr /><h2>FFmpeg Process Messages</h2>';
            \
PHPVideoToolkit\Trace::vars($process->getMessages());
            echo
'<hr /><h2>Buffer Output</h2>';
            \
PHPVideoToolkit\Trace::vars($process->getBuffer(true));
        }
    }
    catch(\
PHPVideoToolkit\Exception $e)
    {
        echo
'<h1>Error</h1>';
        \
PHPVideoToolkit\Trace::vars($e->getMessage());
        echo
'<h2>\PHPVideoToolkit\Exception</h2>';
        \
PHPVideoToolkit\Trace::vars($e);
    }