2017 © Pedro Peláez
 

library php-ffmpeg

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg. Now supports Qt-faststart

image

iakumai/php-ffmpeg

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg. Now supports Qt-faststart

  • Tuesday, September 17, 2013
  • by IAkumaI
  • Repository
  • 2 Watchers
  • 5 Stars
  • 110 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 537 Forks
  • 0 Open issues
  • 20 Versions
  • 8 % Grown

The README.md

PHP FFmpeg (Fork)

An Object Oriented library to convert video/audio files with FFmpeg / AVConv., (*1)

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

Installation

The recommended way to install PHP-FFMpeg is through Composer., (*3)

{
    "require": {
        "iakumai/php-ffmpeg": "0.5.*@dev"
    }
}

Basic Usage

$ffmpeg = FFMpeg\FFMpeg::create();
$video = $ffmpeg->open('video.mpg');
$video
    ->filters()
    ->resize(new FFMpeg\Coordinate\Dimension(320, 240))
    ->synchronize();
$video
    ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))
    ->save('frame.jpg');
$video
    ->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')
    ->save(new FFMpeg\Format\Video\WMV(), 'export-wmv.wmv')
    ->save(new FFMpeg\Format\Video\WebM(), 'export-webm.webm');

Documentation

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

Qt-Faststart

FFMpeg\QtFaststart can make MP4 files progressive. This helps the video to playback as early as possible., (*5)

$qtfaststart = FFMpeg\QtFaststart::create();
$qtfaststart->process('path/to/file.mp4');

If you want to give binary paths explicitely, you can pass an array as configuration., (*6)

$qtfaststart = FFMpeg\QtFaststart::create(array(
    'qtfaststart.binaries' => '/opt/local/ffmpeg/bin/qt-faststart'
));

FFMpeg

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

$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., (*8)

$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., (*9)

$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., (*10)

Video

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

Transcoding

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

$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., (*13)

You can use an extra parameters in save() method., (*14)

$format = new Format\Video\X264();
$video->save($format, 'video.avi', array('-qdiff', '4'));
Extracting image

You can extract a frame at any timecode using the FFMpeg\Media\Video::frame method., (*15)

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., (*16)

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

You can apply filters on FFMpeg\Media\Video with the FFMpeg\Media\Video::addFilter method. Video accepts Audio and Video filters., (*17)

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

Filters are chainable, (*19)

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

Resizes a video to a given size., (*20)

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

The resize filter takes three parameters :, (*21)

  • $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.
Framerate

Changes the frame rate of the video., (*22)

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

The framerate filter takes two parameters :, (*23)

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

Synchronizes audio and video., (*24)

Some containers may use a delay that results in desynchronized outputs. This filters solves this issue., (*25)

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

Audio

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

Transcoding

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

$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., (*28)

Filters

You can apply filters on FFMpeg\Media\Audio with the FFMpeg\Media\Audio::addFilter method. It only accepts audio filters., (*29)

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

Resample

Resamples an audio file., (*31)

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

The resample filter takes two parameters :, (*32)

  • $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., (*33)

You can save frames using the FFMpeg\Media\Frame::save method., (*34)

$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., (*35)

Formats

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

Format can also extends FFMpeg\Format\ProgressableInterface to get realtime informations about the transcoding., (*37)

Predefined formats already provide progress informations as events., (*38)

$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., (*39)

Create your own format

The easiest way to create a format is to extend the abstract FFMpeg\Format\Video\DefaultVideo and FFMpeg\Format\Audio\DefaultAudio. and implement the following methods., (*40)

class CustomWMVFormat extends FFMpeg\Format\Video\DefaultVideo
{
    public function __construct($audioCodec = 'wmav2', $videoCodec = 'wmv2')
    {
        $this
            ->setAudioCodec($audioCodec)
            ->setVideoCodec($videoCodec);
    }

    public function supportBFrames()
    {
        return false;
    }

    public function getAvailableAudioCodecs()
    {
        return array('wmav2');
    }

    public function getAvailableVideoCodecs()
    {
        return array('wmv2');
    }
}

Coordinates

FFMpeg use many units for time and space coordinates., (*41)

  • FFMpeg\Coordinate\AspectRatio represents an aspect ratio.
  • FFMpeg\Coordinate\Dimension represent a dimension.
  • FFMpeg\Coordinate\FrameRate represent a framerate.
  • FFMpeg\Coordinate\Point represent a point.
  • FFMpeg\Coordinate\TimeCode represent a timecode.

FFProbe

FFMpeg\FFProbe is used internally by FFMpeg\FFMpeg to probe medias. You can also use it to extract media metadata., (*42)

$ffprobe = FFMpeg\FFProbe::create();
$ffprobe
    ->streams('/path/to/video/mp4') // extracts streams informations
    ->videos()                      // filters video streams
    ->first()                       // returns the first video stream
    ->get('duration');              // returns the duration property

Using with Silex Microframework

Service provider is easy to set up :, (*43)

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

$video = $app['ffmpeg']->open('video.mpeg');

Available options are as follow :, (*44)

$app->register(new FFMpeg\FFMpegServiceProvider(), array(
    'ffmpeg.configuration' => array(
        'ffmpeg.threads'   => 4,
        'ffmpeg.timeout'   => 300,
        'ffmpeg.binaries'  => '/opt/local/ffmpeg/bin/ffmpeg',
        'ffprobe.timeout'  => 30,
        'ffprobe.binaries' => '/opt/local/ffmpeg/bin/ffprobe',
    ),
    'ffmpeg.logger' => $logger,
));

API Browser

Browse the API, (*45)

License

This project is licensed under the MIT license., (*46)

The Versions

17/09 2013

dev-master

9999999-dev

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg. Now supports Qt-faststart

  Sources   Download

MIT

The Requires

 

The Development Requires

by Valery Ozarnichuk

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

17/09 2013

0.5.3

0.5.3.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg. Now supports Qt-faststart

  Sources   Download

MIT

The Requires

 

The Development Requires

by Valery Ozarnichuk

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

13/09 2013

0.5.x-dev

0.5.9999999.9999999-dev

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg. Now supports Qt-faststart

  Sources   Download

MIT

The Requires

 

The Development Requires

by Valery Ozarnichuk

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

13/09 2013

0.5.2

0.5.2.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg. Now supports Qt-faststart

  Sources   Download

MIT

The Requires

 

The Development Requires

by Valery Ozarnichuk

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

13/09 2013

0.5.1

0.5.1.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg. Now supports Qt-faststart

  Sources   Download

MIT

The Requires

 

The Development Requires

by Valery Ozarnichuk

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

12/09 2013

0.5

0.5.0.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg. Now supports Qt-faststart

  Sources   Download

MIT

The Requires

 

The Development Requires

by Valery Ozarnichuk

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

09/09 2013

0.3.x-dev

0.3.9999999.9999999-dev

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

05/09 2013

0.3.4

0.3.4.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

05/09 2013

0.3.3

0.3.3.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

08/08 2013

0.3.2

0.3.2.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

06/08 2013

0.3.1

0.3.1.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

04/07 2013

0.3.0

0.3.0.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

10/05 2013

0.2.4

0.2.4.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

21/04 2013

0.2.3

0.2.3.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

11/02 2013

0.2.2

0.2.2.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

07/02 2013

0.1.x-dev

0.1.9999999.9999999-dev

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

07/02 2013

0.1.1

0.1.1.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

04/02 2013

0.2.1

0.2.1.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

13/12 2012

0.2.0

0.2.0.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe

30/10 2012

0.1.0

0.1.0.0

FFMpeg PHP, an Object Oriented library to communicate with AVconv / ffmpeg

  Sources   Download

MIT

The Requires

 

The Development Requires

audio video ffmpeg video processing ffprobe audio processing avconv avprobe