PHP code example of sujayjaju / ffmpeg-bundle

1. Go to this page and download the library: Download sujayjaju/ffmpeg-bundle 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/ */

    

sujayjaju / ffmpeg-bundle example snippets

 php
            ....
            new sujayjaju\FFmpegBundle\PhpFFmpegBundle(),
            ....
 php
	$ffmpeg = $this->get('php_ffmpeg.ffmpeg');

	// Open video
	$video = $ffmpeg->open('/your/source/folder/input.avi');
	
	// Resize to 640x480
	$video
        ->filters()
        ->resize(new Dimension(640, 480), ResizeFilter::RESIZEMODE_INSET)
        ->synchronize();
        
    // Create a thumbnail
    $video
        ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
        ->save('/PATH/frame.jpg');

	// Start transcoding and save video
	$video->save(new FMpeg\Format\Video\X264(), '/PATH/video.mp4');
 php
namespace Application\YourBundle\EventListener;

use FFMpeg\Exception\InvalidArgumentException;
use FFMpeg\Exception\RuntimeException;
use Oneup\UploaderBundle\Event\PostUploadEvent;

class YourUploadListener
{
    protected $doctrine;
    protected $ffmpeg;


    public function __construct($doctrine, $ffmpeg)
    {
        $this->doctrine = $doctrine;
        $this->ffmpeg = $ffmpeg;
    }

    public function onUpload(PostUploadEvent $event)
    {
        $file_path = $event->getFile()->getPathname();
        $response = $event->getResponse();
        $video = null;
        try{
            $ffmpeg = $this->ffmpeg->open($file_path);
        }catch(InvalidArgumentException $e){
            $response->setSuccess(false);
            $response->setError("Could not load file");
            $response['preventRetry'] = true;
            unlink($file_path);
            return;
        }catch(RuntimeException $e){
            $response->setSuccess(false);
            $response->setError("Invalid File");
            $response['preventRetry'] = true;
            unlink($file_path);
            return;
        }

        // Sample code to check if video was uploaded
        $streams = $ffmpeg->getStreams();
        foreach($streams as $stream){
            if($stream->get('codec_type') == 'video'){
                $video = $stream;
            }
        }

        if(is_null($video)){
            $response->setSuccess(false);
            $response->setError("Invalid Video");
            $response['preventRetry'] = true;
            unlink($file_path);
            return;
        }

        // Do what you want with the video
        // $video
    }
}