ffmpeg-mappable-media/README.md

500 lines
13 KiB
Markdown
Raw Normal View History

2012-04-13 12:12:24 +02:00
#PHP FFmpeg
2014-02-28 00:23:32 +01:00
[![Build Status](https://secure.travis-ci.org/PHP-FFMpeg/PHP-FFMpeg.png?branch=master)](http://travis-ci.org/PHP-FFMpeg/PHP-FFMpeg)
2013-11-13 00:06:17 +01:00
2013-11-13 00:05:50 +01:00
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/607f3111-e2d7-44e8-8bcc-54dd64521983/big.png)](https://insight.sensiolabs.com/projects/607f3111-e2d7-44e8-8bcc-54dd64521983)
2012-04-13 12:12:24 +02:00
2013-07-03 18:14:44 +02:00
An Object Oriented library to convert video/audio files with FFmpeg / AVConv.
2012-04-13 14:16:50 +02:00
2012-06-21 11:42:45 +03:00
Check another amazing repo : [PHP FFMpeg extras](https://github.com/alchemy-fr/PHP-FFMpeg-Extras), you will find lots of Audio/Video formats there.
2013-10-02 10:24:12 +02:00
## Your attention please
2013-12-02 12:47:27 +01:00
### How this library works :
2013-10-02 10:24:12 +02:00
This library requires a working FFMpeg install. You will need both FFMpeg and FFProbe binaries to use it.
2013-11-29 11:38:16 +01:00
Be sure that these binaries can be located with system PATH to get the benefit of the binary detection,
2013-10-02 10:24:12 +02:00
otherwise you should have to explicitely give the binaries path on load.
For Windows users : Please find the binaries at http://ffmpeg.zeranoe.com/builds/.
2013-12-02 12:47:27 +01:00
### Known issues :
- Using rotate and resize will produce a corrupted output when using
[libav](http://libav.org/) 0.8. The bug is fixed in version 9. This bug does not
appear in latest ffmpeg version.
2013-07-03 18:14:44 +02:00
## Installation
2012-05-31 16:07:30 +02:00
2013-07-03 18:14:44 +02:00
The recommended way to install PHP-FFMpeg is through [Composer](https://getcomposer.org).
2012-05-31 16:25:42 +02:00
2013-07-03 18:14:44 +02:00
```json
{
"require": {
2014-08-12 21:05:45 +02:00
"php-ffmpeg/php-ffmpeg": "~0.5"
2013-07-03 18:14:44 +02:00
}
}
```
2012-05-31 16:25:42 +02:00
2013-07-03 18:14:44 +02:00
## Basic Usage
2013-07-03 19:42:24 +02:00
```php
$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');
```
2013-07-03 18:14:44 +02:00
## 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` :
```php
$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.
```php
$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
2013-11-29 11:38:16 +01:00
`FFMpeg\FFMpeg` creates media based on URIs. URIs could be either a pointer to a
local filesystem resource, an HTTP resource or any resource supported by FFmpeg.
**Note** : To list all supported resource type of your FFmpeg build, use the
`-protocols` command :
```
ffmpeg -protocols
```
To open a resource, use the `FFMpeg\FFMpeg::open` method.
2013-07-03 18:14:44 +02:00
```php
$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
2013-07-03 18:57:42 +02:00
`FFMpeg\Media\Video` can be transcoded, ie : change codec, isolate audio or
video. Frames can be extracted.
2013-07-03 18:14:44 +02:00
##### Transcoding
You can transcode videos using the `FFMpeg\Media\Video:save` method. You will
pass a `FFMpeg\Format\FormatInterface` for that.
Please note that audio and video bitrate are set on the format.
2013-07-03 18:14:44 +02:00
```php
$format = new Format\Video\X264();
$format->on('progress', function ($video, $format, $percentage) {
echo "$percentage % transcoded";
});
$format
-> setKiloBitrate(1000)
2014-06-25 09:55:00 +02:00
-> setAudioChannels(2)
-> setAudioKiloBitrate(256);
2013-07-03 18:14:44 +02:00
$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.
2016-11-16 14:11:11 -03:00
This code returns a `FFMpeg\Media\Frame` instance corresponding to the second 42.
2013-07-03 18:14:44 +02:00
You can pass any `FFMpeg\Coordinate\TimeCode` as argument, see dedicated
documentation below for more information.
```php
$frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(42));
2013-07-03 20:16:29 +02:00
$frame->save('image.jpg');
2013-07-03 18:14:44 +02:00
```
2016-11-16 14:11:11 -03:00
##### Generate a waveform
You can generate a waveform of an audio file using the `FFMpeg\Media\Audio::waveform`
method.
This code returns a `FFMpeg\Media\Waveform` instance.
You can optionally pass dimensions as arguments, see dedicated
documentation below for more information.
The ouput file MUST use the PNG extension.
```php
$waveform = $audio->waveform(640, 120);
$waveform->save('waveform.png');
```
If you want to get a waveform from a video, convert it in an audio file first.
```php
// Open your video file
$video = $ffmpeg->open( 'video.mp4' );
// Set an audio format
$audio_format = new FFMpeg\Format\Audio\Mp3();
// Extract the audio into a new file
$video->save('audio.mp3');
// Set the audio file
$audio = $ffmpeg->open( 'audio.mp3' );
// Create the waveform
$waveform = $audio->waveform();
$waveform->save( 'waveform.png' );
```
2013-07-03 18:14:44 +02:00
##### Filters
2013-07-03 18:46:41 +02:00
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
```php
$video
->filters()
->resize($dimension, $mode, $useStandards)
2013-07-03 20:16:29 +02:00
->framerate($framerate, $gop)
2013-07-03 18:46:41 +02:00
->synchronize();
2013-07-03 19:42:24 +02:00
```
2013-07-03 18:46:41 +02:00
2013-11-29 11:38:16 +01:00
###### Rotate
Rotates a video to a given angle.
```php
$video->filters()->rotate($angle);
```
The `$angle` parameter must be one of the following constants :
- `FFMpeg\Filters\Video\RotateFilter::ROTATE_90` : 90° clockwise
- `FFMpeg\Filters\Video\RotateFilter::ROTATE_180` : 180°
- `FFMpeg\Filters\Video\RotateFilter::ROTATE_270` : 90° counterclockwise
2013-07-03 18:46:41 +02:00
###### Resize
Resizes a video to a given size.
```php
$video->filters()->resize($dimension, $mode, $useStandards);
```
2016-12-21 10:30:52 -03:00
The resize filter takes three parameters:
2013-07-03 18:46:41 +02:00
- `$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.
2016-12-21 10:30:52 -03:00
If you want a video in a non-standard ratio, you can use the padding filter to resize your video in the desired size, and wrap it into black bars.
```php
$video->filters()->pad($dimension);
```
The pad filter takes one parameter:
- `$dimension`, an instance of `FFMpeg\Coordinate\Dimension`
Don't forget to save it afterwards.
```php
$video->save(new FFMpeg\Format\Video\X264(), $new_file);
```
###### Watermark
Watermark a video with a given image.
```php
$video
->filters()
->watermark($watermarkPath, array(
'position' => 'relative',
'bottom' => 50,
'right' => 50,
));
```
The watermark filter takes two parameters:
`$watermarkPath`, the path to your watermark file.
`$coordinates`, an array defining how you want your watermark positioned. You can use relative positioning as demonstrated above or absolute as such:
```php
$video
->filters()
->watermark($watermarkPath, array(
'position' => 'absolute',
'x' => 1180,
'y' => 620,
));
```
2013-07-03 20:16:29 +02:00
###### Framerate
2013-07-03 18:46:41 +02:00
2013-07-03 20:32:06 +02:00
Changes the frame rate of the video.
2013-07-03 18:46:41 +02:00
```php
2013-07-03 20:16:29 +02:00
$video->filters()->framerate($framerate, $gop);
2013-07-03 18:46:41 +02:00
```
2013-07-03 20:16:29 +02:00
The framerate filter takes two parameters :
2013-07-03 18:46:41 +02:00
- `$framerate`, an instance of `FFMpeg\Coordinate\Framerate`
- `$gop`, a [GOP](https://wikipedia.org/wiki/Group_of_pictures) value (integer)
###### Synchronize
Synchronizes audio and video.
Some containers may use a delay that results in desynchronized outputs. This
filters solves this issue.
```php
$video->filters()->synchronize();
```
2013-09-27 01:31:29 -03:00
###### Clip
Cuts the video at a desired point.
```php
$video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(30), FFMpeg\Coordinate\TimeCode::fromSeconds(15));
```
The clip filter takes two parameters:
- `$start`, an instance of `FFMpeg\Coordinate\TimeCode`, specifies the start point of the clip
- `$duration`, optional, an instance of `FFMpeg\Coordinate\TimeCode`, specifies the duration of the clip
2013-07-03 18:14:44 +02:00
#### Audio
2013-07-03 18:57:42 +02:00
`FFMpeg\Media\Audio` can be transcoded, ie : change codec, isolate audio or
video. Frames can be extracted.
2013-07-03 18:14:44 +02:00
##### Transcoding
2013-07-03 18:57:42 +02:00
You can transcode audios using the `FFMpeg\Media\Audio:save` method. You will
pass a `FFMpeg\Format\FormatInterface` for that.
Please note that audio kilobitrate is set on the audio format.
2013-07-03 18:57:42 +02:00
```php
2014-12-03 21:59:27 -02:00
$ffmpeg = FFMpeg\FFMpeg::create();
$audio = $ffmpeg->open('track.mp3');
$format = new FFMpeg\Format\Audio\Flac();
$format->on('progress', function ($audio, $format, $percentage) {
2013-07-03 18:57:42 +02:00
echo "$percentage % transcoded";
});
$format
2014-06-25 09:55:00 +02:00
-> setAudioChannels(2)
-> setAudioKiloBitrate(256);
2013-07-03 18:57:42 +02:00
$audio->save($format, 'track.flac');
```
Transcoding progress can be monitored in realtime, see Format documentation
below for more informations.
2013-07-03 18:14:44 +02:00
##### Filters
2013-07-03 18:57:42 +02:00
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.
```php
$audio->filters()->resample($rate);
```
The resample filter takes two parameters :
- `$rate`, a valid audio sample rate value (integer)
2013-07-03 18:14:44 +02:00
#### Frame
2013-07-03 18:57:42 +02:00
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.
```php
$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.
2013-07-03 18:14:44 +02:00
#### 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.
```php
$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.
2013-07-03 20:16:29 +02:00
##### 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.
```php
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');
}
}
```
2013-07-03 18:46:41 +02:00
#### Coordinates
2013-07-03 18:14:44 +02:00
FFMpeg use many units for time and space coordinates.
2013-07-03 19:42:24 +02:00
- `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.
2013-07-03 18:14:44 +02:00
2013-08-05 14:40:53 +02:00
### FFProbe
`FFMpeg\FFProbe` is used internally by `FFMpeg\FFMpeg` to probe medias. You can
also use it to extract media metadata.
```php
$ffprobe = FFMpeg\FFProbe::create();
$ffprobe
->streams('/path/to/video/mp4') // extracts streams informations
->videos() // filters video streams
->first() // returns the first video stream
->get('codec_name'); // returns the codec_name property
```
```php
$ffprobe = FFMpeg\FFProbe::create();
$ffprobe
->format('/path/to/video/mp4') // extracts file informations
->get('duration'); // returns the duration property
2013-08-05 14:40:53 +02:00
```
2013-07-03 18:14:44 +02:00
2013-07-03 19:42:24 +02:00
##Using with Silex Microframework
2013-07-03 19:42:24 +02:00
Service provider is easy to set up :
```php
2013-07-03 19:42:24 +02:00
$app = new Silex\Application();
$app->register(new FFMpeg\FFMpegServiceProvider());
2013-07-03 19:42:24 +02:00
$video = $app['ffmpeg']->open('video.mpeg');
```
2013-07-03 19:42:24 +02:00
Available options are as follow :
2012-09-03 14:55:04 +02:00
```php
2013-07-03 19:42:24 +02:00
$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,
));
2012-09-03 14:55:04 +02:00
```
2013-07-03 18:14:44 +02:00
## API Browser
Browse the [API](http://readthedocs.org/docs/ffmpeg-php/en/latest/_static/API/)
## License
2012-05-31 16:07:30 +02:00
This project is licensed under the [MIT license](http://opensource.org/licenses/MIT).