Extension to php-ffmpeg to provide greater control over mapped output https://packagist.org/packages/danjones000/ffmpeg-mappable-media
Find a file
2013-07-03 18:57:42 +02:00
docs Fix typo 2013-04-21 18:08:29 +02:00
src/FFMpeg Add notes about resize modes 2013-07-03 18:46:22 +02:00
tests Add ExecutableNotFoundException 2013-07-03 14:13:29 +02:00
.gitignore added support for retrieving progress information via helpers 2012-12-04 17:19:58 +01:00
.travis.yml Re-enable build on PHP 5.5 2013-07-03 18:24:20 +02:00
buildAPI.sh Add API tool and configuration 2012-05-31 12:19:23 +02:00
CHANGELOG.md Bump symfony dependency to 2.1 2013-05-30 16:58:14 +02:00
composer.json Add branch alias 2013-06-25 21:52:37 +02:00
composer.lock Remove unused dependencies 2013-06-25 10:08:47 +02:00
LICENSE Add License 2012-04-13 14:34:53 +02:00
phpunit-functional.xml.dist Version 0.3 2013-06-25 10:03:20 +02:00
phpunit.xml.dist Version 0.3 2013-06-25 10:03:20 +02:00
README.md Add audio documentation 2013-07-03 18:57:42 +02:00
sami_configuration.php Update API name 2012-05-31 12:29:33 +02:00
vendors.win.php Add vendors script for window CI 2012-04-17 20:32:30 +02:00

#PHP FFmpeg

Build Status

An Object Oriented library to convert video/audio files with FFmpeg / AVConv.

Check another amazing repo : PHP FFMpeg extras, you will find lots of Audio/Video formats there.

Installation

The recommended way to install PHP-FFMpeg is through Composer.

{
    "require": {
        "php-ffmpeg/php-ffmpeg": "~0.3.0"
    }
}

Basic Usage

Documentation

This documentation is an introduction to discover the API. It's recommended to browse the source code as it is self-documented.

FFMpeg

FFMpeg\FFMpeg is the main object to use to manipulate medias. To build it, use the static FFMpeg\FFMpeg::create :

$ffmpeg = FFMpeg\FFMpeg::create();

FFMpeg will autodetect ffmpeg and ffprobe binaries. If you want to give binary paths explicitely, you can pass an array as configuration. A Psr\Logger\LoggerInterface can also be passed to log binary executions.

$ffmpeg = FFMpeg\FFMpeg::create(array(
    'ffmpeg.binaries'  => '/opt/local/ffmpeg/bin/ffmpeg',
    'ffprobe.binaries' => '/opt/local/ffmpeg/bin/ffprobe',
    'timeout'          => 3600, // The timeout for the underlying process
    'ffmpeg.threads'   => 12,   // The number of threads that FFMpeg should use
), $logger);

Manipulate media

FFMpeg\FFMpeg creates media based on file paths. To open a file path, use the FFMpeg\FFMpeg::open method.

$ffmpeg->open('video.mpeg');

Two types of media can be resolved : FFMpeg\Media\Audio and FFMpeg\Media\Video. A third type, FFMpeg\Media\Frame, is available through videos.

Video

FFMpeg\Media\Video can be transcoded, ie : change codec, isolate audio or video. Frames can be extracted.

Transcoding

You can transcode videos using the FFMpeg\Media\Video:save method. You will pass a FFMpeg\Format\FormatInterface for that.

$format = new Format\Video\X264();
$format->on('progress', function ($video, $format, $percentage) {
    echo "$percentage % transcoded";
});

$video->save($format, 'video.avi');

Transcoding progress can be monitored in realtime, see Format documentation below for more informations.

Extracting image

You can extract a frame at any timecode using the FFMpeg\Media\Video::frame method.

This code return a FFMpeg\Media\Frame instance corresponding to the second 42. You can pass any FFMpeg\Coordinate\TimeCode as argument, see dedicated documentation below for more information.

$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(42));
$frame->saveAs('image.jpg');
Filters

You can apply filters on FFMpeg\Media\Video with the FFMpeg\Media\Video::addFilter method. Video accepts Audio and Video filters.

You can build your own filters and some are bundled in PHP-FFMpeg - they are accessible through the FFMpeg\Media\Video::filters method.

Filters are chainable

$video
    ->filters()
    ->resize($dimension, $mode, $useStandards)
    ->resample($framerate, $gop)
    ->synchronize();

###### Resize

Resizes a video to a given size.

```php
$video->filters()->resize($dimension, $mode, $useStandards);

The resize filter takes three parameters :

  • $dimension, an instance of FFMpeg\Coordinate\Dimension
  • $mode, one of the constants FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_* constants
  • $useStandards, a boolean to force the use of the nearest aspect ratio standard.
Resample

Resample the video frame rate.

$video->filters()->resample($framerate, $gop);

The resample filter takes two parameters :

  • $framerate, an instance of FFMpeg\Coordinate\Framerate
  • $gop, a GOP value (integer)
Synchronize

Synchronizes audio and video.

Some containers may use a delay that results in desynchronized outputs. This filters solves this issue.

$video->filters()->synchronize();

Audio

FFMpeg\Media\Audio can be transcoded, ie : change codec, isolate audio or video. Frames can be extracted.

Transcoding

You can transcode audios using the FFMpeg\Media\Audio:save method. You will pass a FFMpeg\Format\FormatInterface for that.

$format = new Format\Audio\Flac();
$format->on('progress', function ($$audio, $format, $percentage) {
    echo "$percentage % transcoded";
});

$audio->save($format, 'track.flac');

Transcoding progress can be monitored in realtime, see Format documentation below for more informations.

Filters

You can apply filters on FFMpeg\Media\Audio with the FFMpeg\Media\Audio::addFilter method. It only accepts audio filters.

You can build your own filters and some are bundled in PHP-FFMpeg - they are accessible through the FFMpeg\Media\Audio::filters method.

Resample

Resamples an audio file.

$audio->filters()->resample($rate);

The resample filter takes two parameters :

  • $rate, a valid audio sample rate value (integer)

Frame

A frame is a image at a timecode of a video ; see documentation above about frame extraction.

You can save frames using the FFMpeg\Media\Frame::save method.

$frame->save('target.jpg');

This method has a second optional boolean parameter. Set it to true to get accurate images ; it takes more time to execute.

Formats

A format implements FFMpeg\Format\FormatInterface. To save to a video file, use FFMpeg\Format\VideoInterface, and FFMpeg\Format\AudioInterface for audio files.

Format can also extends FFMpeg\Format\ProgressableInterface to get realtime informations about the transcoding.

Predefined formats already provide progress informations as events.

$format = new Format\Video\X264();
$format->on('progress', function ($video, $format, $percentage) {
    echo "$percentage % transcoded";
});

$video->save($format, 'video.avi');

The callback provided for the event can be any callable.

Coordinates

FFMpeg use many units for time and space coordinates.

FFMpeg\Coordinate\AspectRatio
FFMpeg\Coordinate\Dimension
FFMpeg\Coordinate\FrameRate
FFMpeg\Coordinate\Point
FFMpeg\Coordinate\TimeCode

##Usage Example

$x264 = new X264();
$x264->setDimensions(320, 240);

$ffmpeg->open('Video.mpeg')
    ->encode($new WebM(), 'file.webm')
    ->encode($x264, 'file.mp4')
    ->encode($new Ogg(), 'file.ogv')
    ->close();

##Getting progress information

$progressHelper = new FFMpeg\Helper\AudioProgressHelper(function($percent, $remaining, $rate) {
	echo "Current progress: " . $percent "%\n";
	echo "Remaining time: " . $remaining " seconds\n";
});

$ffmpeg->open('Audio.wav')
	->attachHelper($progressHelper)
    ->encode(new Mp3(), 'file.mp3')
    ->close();

##Using with Silex Microframework

use FFMpeg\SilexServiceProvider;
use Silex\Application;

$app = new Application();
$app->register(new FFMpegServiceProvider());

API Browser

Browse the API

License

This project is licensed under the MIT license.