PHP code example of unifysofttech-pbmedia / laravel-ffmpeg
1. Go to this page and download the library: Download unifysofttech-pbmedia/laravel-ffmpeg 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/ */
unifysofttech-pbmedia / laravel-ffmpeg example snippets
use ProtoneMedia\LaravelFFMpeg\FFMpeg\CopyFormat;
FFMpeg::open('video.mp4')
->export()
->inFormat(new CopyFormat)
->save('video.mkv');
// The 'fromDisk()' method is not sk', as specified in
// the config file.
FFMpeg::open('my_movie.mov')
// export to FTP, converted in WMV
->export()
->toDisk('ftp')
->inFormat(new \FFMpeg\Format\Video\WMV)
->save('my_movie.wmv')
// export to Amazon S3, converted in X264
->export()
->toDisk('s3')
->inFormat(new \FFMpeg\Format\Video\X264)
->save('my_movie.mkv');
// you could even discard the 'toDisk()' method,
// now the converted file will be saved to
// the same disk as the source!
->export()
->inFormat(new FFMpeg\Format\Video\WebM)
->save('my_movie.webm')
// optionally you could set the visibility
// of the exported file
->export()
->inFormat(new FFMpeg\Format\Video\WebM)
->withVisibility('public')
->save('my_movie.webm')
FFMpeg::fromDisk('videos')
->open('steve_howe.mp4')
->getFrameFromSeconds(10)
->export()
->toDisk('thumnails')
->save('FrameAt10sec.png');
// Instead of the 'getFrameFromSeconds()' method, you could
// also use the 'getFrameFromString()' or the
// 'getFrameFromTimecode()' methods:
$media = FFMpeg::open('steve_howe.mp4');
$frame = $media->getFrameFromString('00:00:13.37');
// or
$timecode = new FFMpeg\Coordinate\TimeCode(...);
$frame = $media->getFrameFromTimecode($timecode);
// This code takes 2 input videos, stacks they horizontally in 1 output video and
// adds to this new video the audio from the first video. (It is impossible
// with a simple filter graph that has only 1 input and only 1 output).
FFMpeg::fromDisk('local')
->open(['video.mp4', 'video2.mp4'])
->export()
->addFilter('[0:v][1:v]', 'hstack', '[v]') // $in, $parameters, $out
->addFormatOutputMapping(new X264, Media::make('local', 'stacked_video.mp4'), ['0:a', '[v]'])
->save();
// This gives you an instance of ProtoneMedia\LaravelFFMpeg\MediaOpener
$media = FFMpeg::fromDisk('videos')->open('video.mp4');
// The 'getStreams' method will be called on the underlying Media object since
// it doesn't exists on this object.
$codec = $media->getStreams()->first()->get('codec_name');
// This gives you an instance of ProtoneMedia\LaravelFFMpeg\MediaOpener
$media = FFMpeg::fromDisk('videos')->open('video.mp4');
// This gives you an instance of FFMpeg\Media\MediaTypeInterface
$baseMedia = $media();
use FFMpeg\Format\ProgressListener\AbstractProgressListener;
use ProtoneMedia\LaravelFFMpeg\FFMpeg\ProgressListenerDecorator;
$format = new \FFMpeg\Format\Video\X264;
$decoratedFormat = ProgressListenerDecorator::decorate($format);
FFMpeg::open('video.mp4')
->export()
->inFormat($decoratedFormat)
->onProgress(function () use ($decoratedFormat) {
$listeners = $decoratedFormat->getListeners(); // array of listeners
$listener = $listeners[0]; // instance of AbstractProgressListener
$listener->getCurrentPass();
$listener->getTotalPass();
$listener->getCurrentTime();
})
->save('new_video.mp4');