#PHP FFmpeg

 [![Build Status](https://secure.travis-ci.org/PHP-FFMpeg/PHP-FFMpeg.png?branch=master)](http://travis-ci.org/PHP-FFMpeg/PHP-FFMpeg)

 [![SensioLabsInsight](https://insight.sensiolabs.com/projects/607f3111-e2d7-44e8-8bcc-54dd64521983/big.png)](https://insight.sensiolabs.com/projects/607f3111-e2d7-44e8-8bcc-54dd64521983)

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

 Check another amazing repo : [PHP FFMpeg extras](https://github.com/alchemy-fr/PHP-FFMpeg-Extras), you will find lots of Audio/Video formats there.

 ## Your attention please

 ### How this library works :

 This library requires a working FFMpeg install. You will need both FFMpeg and FFProbe binaries to use it.
Be sure that these binaries can be located with system PATH to get the benefit of the binary detection,
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/. ### 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. ## Installation The recommended way to install PHP-FFMpeg is through [Composer](https://getcomposer.org). ```json
{
"require": {
"php-ffmpeg/php-ffmpeg": "~0.5"
}
}
``` ## Basic Usage ```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');
``` ## 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 `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. ```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 `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. Please note that audio and video bitrate are set on the format. ```php
$format = new Format\Video\X264();
$format->on('progress', function ($video, $format, $percentage) {
echo "$percentage % transcoded";
}); $format
-> setKiloBitrate(1000)
-> setAudioChannels(2)
-> setAudioKiloBitrate(256); $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. ```php
$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. 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)
->framerate($framerate, $gop)
->synchronize();
``` ###### 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 ###### 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. ###### 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,
));
``` ###### Framerate Changes the frame rate of the video. ```php
$video->filters()->framerate($framerate, $gop);
``` The framerate filter takes two parameters : - `$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();
``` ###### 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 #### 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. Please note that audio kilobitrate is set on the audio format. ```php
$ffmpeg = FFMpeg\FFMpeg::create();
$audio = $ffmpeg->open('track.mp3'); $format = new FFMpeg\Format\Audio\Flac();
$format->on('progress', function ($audio, $format, $percentage) {
echo "$percentage % transcoded";
}); $format
-> setAudioChannels(2)
-> setAudioKiloBitrate(256); $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. ```php
$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. ```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. #### 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. ##### 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');
}
}
``` #### Coordinates FFMpeg use many units for time and space coordinates. - `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. ```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
``` ##Using with Silex Microframework Service provider is easy to set up : ```php
$app = new Silex\Application();
$app->register(new FFMpeg\FFMpegServiceProvider()); $video = $app['ffmpeg']->open('video.mpeg');
``` Available options are as follow : ```php
$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](http://readthedocs.org/docs/ffmpeg-php/en/latest/_static/API/) ## License This project is licensed under the [MIT license](http://opensource.org/licenses/MIT).

关于FFMPeg-PHP你必须要知道的的更多相关文章

  1. 程序员必须要知道的Hadoop的一些事实

    程序员必须要知道的Hadoop的一些事实.现如今,Apache Hadoop已经无人不知无人不晓.当年雅虎搜索工程师Doug Cutting开发出这个用以创建分布式计算机环境的开源软...... 1: ...

  2. 【转载】在IT界取得成功应该知道的10件事

     在IT界取得成功应该知道的10件事 2011-08-11 13:31:30 分类: 项目管理 导读:前面大多数文章都是Jack Wallen写的,这是他的新作,看来要成为NB程序员还要不停的自我总结 ...

  3. 理工科应该的知道的C/C++数学计算库(转)

    理工科应该的知道的C/C++数学计算库(转) 作为理工科学生,想必有限元分析.数值计算.三维建模.信号处理.性能分析.仿真分析...这些或多或少与我们常用的软件息息相关,假如有一天你只需要这些大型软件 ...

  4. 你应该知道的10个奇特的 HTML5 单页网站

    网页设计师努力寻找新的方式来展现内容.其中一个大的趋势是单页网站,现在被世界上的一些大的品牌广泛采用,使用它们来为用户提供一个快速,干净和简单的而且​​美丽的网站. 下面是10个令人惊叹的单页 H​​ ...

  5. Git / 程序员需要知道的12个Git高级命令

    众所周知,Git目前已经是分布式版本控制领域的翘楚,围绕着Git形成了完整的生态圈.学习Git,首先当然是学习Git的基本工作流.相比于SVN等传统版本控制系统来说,Git是专为分布式版本控制而生的强 ...

  6. 你应该知道的RPC原理

    你应该知道的RPC原理 在学校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 而一旦踏入公司尤其是大型互 ...

  7. 希望早几年知道的5个Unix命令

    原文: http://spin.atomicobject.com/2013/09/09/5-unix-commands/ 希望早几年知道的5个Unix命令 使用*nix系统已经有一段时间了.但是还是有 ...

  8. 关于Solr搜索标点与符号的中文分词你必须知道的(mmseg源码改造)

    关于Solr搜索标点与符号的中文分词你必须知道的(mmseg源码改造) 摘要:在中文搜索中的标点.符号往往也是有语义的,比如我们要搜索“C++”或是“C#”,我们不希望搜索出来的全是“C”吧?那样对程 ...

  9. 对于JavaScript的函数.NET开发人员应该知道的11件事

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 昨天小感冒今天重感冒,也不能长篇大论.如果你是.NET开发人员,在进入前端开发领域的时候,对 ...

  10. 每个极客都应该知道的Linux技巧

    每个极客都应该知道的Linux技巧 2014/03/07 | 分类: IT技术 | 0 条评论 | 标签: LINUX 分享到:18 本文由 伯乐在线 - 欣仔 翻译自 TuxRadar Linux. ...

随机推荐

  1. AI_深度学习概论

    什么是是神经网络? 假如有6间房屋的数据集,已知房子的面积,单位是平方米或平方英尺,已知房子的价格.如果通过这6间房子的价格和房子的面积,预测房子的价格,首先要建立起一个数据模型 ,x轴为价格,y轴为 ...

  2. 数据库分表之Mybatis+Mysql实践(含部分关键代码)

    2018年01月31日      随着我们系统用户数量的日增,业务数据处于一个爆发前,增长的数据量已经给我们的系统造成了很大的不确定.在上个周末用户量较多,并发较大的情况下,读写频繁的验证码表,数据量 ...

  3. java事件处理机制

    java中的事件机制的参与者有3种角色:   1.event object:就是事件产生时具体的"事件",用于listener的相应的方法之中,作为参数,一般存在与listerne ...

  4. MyEclipse中好用的快捷键汇总

    MyEclipse中常用的快捷键有很多,合理的使用其中一些快捷键组合,可以有效提高开发的效率和质量. 1.Ctrl + Shift + R:打开资源.可以查找并打开工作区中任何一个文件,且支持使用通配 ...

  5. getRequestDispatcher()和response.sendRedirect()

    request.getRequestDispatcher()是请求转发,前后页面共享一个request   response.sendRedirect()是重新定向,前后页面不是一个request.

  6. android 中string.xml中的%1$s

    idView.setText(getString(R.string.estate_id, mCollectParamObj.getPlotNo())); estate_id:小区号%1$s %d   ...

  7. 使用keepAlive对上下拉刷新列表数据 和 滚动位置细节处理 - vue

    [前言] 使用vue处理项目中遇到列表页面时,之前项目中总会有一些细节问题处理得不太好,这里总结一下,以便优化以后的代码.如下: 1. 使用mint-ui中的LoadMore组件上下拉刷新时,有时无法 ...

  8. ferror,clearerr和EOF含义

    1.我们并不是实时操纵文件,也不是实时生效,它依赖于缓冲区.非缓冲模式编程与常规区别,就是实时与不实时的区别. 2.//fgetc fputc, fgets fputs, fgetwc fputwc, ...

  9. java中的二叉树排序问题

    原创:转载请注明出处 目的:想用java实现二叉树排序算法 思想:利用java中面向对象的思想,即: Tree:类 树根Tree:root //static所属于每一个Tree 左节点Tree:lef ...

  10. c# 可变字符串 StringBuilder

    普通的string不可变,除非用+ 但是+两个字符串后 会生成一个新的string实例.会在内存中创建新的字符串对象.如果重复的对字符串修改,将极大的增加内存开销.c#提供了个可变的字符串序列Stri ...