PHP code example of buggedcom / phpvideotoolkit

1. Go to this page and download the library: Download buggedcom/phpvideotoolkit library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

buggedcom / phpvideotoolkit example snippets


namespace PHPVideoToolkit;

$config = new Config(array(
	'temp_directory' => './tmp',
	'ffmpeg' => '/opt/local/bin/ffmpeg',
	'ffprobe' => '/opt/local/bin/ffprobe',
	'yamdi' => '/opt/local/bin/yamdi',
	'qtfaststart' => '/opt/local/bin/qt-faststart',
	'cache_driver' => 'InTempDirectory',
), true);

namespace PHPVideoToolkit;

$ffmpeg = new FfmpegParser();
$is_available = $ffmpeg->isAvailable(); // returns boolean
$ffmpeg_version = $ffmpeg->getVersion(); // outputs something like - array('version'=>1.0, 'build'=>null)
	

namespace PHPVideoToolkit;

$parser = new MediaParser();
$data = $parser->getFileInformation('BigBuckBunny_320x180.mp4');
echo '<pre>'.print_r($data, true).'</pre>';
	

namespace PHPVideoToolkit;

$timecode = new Timecode(102.34);
$timecode = new Timecode(102.34, Timecode::INPUT_FORMAT_SECONDS);
$timecode = new Timecode(1.705666667, Timecode::INPUT_FORMAT_MINUTES);
$timecode = new Timecode(.028427778, Timecode::INPUT_FORMAT_HOURS);
$timecode = new Timecode('00:01:42.34', Timecode::INPUT_FORMAT_TIMECODE);


namespace PHPVideoToolkit;

$timecode = new Timecode('00:01:42.34', Timecode::INPUT_FORMAT_TIMECODE);
$timecode->hours += 15; // 15:01:42.34
$timecode->seconds -= 54125.5; // 00:00:18.84
$timecode->milliseconds -= 18840; // 00:00:00.00

// ...

$timecode->setSeconds(193.7);
echo $timecode; // Outputs '00:03:13.70'

// ...

$timecode->setTimecode('12:45:39.01');
echo $timecode->total_seconds; // Outputs 45939.01
echo $timecode->seconds; // Outputs 39


namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');

$output_format = new VideoFormat_Mp4();
// attempt to auto rotate the video to the correct orientation (ie mobile phone users - hurgygur)
$output_format->setVideoRotation(true)
			  ->setVideoFrameRate(10)
			  ->setVideoPixelFormat('rgb24')
			  ->setAudioSampleFrequency(44100);

$video->save('output.mp4', $output_format);
				

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$video->save('output.my_silly_custom_file_extension', new ImageFormat_Jpeg());
				

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->extractFrame(new Timecode(40))
				->save('./output/big_buck_bunny_frame.jpg');
$output = $process->getOutput();

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->extractFrames(new Timecode(40), new Timecode(50))
				->save('./output/big_buck_bunny_frame_%timecode.jpg');
$output = $process->getOutput();

namespace PHPVideoToolkit;

$output_format = new ImageFormat_Jpeg();

/*
OR 

$output_format = new VideoFormat();
$output_format->setFrameRate(1);
// optionally also set the video and output format, however if you use the ImageFormat_Jpeg 
// output format object this is automatically done for you. If you do not add below, FFmpeg
// automatically guesses from your output file extension which format and codecs you wish to use.
$output_format->setVideoCodec('mjpeg')
			  ->setFormat('image2');

*/

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->extractFrames(null, new Timecode(50)) // if null then the extracted segment starts from the begining of the video
				->save('./output/big_buck_bunny_frame_%timecode.jpg', $output_format);
$output = $process->getOutput();

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->extractFrames(new Timecode(50), null, 1) // if null then the extracted segment goes from the start timecode to the end of the video
				->save('./output/big_buck_bunny_frame_%timecode.jpg');
$output = $process->getOutput();

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->extractFrames(new Timecode(40), new Timecode(50), '1/60')
				->save('./output/big_buck_bunny_frame_%timecode.jpg');
$output = $process->getOutput();

namespace PHPVideoToolkit;

$video = new \PHPVideoToolkit\Video($example_video_path);

$process = $video->extractSegment(new \PHPVideoToolkit\Timecode(10), new \PHPVideoToolkit\Timecode(20))
				->extractFrames(null, null, 1)
				->save('./output/%timecode.jpg', null, \PHPVideoToolkit\Media::OVERWRITE_EXISTING);

$output = $process->getOutput();

$audio = new Audio('Ballad_of_the_Sneak.mp3');

$process = $audio->getProcess();
$process->addPreInputCommand('-framerate', '1/5');
$process->addPreInputCommand('-pattern_type', 'glob');
$process->addPreInputCommand('-i', 'images/*.jpg');
$process->addCommand('-pix_fmt', 'yuv420p');
$process->addCommand('-shortest', '');

$output_format = new VideoFormat();
$output_format->setVideoFrameRate('1/5');
$output_format->setVideoDimensions(320, 240);

$process = $audio->save('./output/my_homemade_video.mp4', $output_format, Media::OVERWRITE_EXISTING);


namespace PHPVideoToolkit;

$config->convert = '/opt/local/bin/convert';
$config->gif_transcoder = 'gifsicle';

$output_path = './output/big_buck_bunny.gif';

$output_format = Format::getFormatFor($output_path, $config, 'ImageFormat');
$output_format->setVideoFrameRate(5);
		
$video = new Video('media/BigBuckBunny_320x180.mp4', $config);
$process = $video->extractSegment(new Timecode(10), new Timecode(20))
				->save($output_path, $output_format);
				
$output = $process->getOutput();

namespace PHPVideoToolkit;

$config->gif_transcoder = 'convert';

$output_path = './output/big_buck_bunny.gif';

$output_format = Format::getFormatFor($output_path, $config, 'ImageFormat');
$output_format->setVideoFrameRate(5);
		
$video = new Video('media/BigBuckBunny_320x180.mp4', $config);
$process = $video->extractSegment(new Timecode(10), new Timecode(20))
				->save($output_path, $output_format);
				
$output = $process->getOutput();

namespace PHPVideoToolkit;

$config->gif_transcoder = 'php';

$output_path = './output/big_buck_bunny.gif';

$output_format = Format::getFormatFor($output_path, $config, 'ImageFormat');
$output_format->setVideoFrameRate(5);
		
$video = new Video('media/BigBuckBunny_320x180.mp4', $config);
$process = $video->extractSegment(new Timecode(10), new Timecode(20))
				->save($output_path, $output_format);
				
$output = $process->getOutput();

namespace PHPVideoToolkit;

$config->convert = null; // This disables the imagemagick convert path so gifsicle transcoder falls back to GD
$config->gif_transcoder = 'gifsicle';

$output_path = './output/big_buck_bunny.gif';

$output_format = Format::getFormatFor($output_path, $config, 'ImageFormat');
$output_format->setVideoFrameRate(5);
		
$video = new Video('media/BigBuckBunny_320x180.mp4', $config);
$process = $video->extractSegment(new Timecode(10), new Timecode(20))
				->save($output_path, $output_format);
				
$output = $process->getOutput();


namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');

$output_format = new VideoFormat();
$output_format->setVideoDimensions(160, 120);

$video->save('BigBuckBunny_160x120.3gp', $output_format);


namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->extractAudio()->save('./output/big_buck_bunny.mp3');
// $process = $video->extractVideo()->save('./output/big_buck_bunny.mp4');
				
$output = $process->getOutput();

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->extractSegment(new Timecode('00:02:22.0', Timecode::INPUT_FORMAT_TIMECODE), new Timecode(180))
				->save('./output/big_buck_bunny.mp4');
$output = $process->getOutput();

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->split(45)
				->save('./output/big_buck_bunny_%timecode.mp4');
$output = $process->getOutput();

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->purgeMetaData()
				->setMetaData('title', 'Hello World')
				->save('./output/big_buck_bunny.mp4');
$output = $process->getOutput();

namespace PHPVideoToolkit;

$output_path = './output/big_buck_bunny.mpeg';

$output_format = new VideoFormat();
$output_format->setAudioCodec('acc')
			  ->setVideoCodec('ogg');

$video = new Video('media/BigBuckBunny_320x180.mp4');
$process = $video->save($output_path, $output_format);
$output = $process->getOutput();

namespace PHPVideoToolkit;

$output_path = './output/big_buck_bunny.mp3';

$output_format = new AudioFormat();
$output_format->setAudioCodec('acc');

$video = new Video('media/BigBuckBunny_320x180.mp4');
$process = $video->save($output_path, $output_format);

$output = $process->getOutput();

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->saveNonBlocking('./output/big_buck_bunny.mov');

// do something else important, db queries etc

while($process->isCompleted() === false)
{
	// do something more stuff in a loop.
	// doesn't have to be a loop, just an example.
	
	sleep(0.5);
}

if($process->hasError() === true)
{
	// an error was encountered, do something with it.
}
else
{
	// encoding has completed and no error was detected so 
	// we can get the output from the process.
	$output = $process->getOutput();
}


namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');

$progress_handler = new ProgressHandlerNative(function($data)
{
	echo '<pre>'.print_r($data, true).'</pre>';
});

$process = $video->purgeMetaData()
				->setMetaData('title', 'Hello World')
				->save('./output/big_buck_bunny.mp4', null, Video::OVERWRITE_EXISTING, $progress_handler);
$output = $process->getOutput();

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');

$progress_handler = new ProgressHandlerNative(null);

$process = $video->purgeMetaData()
				->setMetaData('title', 'Hello World')
				->saveNonBlocking('./output/big_buck_bunny.mp4', null, Video::OVERWRITE_EXISTING, $progress_handler);
				
while($progress_handler->completed !== true)
{
	// note setting true in probe() automatically tells the probe to wait after the data is returned.
	echo '<pre>'.print_r($progress_handler->probe(true), true).'</pre>';
}
				
$output = $process->getOutput();



namespace PHPVideoToolkit;

session_start();

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->saveNonBlocking('./output/big_buck_bunny.mp4', null, Video::OVERWRITE_EXISTING);
				
$_SESSION['phpvideotoolkit_portable_process_id'] = $video->getPortableId();



namespace PHPVideoToolkit;

session_start();

$handler = new ProgressHandlerPortable($_SESSION['phpvideotoolkit_portable_process_id']);

$probe = $handler->probe();

echo json_encode(array(
	'finished' => $probe['finished'], // true when the process has ended by interruption, error or success
	'completed' => $probe['completed'], // true when the process has ended with a successful encoding that encountered no errors.
	'percentage' => $probe['percentage']
));
exit;


namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');

$multi_output = new MultiOutput();

$ogg_output = './output/big_buck_bunny.multi1.ogg';
$format = Format::getFormatFor($ogg_output, $config, 'VideoFormat');
$format->setVideoDimensions(VideoFormat::DIMENSION_SQCIF);
$multi_output->addOutput($ogg_output, $format);

$threegp_output = './output/big_buck_bunny.multi2.3gp';
$format = Format::getFormatFor($threegp_output, $config, 'VideoFormat');
$format->setVideoDimensions(VideoFormat::DIMENSION_XGA);
$multi_output->addOutput($threegp_output, $format);

$output = $video->save($multi_output, null, Media::OVERWRITE_EXISTING);


namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->save('./output/big_buck_bunny.mov');

echo 'Expected Executed Command<br />';
echo '<pre>'.$process->getExecutedCommand().'</pre>';

echo 'Expected Command Line Buffer<br />';
echo '<pre>'.$process->getBuffer().'</pre>';

echo 'Actual Executed Command<br />';
echo '<pre>'.$process->getExecutedCommand(true).'</pre>';

echo 'Actual Command Line Buffer<br />';
echo '<pre>'.$process->getRawBuffer().'</pre>';

namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');
$process = $video->getProcess();


// ... continued from above

$process->addPreInputCommand('-custom-command');
$process->addCommand('-custom-command-with-arg', 'arg value');
$process->addPostOutputCommand('-output-command', 'another value');

// ... now save the output video

$video->save('./your/output/file.mp4');


namespace PHPVideoToolkit;

$video  = new Video('BigBuckBunny_320x180.mp4');

$process = $video->getProcess();
$process->setProcessTimelimit(10); // in seconds

try
{
	$video->save('output.mp4');
}
catch(FfmpegProcessOutputException $e)
{
	echo $e->getMessage(); // Imposed time limit (10 seconds) exceeded.
}
				
\PHPVideoToolkit\Config->force_enable_qtfaststart = true;