关键词:
1 #PHP FFmpeg 2 3 [![Build Status](https://secure.travis-ci.org/PHP-FFMpeg/PHP-FFMpeg.png?branch=master)](http://travis-ci.org/PHP-FFMpeg/PHP-FFMpeg) 4 5 [![SensioLabsInsight](https://insight.sensiolabs.com/projects/607f3111-e2d7-44e8-8bcc-54dd64521983/big.png)](https://insight.sensiolabs.com/projects/607f3111-e2d7-44e8-8bcc-54dd64521983) 6 7 An Object Oriented library to convert video/audio files with FFmpeg / AVConv. 8 9 Check another amazing repo : [PHP FFMpeg extras](https://github.com/alchemy-fr/PHP-FFMpeg-Extras), you will find lots of Audio/Video formats there. 10 11 ## Your attention please 12 13 ### How this library works : 14 15 This library requires a working FFMpeg install. You will need both FFMpeg and FFProbe binaries to use it. 16 Be sure that these binaries can be located with system PATH to get the benefit of the binary detection, 17 otherwise you should have to explicitely give the binaries path on load. 18 19 For Windows users : Please find the binaries at http://ffmpeg.zeranoe.com/builds/. 20 21 ### Known issues : 22 23 - Using rotate and resize will produce a corrupted output when using 24 [libav](http://libav.org/) 0.8. The bug is fixed in version 9. This bug does not 25 appear in latest ffmpeg version. 26 27 ## Installation 28 29 The recommended way to install PHP-FFMpeg is through [Composer](https://getcomposer.org). 30 31 ```json 32 { 33 "require": { 34 "php-ffmpeg/php-ffmpeg": "~0.5" 35 } 36 } 37 ``` 38 39 ## Basic Usage 40 41 ```php 42 $ffmpeg = FFMpegFFMpeg::create(); 43 $video = $ffmpeg->open(‘video.mpg‘); 44 $video 45 ->filters() 46 ->resize(new FFMpegCoordinateDimension(320, 240)) 47 ->synchronize(); 48 $video 49 ->frame(FFMpegCoordinateTimeCode::fromSeconds(10)) 50 ->save(‘frame.jpg‘); 51 $video 52 ->save(new FFMpegFormatVideoX264(), ‘export-x264.mp4‘) 53 ->save(new FFMpegFormatVideoWMV(), ‘export-wmv.wmv‘) 54 ->save(new FFMpegFormatVideoWebM(), ‘export-webm.webm‘); 55 ``` 56 57 ## Documentation 58 59 This documentation is an introduction to discover the API. It‘s recommended 60 to browse the source code as it is self-documented. 61 62 ### FFMpeg 63 64 `FFMpegFFMpeg` is the main object to use to manipulate medias. To build it, 65 use the static `FFMpegFFMpeg::create` : 66 67 ```php 68 $ffmpeg = FFMpegFFMpeg::create(); 69 ``` 70 71 FFMpeg will autodetect ffmpeg and ffprobe binaries. If you want to give binary 72 paths explicitely, you can pass an array as configuration. A `PsrLoggerLoggerInterface` 73 can also be passed to log binary executions. 74 75 ```php 76 $ffmpeg = FFMpegFFMpeg::create(array( 77 ‘ffmpeg.binaries‘ => ‘/opt/local/ffmpeg/bin/ffmpeg‘, 78 ‘ffprobe.binaries‘ => ‘/opt/local/ffmpeg/bin/ffprobe‘, 79 ‘timeout‘ => 3600, // The timeout for the underlying process 80 ‘ffmpeg.threads‘ => 12, // The number of threads that FFMpeg should use 81 ), $logger); 82 ``` 83 84 ### Manipulate media 85 86 `FFMpegFFMpeg` creates media based on URIs. URIs could be either a pointer to a 87 local filesystem resource, an HTTP resource or any resource supported by FFmpeg. 88 89 **Note** : To list all supported resource type of your FFmpeg build, use the 90 `-protocols` command : 91 92 ``` 93 ffmpeg -protocols 94 ``` 95 96 To open a resource, use the `FFMpegFFMpeg::open` method. 97 98 ```php 99 $ffmpeg->open(‘video.mpeg‘); 100 ``` 101 102 Two types of media can be resolved : `FFMpegMediaAudio` and `FFMpegMediaVideo`. 103 A third type, `FFMpegMediaFrame`, is available through videos. 104 105 #### Video 106 107 `FFMpegMediaVideo` can be transcoded, ie : change codec, isolate audio or 108 video. Frames can be extracted. 109 110 ##### Transcoding 111 112 You can transcode videos using the `FFMpegMediaVideo:save` method. You will 113 pass a `FFMpegFormatFormatInterface` for that. 114 115 Please note that audio and video bitrate are set on the format. 116 117 ```php 118 $format = new FormatVideoX264(); 119 $format->on(‘progress‘, function ($video, $format, $percentage) { 120 echo "$percentage % transcoded"; 121 }); 122 123 $format 124 -> setKiloBitrate(1000) 125 -> setAudioChannels(2) 126 -> setAudioKiloBitrate(256); 127 128 $video->save($format, ‘video.avi‘); 129 ``` 130 131 Transcoding progress can be monitored in realtime, see Format documentation 132 below for more informations. 133 134 ##### Extracting image 135 136 You can extract a frame at any timecode using the `FFMpegMediaVideo::frame` 137 method. 138 139 This code return a `FFMpegMediaFrame` instance corresponding to the second 42. 140 You can pass any `FFMpegCoordinateTimeCode` as argument, see dedicated 141 documentation below for more information. 142 143 ```php 144 $frame = $video->frame(FFMpegCoordinateTimeCode::fromSeconds(42)); 145 $frame->save(‘image.jpg‘); 146 ``` 147 148 ##### Filters 149 150 You can apply filters on `FFMpegMediaVideo` with the `FFMpegMediaVideo::addFilter` 151 method. Video accepts Audio and Video filters. 152 153 You can build your own filters and some are bundled in PHP-FFMpeg - they are 154 accessible through the `FFMpegMediaVideo::filters` method. 155 156 Filters are chainable 157 158 ```php 159 $video 160 ->filters() 161 ->resize($dimension, $mode, $useStandards) 162 ->framerate($framerate, $gop) 163 ->synchronize(); 164 ``` 165 166 ###### Rotate 167 168 Rotates a video to a given angle. 169 170 ```php 171 $video->filters()->rotate($angle); 172 ``` 173 174 The `$angle` parameter must be one of the following constants : 175 176 - `FFMpegFiltersVideoRotateFilter::ROTATE_90` : 90° clockwise 177 - `FFMpegFiltersVideoRotateFilter::ROTATE_180` : 180° 178 - `FFMpegFiltersVideoRotateFilter::ROTATE_270` : 90° counterclockwise 179 180 ###### Resize 181 182 Resizes a video to a given size. 183 184 ```php 185 $video->filters()->resize($dimension, $mode, $useStandards); 186 ``` 187 188 The resize filter takes three parameters : 189 190 - `$dimension`, an instance of `FFMpegCoordinateDimension` 191 - `$mode`, one of the constants `FFMpegFiltersVideoResizeFilter::RESIZEMODE_*` constants 192 - `$useStandards`, a boolean to force the use of the nearest aspect ratio standard. 193 194 ###### Watermark 195 196 Watermark a video with a given image. 197 198 ```php 199 $video 200 ->filters() 201 ->watermark($watermarkPath, array( 202 ‘position‘ => ‘relative‘, 203 ‘bottom‘ => 50, 204 ‘right‘ => 50, 205 )); 206 ``` 207 208 The watermark filter takes two parameters: 209 210 `$watermarkPath`, the path to your watermark file. 211 `$coordinates`, an array defining how you want your watermark positioned. You can use relative positioning as demonstrated above or absolute as such: 212 213 ```php 214 $video 215 ->filters() 216 ->watermark($watermarkPath, array( 217 ‘position‘ => ‘absolute‘, 218 ‘x‘ => 1180, 219 ‘y‘ => 620, 220 )); 221 ``` 222 223 ###### Framerate 224 225 Changes the frame rate of the video. 226 227 ```php 228 $video->filters()->framerate($framerate, $gop); 229 ``` 230 231 The framerate filter takes two parameters : 232 233 - `$framerate`, an instance of `FFMpegCoordinateFramerate` 234 - `$gop`, a [GOP](https://wikipedia.org/wiki/Group_of_pictures) value (integer) 235 236 ###### Synchronize 237 238 Synchronizes audio and video. 239 240 Some containers may use a delay that results in desynchronized outputs. This 241 filters solves this issue. 242 243 ```php 244 $video->filters()->synchronize(); 245 ``` 246 247 ###### Clip 248 249 Cuts the video at a desired point. 250 251 ```php 252 $video->filters()->clip(FFMpegCoordinateTimeCode::fromSeconds(30), FFMpegCoordinateTimeCode::fromSeconds(15)); 253 ``` 254 255 The clip filter takes two parameters: 256 257 - `$start`, an instance of `FFMpegCoordinateTimeCode`, specifies the start point of the clip 258 - `$duration`, optional, an instance of `FFMpegCoordinateTimeCode`, specifies the duration of the clip 259 260 #### Audio 261 262 `FFMpegMediaAudio` can be transcoded, ie : change codec, isolate audio or 263 video. Frames can be extracted. 264 265 ##### Transcoding 266 267 You can transcode audios using the `FFMpegMediaAudio:save` method. You will 268 pass a `FFMpegFormatFormatInterface` for that. 269 270 Please note that audio kilobitrate is set on the audio format. 271 272 ```php 273 $ffmpeg = FFMpegFFMpeg::create(); 274 $audio = $ffmpeg->open(‘track.mp3‘); 275 276 $format = new FFMpegFormatAudioFlac(); 277 $format->on(‘progress‘, function ($audio, $format, $percentage) { 278 echo "$percentage % transcoded"; 279 }); 280 281 $format 282 -> setAudioChannels(2) 283 -> setAudioKiloBitrate(256); 284 285 $audio->save($format, ‘track.flac‘); 286 ``` 287 288 Transcoding progress can be monitored in realtime, see Format documentation 289 below for more informations. 290 291 ##### Filters 292 293 You can apply filters on `FFMpegMediaAudio` with the `FFMpegMediaAudio::addFilter` 294 method. It only accepts audio filters. 295 296 You can build your own filters and some are bundled in PHP-FFMpeg - they are 297 accessible through the `FFMpegMediaAudio::filters` method. 298 299 ###### Resample 300 301 Resamples an audio file. 302 303 ```php 304 $audio->filters()->resample($rate); 305 ``` 306 307 The resample filter takes two parameters : 308 309 - `$rate`, a valid audio sample rate value (integer) 310 311 #### Frame 312 313 A frame is a image at a timecode of a video ; see documentation above about 314 frame extraction. 315 316 You can save frames using the `FFMpegMediaFrame::save` method. 317 318 ```php 319 $frame->save(‘target.jpg‘); 320 ``` 321 322 This method has a second optional boolean parameter. Set it to true to get 323 accurate images ; it takes more time to execute. 324 325 #### Formats 326 327 A format implements `FFMpegFormatFormatInterface`. To save to a video file, 328 use `FFMpegFormatVideoInterface`, and `FFMpegFormatAudioInterface` for 329 audio files. 330 331 Format can also extends `FFMpegFormatProgressableInterface` to get realtime 332 informations about the transcoding. 333 334 Predefined formats already provide progress informations as events. 335 336 ```php 337 $format = new FormatVideoX264(); 338 $format->on(‘progress‘, function ($video, $format, $percentage) { 339 echo "$percentage % transcoded"; 340 }); 341 342 $video->save($format, ‘video.avi‘); 343 ``` 344 345 The callback provided for the event can be any callable. 346 347 ##### Create your own format 348 349 The easiest way to create a format is to extend the abstract 350 `FFMpegFormatVideoDefaultVideo` and `FFMpegFormatAudioDefaultAudio`. 351 and implement the following methods. 352 353 ```php 354 class CustomWMVFormat extends FFMpegFormatVideoDefaultVideo 355 { 356 public function __construct($audioCodec = ‘wmav2‘, $videoCodec = ‘wmv2‘) 357 { 358 $this 359 ->setAudioCodec($audioCodec) 360 ->setVideoCodec($videoCodec); 361 } 362 363 public function supportBFrames() 364 { 365 return false; 366 } 367 368 public function getAvailableAudioCodecs() 369 { 370 return array(‘wmav2‘); 371 } 372 373 public function getAvailableVideoCodecs() 374 { 375 return array(‘wmv2‘); 376 } 377 } 378 ``` 379 380 #### Coordinates 381 382 FFMpeg use many units for time and space coordinates. 383 384 - `FFMpegCoordinateAspectRatio` represents an aspect ratio. 385 - `FFMpegCoordinateDimension` represent a dimension. 386 - `FFMpegCoordinateFrameRate` represent a framerate. 387 - `FFMpegCoordinatePoint` represent a point. 388 - `FFMpegCoordinateTimeCode` represent a timecode. 389 390 ### FFProbe 391 392 `FFMpegFFProbe` is used internally by `FFMpegFFMpeg` to probe medias. You can 393 also use it to extract media metadata. 394 395 ```php 396 $ffprobe = FFMpegFFProbe::create(); 397 $ffprobe 398 ->streams(‘/path/to/video/mp4‘) // extracts streams informations 399 ->videos() // filters video streams 400 ->first() // returns the first video stream 401 ->get(‘codec_name‘); // returns the codec_name property 402 ``` 403 404 ```php 405 $ffprobe = FFMpegFFProbe::create(); 406 $ffprobe 407 ->format(‘/path/to/video/mp4‘) // extracts file informations 408 ->get(‘duration‘); // returns the duration property 409 ``` 410 411 ##Using with Silex Microframework 412 413 Service provider is easy to set up : 414 415 ```php 416 $app = new SilexApplication(); 417 $app->register(new FFMpegFFMpegServiceProvider()); 418 419 $video = $app[‘ffmpeg‘]->open(‘video.mpeg‘); 420 ``` 421 422 Available options are as follow : 423 424 ```php 425 $app->register(new FFMpegFFMpegServiceProvider(), array( 426 ‘ffmpeg.configuration‘ => array( 427 ‘ffmpeg.threads‘ => 4, 428 ‘ffmpeg.timeout‘ => 300, 429 ‘ffmpeg.binaries‘ => ‘/opt/local/ffmpeg/bin/ffmpeg‘, 430 ‘ffprobe.timeout‘ => 30, 431 ‘ffprobe.binaries‘ => ‘/opt/local/ffmpeg/bin/ffprobe‘, 432 ), 433 ‘ffmpeg.logger‘ => $logger, 434 )); 435 ``` 436 437 ## API Browser 438 439 Browse the [API](http://readthedocs.org/docs/ffmpeg-php/en/latest/_static/API/) 440 441 ## License 442 443 This project is licensed under the [MIT license](http://opensource.org/licenses/MIT).
关于http协议,你必须要知道的(代码片段)
引言HTTP协议是HyperTextTransferProtocol(超文本传输协议)的缩写,是用于从万维网服务器传输超文本到本地浏览器的传送协议。HTTP是基于TCP/IP协议通信协议来传递数据(HTML文件,图片文件,查询结果等)。它不涉及数据包(packet)传... 查看详情
关于配置,你必须要知道这一点....(代码片段)
kafka管控平台推荐使用滴滴开源的Kafka运维管控平台(戳我呀)更符合国人的操作习惯、更强大的管控能力、更高效的问题定位能力、更便捷的集群运维能力、更专业的资源治理、更友好的运维生态、BliBli视频:石臻臻的杂货铺kafka的... 查看详情
深入集合类系列——你必须要知道的两棵继承树
你必须要知道的10款app开发框架
对于大部分Web开发人员,HTML、CSS和Javascript是他们最熟练的开发技能。然而,开发一个原生的移动App,对他们来说却是完全陌生的领域。因为开发Android,iOS或WindowsPhone上的原生App(app开发公司ty300.com),需要掌握完全不同的开发... 查看详情
oracle!你必须要知道的knowledgepoints(入门篇)(代码片段)
一、入门oracle有四个用户,分别为sys、system、sysman和scott,其中sys是oracle权限最高的用户,类似于Linux系统的root,scott是实例用户,上课就以这个用户里的三张员工表empno、dept、salgrade作为示例来授课。启动服务1.快捷键ctrl+alt+del打... 查看详情
oracle!你必须要知道的knowledgepoints(下)(代码片段)
子查询什么是子查询当查询中的限制条件需要另一个查询提供时,我们可以把两个查询语句嵌套起来,提供条件的查询语句作为子查询。子查询,也叫内部查询,先于主查询执行,子查询的结果被用于主查询。子查询分为单行子... 查看详情
你必须要知道的雪崩穿透预热更新降级
以前总觉得,写得一手好Java代码,走遍天下都不怕,后来随着时间得推移,才意识到程序的高效性流畅性才是最重要最重要的。所有的技术都是前人不断实践突破革新留下的产物,它们的存在以及运用一定有... 查看详情
想要节省空间,你必须要知道——动态内存管理(附通讯录动态内存版源码)(代码片段)
想要节省空间,你必须要知道——动态内存管理(附通讯录动态内存版源码)1. 为什么存在动态内存分配2. 动态内存函数的介绍2.1 malloc2.2 freemalloc和free通常配合一起使用:2.3 calloc2.4 ... 查看详情
在做自动化测试之前你必须要知道的事
做测试好几年了,真正学习和实践自动化测试一年,自我感觉这一个年中收获许多。我们更普遍的认识把“自动化测试”看做“基于产品或项目UI层的自动化测试”。 UI层的自动化测试,这个大家应该再熟悉不过... 查看详情
作为web开发人员,你必须要知道的问题!(持续更新)
GET和POST的区别 GET请注意,查询字符串(名称/值对)是在GET请求的URL中发送的:/test/demo_form.asp?name1=value1&name2=value2GET请求可被缓存GET请求保留在浏览器历史记录中GET请求可被收藏为书签GET请求不应在处理敏感数据时使用GET... 查看详情
你必须要知道的软件测试3个主流方式
在产品项目的最后推进过程中,会经过一系列的测试来判断以及优化产品,在测试中使产品的属性特征最优化,最终达到吸引更多客户的目的;本文作者分享了三种软件测试的主流方式,我们一起来了解一下... 查看详情
10个你必须要知道的重要javascript数组方法(代码片段)
...忙碌的初学者,我选择了10种最常见的数组方法,你必须学习它们,这些可以帮助你提升学习效率,节省时间。为了便于理解,我为每个数组方法提供了一个示例用例。01、Array.map()通过调用回调函数,map()... 查看详情
10个你必须要知道的重要javascript数组方法(代码片段)
...忙碌的初学者,我选择了10种最常见的数组方法,你必须学习它们,这些可以帮助你提升学习效率,节省时间。为了便于理解,我为每个数组方法提供了一个示例用例。01、Array.map()通过调用回调函数,map()... 查看详情
jmeter--作为测试你必须要知道的基础名词与环境搭建
大家好,我们本章开始学习Jmeter,后续还会有RF以及LoadRunner的介绍,为什么要学习Jmeter,它主要是用来做性能测试的,其中它也需要间接或直接的需要用到抓包工具,至于为什么需要用到抓包工... 查看详情
线程池续:你必须要知道的线程池submit()实现原理之futuretask!(代码片段)
前言上一篇内容写了Java中线程池的实现原理及源码分析,说好的是实实在在的大满足,想通过一篇文章让大家对线程池有个透彻的了解,但是文章写完总觉得还缺点什么?上篇文章只提到线程提交的execute()方法,并没有讲解线程... 查看详情
线程池续:你必须要知道的线程池submit()实现原理之futuretask!(代码片段)
前言上一篇内容写了Java中线程池的实现原理及源码分析,说好的是实实在在的大满足,想通过一篇文章让大家对线程池有个透彻的了解,但是文章写完总觉得还缺点什么?上篇文章只提到线程提交的execute()方法,并没有讲解线程... 查看详情
划重点|ios15正式发布,全新的通知推送系统,你必须要知道
简介: 今年友盟+联合达摩院决策智能实验室讲算法技术,推出国内首个智能推送功能,帮助产品运营人员实现一键式触达的精细化运营。通过精心打磨的在线学习与优化算法,对推送人群与推送文案进行精准... 查看详情
关于flutter列表的性能优化,你必须要了解的(代码片段)
这里是坚果前端小课堂,大家喜欢的话,可以关注我的公众号“坚果前端,”,或者加我好友,获取更多精彩内容嵌套列表-ShrinkWrap与Slivers使用ShrinkWrap的列表列表下面是一些使用ListView对象呈现列表列表的代... 查看详情