<?php

include_once './includes/bootstrap.php';
try {
    $video = new \PHPVideoToolkit\Video('media/BigBuckBunny_320x180.mp4', $config);
    $process = $video->getProcess();
    //	$process->setProcessTimelimit(1);
    $output = $video->extractSegment(new \PHPVideoToolkit\Timecode(10), new \PHPVideoToolkit\Timecode(20))->save('./output/big_buck_bunny_' . time() . '.mp3', new \PHPVideoToolkit\AudioFormat_Mp3('output', '/opt/local/bin/ffmpeg', './tmp'));
    echo '<h1>Executed Command</h1>';
    \PHPVideoToolkit\Trace::vars($process->getExecutedCommand());
    echo '<hr /><h1>FFmpeg Process Messages</h1>';
    \PHPVideoToolkit\Trace::vars($process->getMessages());
    echo '<hr /><h1>Buffer Output</h1>';
    \PHPVideoToolkit\Trace::vars($process->getBuffer(true));
} catch (\PHPVideoToolkit\FfmpegProcessOutputException $e) {
    echo '<h1>Error</h1>';
    \PHPVideoToolkit\Trace::vars($e);
    $process = $video->getProcess();
    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);
}
<?php

include_once './includes/bootstrap.php';
try {
    $video = new \PHPVideoToolkit\Video('media/BigBuckBunny_320x180.mp4', $config);
    $process = $video->getProcess();
    //	$process->setProcessTimelimit(1);
    $output = $video->extractSegment(new \PHPVideoToolkit\Timecode(10), new \PHPVideoToolkit\Timecode(20))->save('./output/big_buck_bunny.3gp');
    echo '<h1>Executed Command</h1>';
    \PHPVideoToolkit\Trace::vars($process->getExecutedCommand());
    echo '<hr /><h1>FFmpeg Process Messages</h1>';
    \PHPVideoToolkit\Trace::vars($process->getMessages());
    echo '<hr /><h1>Buffer Output</h1>';
    \PHPVideoToolkit\Trace::vars($process->getBuffer(true));
} catch (\PHPVideoToolkit\FfmpegProcessOutputException $e) {
    echo '<h1>Error</h1>';
    \PHPVideoToolkit\Trace::vars($e);
    $process = $video->getProcess();
    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);
}
<?php

include_once './includes/bootstrap.php';
try {
    $video = new \PHPVideoToolkit\Video('media/BigBuckBunny_320x180.mp4', $config);
    $process = $video->getProcess();
    //	$process->setProcessTimelimit(1);
    $output = $video->extractSegment(new \PHPVideoToolkit\Timecode(10), new \PHPVideoToolkit\Timecode(20))->save('./output/big_buck_bunny.flv', null, \PHPVideoToolkit\Media::OVERWRITE_EXISTING);
    echo '<h1>Executed Command</h1>';
    \PHPVideoToolkit\Trace::vars($process->getExecutedCommand());
    echo '<hr /><h1>FFmpeg Process Messages</h1>';
    \PHPVideoToolkit\Trace::vars($process->getMessages());
    echo '<hr /><h1>Buffer Output</h1>';
    \PHPVideoToolkit\Trace::vars($process->getBuffer(true));
} catch (\PHPVideoToolkit\FfmpegProcessOutputException $e) {
    echo '<h1>Error</h1>';
    \PHPVideoToolkit\Trace::vars($e);
    $process = $video->getProcess();
    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);
}
 /**
  * See contract for documentation
  */
 public function loadMedia($media)
 {
     // Set up the basics
     $return_files = array('full' => '', 'chunks' => array());
     $source_media_path = $media->media_path;
     // Extract relevant pieces of the specified media path
     $parsed_url = parse_url($source_media_path);
     $media_scheme = $parsed_url['scheme'];
     $media_host = $parsed_url['host'];
     $media_user = array_key_exists('user', $parsed_url) ? $parsed_url['user'] : "";
     $media_path = $parsed_url['path'];
     $parsed_path = pathinfo($media_path);
     $file_type = array_key_exists('extension', $parsed_path) ? $parsed_path['extension'] : "mp3";
     // Set up the pieces of the destination file names
     $destination_file_base = $this->getFileBase($media);
     $destination_directory = env('FPRINT_STORE') . 'media_cache/';
     $temp_media_file = $destination_file_base . "." . $file_type;
     $temp_media_path = $destination_directory . $temp_media_file;
     // If the file doesn't exist, download it
     if (file_exists($temp_media_path)) {
         $return_files['full'] = $temp_media_file;
     } else {
         $temp_media_file = $this->loadFile($source_media_path, $destination_directory, $destination_file_base, $file_type);
         if ($temp_media_file == "") {
             throw new \Exception("Could not download the media: " . $source_media_path);
         }
         // Save the media path
         $return_files['full'] = $temp_media_file;
         // Set up PHPVideoToolkit (for use in the next steps)
         $config = new \PHPVideoToolkit\Config(array('temp_directory' => '/tmp', 'ffmpeg' => env('FFMPEG_BINARY_PATH'), 'ffprobe' => env('FFPROBE_BINARY_PATH'), 'yamdi' => '', 'qtfaststart' => ''), true);
         // Slice down the new media file if it has a listed start / duration
         if ($media->duration > 0) {
             // Extract the section we care about
             $start = new \PHPVideoToolkit\Timecode($media->start);
             $end = new \PHPVideoToolkit\Timecode($media->start + $media->duration);
             // Process differently based on the file type
             switch ($file_type) {
                 case 'mp3':
                 case 'wav':
                     $audio = new \PHPVideoToolkit\Audio($temp_media_path, null, null, false);
                     $command = $audio->extractSegment($start, $end);
                     break;
                 case 'mp4':
                     $video = new \PHPVideoToolkit\Video($temp_media_path, null, null, false);
                     $command = $video->extractSegment($start, $end);
                     break;
             }
             // We need to save as a separate file then overwrite
             $trimmed_media_path = $temp_media_path . "trimmed." . $file_type;
             $process = $command->save($trimmed_media_path, null, true);
             rename($trimmed_media_path, $temp_media_path);
         }
     }
     // Check if the file parts already exist
     $chunk_glob_path = $destination_directory . $destination_file_base . "_*." . $file_type;
     $chunk_paths = glob($chunk_glob_path);
     // If the chunks already exist use them, otherwise make them.
     if (sizeof($chunk_paths) > 0) {
         foreach ($chunk_paths as $chunk_path) {
             $return_files['chunks'][] = basename($chunk_path);
         }
     } else {
         // Calculate the number of chunks needed
         $parser = new \PHPVideoToolkit\MediaParser();
         $media_data = $parser->getFileInformation($temp_media_path);
         $slice_duration = env('FPRINT_CHUNK_LENGTH');
         $duration = $media_data['duration']->total_seconds;
         // Perform the actual slicing
         // TODO: there is an issue where slices that are too small might cause errors.
         // We will need to add logic to detect those and merge them into the previous slice
         if ($duration > env('FPRINT_CHUNK_LENGTH') + 60) {
             switch ($file_type) {
                 case 'mp3':
                 case 'wav':
                     $audio = new \PHPVideoToolkit\Audio($temp_media_path, null, null, false);
                     $slices = $audio->split(env('FPRINT_CHUNK_LENGTH'));
                     break;
                 case 'mp4':
                     $video = new \PHPVideoToolkit\Video($temp_media_path, null, null, false);
                     $slices = $video->split(env('FPRINT_CHUNK_LENGTH'));
                     break;
             }
             $process = $slices->save($destination_directory . $destination_file_base . "_%index." . $file_type);
             $output = $process->getOutput();
             // Get the filenames
             foreach ($output as $chunk) {
                 $return_files['chunks'][] = basename($chunk->getMediaPath());
             }
         } else {
             $copy_path = str_replace("." . $file_type, "_0." . $file_type, $temp_media_path);
             copy($temp_media_path, $copy_path);
             $return_files['chunks'][] = basename($copy_path);
         }
     }
     return $return_files;
 }
    foreach ($transcoders as $gif_transcoder) {
        $start = microtime_float();
        $output_path = './output/big_buck_bunny.' . $gif_transcoder . '.gif';
        if ($gif_transcoder === 'gifsicle-with-gd') {
            $config->convert = null;
            $config->gif_transcoder = 'gifsicle';
        } else {
            if ($gif_transcoder === 'gifsicle-with-convert') {
                $config->convert = $convert;
                $config->gif_transcoder = 'gifsicle';
            }
        }
        $output_format = \PHPVideoToolkit\Format::getFormatFor($output_path, $config, 'ImageFormat');
        $output_format->setVideoFrameRate(12);
        $video = new \PHPVideoToolkit\Video('media/BigBuckBunny_320x180.mp4', $config);
        $output = $video->extractSegment(new \PHPVideoToolkit\Timecode(10), new \PHPVideoToolkit\Timecode(70))->save($output_path, $output_format);
        $length = microtime_float() - $start;
        echo '<h1>' . str_replace('-', ' ', $gif_transcoder) . '</h1>';
        echo 'File = ' . $output_path . '<br />';
        echo 'Time to encode = ' . $length . '<br />';
        echo 'File size = ' . filesize($output_path) / 1024 / 1024 . ' MB<br />';
    }
} catch (\PHPVideoToolkit\FfmpegProcessOutputException $e) {
    echo '<h1>Error</h1>';
    \PHPVideoToolkit\Trace::vars($e);
    $process = $video->getProcess();
    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());