ffmpeg php 抠像_关于FFMPeg-PHP你必须要知道的

PHP-FFMpeg是一个用于使用FFmpeg库转换视频和音频文件的面向对象的PHP库。它需要FFmpeg和FFProbe的二进制文件,并且可以在安装时通过Composer管理。此库允许您打开本地或远程资源,对视频进行转码、裁剪、旋转等操作,同时提供实时进度反馈。通过这个库,您可以轻松地在PHP中实现视频处理任务,如提取帧、调整大小、改变帧率等。
摘要由CSDN通过智能技术生成

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 using24 [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 ```json32 {33 "require":{34 "php-ffmpeg/php-ffmpeg": "~0.5"

35 }36 }37 ```38

39 ## Basic Usage

40

41 ```php42 $ffmpeg = FFMpeg\FFMpeg::create();43 $video = $ffmpeg->open('video.mpg');44 $video

45 ->filters()46 ->resize(new FFMpeg\Coordinate\Dimension(320, 240))47 ->synchronize();48 $video

49 ->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(10))50 ->save('frame.jpg');51 $video

52 ->save(new FFMpeg\Format\Video\X264(), 'export-x264.mp4')53 ->save(new FFMpeg\Format\Video\WMV(), 'export-wmv.wmv')54 ->save(new FFMpeg\Format\Video\WebM(), 'export-webm.webm');55 ```56

57 ## Documentation

58

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

62 ### FFMpeg63

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

67 ```php68 $ffmpeg = FFMpeg\FFMpeg::create();69 ```70

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

75 ```php76 $ffmpeg = FFMpeg\FFMpeg::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 process80 'ffmpeg.threads'=> 12, // The number of threads that FFMpeg should use81 ), $logger);82 ```83

84 ### Manipulate media85

86 `FFMpeg\FFMpeg` creates media based on URIs. URIs could be either a pointer to a87 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 the90 `-protocols` command :91

92 ```93 ffmpeg -protocols94 ```95

96 To open a resource, use the `FFMpeg\FFMpeg::open` method.97

98 ```php99 $ffmpeg->open('video.mpeg');100 ```101

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

105 #### Video106

107 `FFMpeg\Media\Video` can be transcoded, ie : change codec, isolate audio or108 video. Frames can be extracted.109

110 ##### Transcoding111

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

115 Please note that audio and video bitrate are set on the format.116

117 ```php118 $format = new Format\Video\X264();119 $format->on('progress', function ($video, $format, $percentage) {120 echo "$percentage % transcoded";121 });122

123 $format124 -> 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 documentation132 below for more informations.133

134 ##### Extracting image135

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

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

143 ```php144 $frame = $video->frame(FFMpeg\Coordinate\TimeCode::fromSeconds(42));145 $frame->save('image.jpg');146 ```147

148 ##### Filters149

150 You can apply filters on `FFMpeg\Media\Video` with the `FFMpeg\Media\Video::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 are154 accessible through the `FFMpeg\Media\Video::filters` method.155

156 Filters are chainable157

158 ```php159 $video160 ->filters()161 ->resize($dimension, $mode, $useStandards)162 ->framerate($framerate, $gop)163 ->synchronize();164 ```165

166 ###### Rotate167

168 Rotates a video to a given angle.169

170 ```php171 $video->filters()->rotate($angle);172 ```173

174 The `$angle` parameter must be one of the following constants :175

176 - `FFMpeg\Filters\Video\RotateFilter::ROTATE_90` : 90° clockwise177 - `FFMpeg\Filters\Video\RotateFilter::ROTATE_180` : 180°178 - `FFMpeg\Filters\Video\RotateFilter::ROTATE_270` : 90° counterclockwise179

180 ###### Resize181

182 Resizes a video to a given size.183

184 ```php185 $video->filters()->resize($dimension, $mode, $useStandards);186 ```187

188 The resize filter takes three parameters :189

190 - `$dimension`, an instance of `FFMpeg\Coordinate\Dimension`191 - `$mode`, one of the constants `FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_*` constants192 - `$useStandards`, a boolean to force the use of the nearest aspect ratio standard.193

194 ###### Watermark195

196 Watermark a video with a given image.197

198 ```php199 $video200 ->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 ```php214 $video215 ->filters()216 ->watermark($watermarkPath, array(217 'position' => 'absolute',218 'x'=> 1180,219 'y'=> 620,220 ));221 ```222

223 ###### Framerate224

225 Changes the frame rate of the video.226

227 ```php228 $video->filters()->framerate($framerate, $gop);229 ```230

231 The framerate filter takes two parameters :232

233 - `$framerate`, an instance of `FFMpeg\Coordinate\Framerate`234 - `$gop`, a [GOP](https://wikipedia.org/wiki/Group_of_pictures) value (integer)235

236 ###### Synchronize237

238 Synchronizes audio and video.239

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

243 ```php244 $video->filters()->synchronize();245 ```246

247 ###### Clip248

249 Cuts the video at a desired point.250

251 ```php252 $video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(30), FFMpeg\Coordinate\TimeCode::fromSeconds(15));253 ```254

255 The clip filter takes two parameters:256

257 - `$start`, an instance of `FFMpeg\Coordinate\TimeCode`, specifies the start point of the clip258 - `$duration`, optional, an instance of `FFMpeg\Coordinate\TimeCode`, specifies the duration of the clip259

260 #### Audio261

262 `FFMpeg\Media\Audio` can be transcoded, ie : change codec, isolate audio or263 video. Frames can be extracted.264

265 ##### Transcoding266

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

270 Please note that audio kilobitrate is set on the audio format.271

272 ```php273 $ffmpeg = FFMpeg\FFMpeg::create();274 $audio = $ffmpeg->open('track.mp3');275

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

281 $format282 -> setAudioChannels(2)283 -> setAudioKiloBitrate(256);284

285 $audio->save($format, 'track.flac');286 ```287

288 Transcoding progress can be monitored in realtime, see Format documentation289 below for more informations.290

291 ##### Filters292

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

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

299 ###### Resample300

301 Resamples an audio file.302

303 ```php304 $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 #### Frame312

313 A frame is a image at a timecode of a video ; see documentation above about314 frame extraction.315

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

318 ```php319 $frame->save('target.jpg');320 ```321

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

325 #### Formats326

327 A format implements `FFMpeg\Format\FormatInterface`. To save to a video file,328 use `FFMpeg\Format\VideoInterface`, and `FFMpeg\Format\AudioInterface` for329 audio files.330

331 Format can also extends `FFMpeg\Format\ProgressableInterface` to get realtime332 informations about the transcoding.333

334 Predefined formats already provide progress informations as events.335

336 ```php337 $format = new Format\Video\X264();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 format348

349 The easiest way to create a format is to extend the abstract350 `FFMpeg\Format\Video\DefaultVideo` and `FFMpeg\Format\Audio\DefaultAudio`.351 and implement the following methods.352

353 ```php354 class CustomWMVFormat extends FFMpeg\Format\Video\DefaultVideo355 {356 public function __construct($audioCodec = 'wmav2', $videoCodec = 'wmv2')357 {358 $this359 ->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 #### Coordinates381

382 FFMpeg use many units for time and space coordinates.383

384 - `FFMpeg\Coordinate\AspectRatio` represents an aspect ratio.385 - `FFMpeg\Coordinate\Dimension` represent a dimension.386 - `FFMpeg\Coordinate\FrameRate` represent a framerate.387 - `FFMpeg\Coordinate\Point` represent a point.388 - `FFMpeg\Coordinate\TimeCode` represent a timecode.389

390 ### FFProbe391

392 `FFMpeg\FFProbe` is used internally by `FFMpeg\FFMpeg` to probe medias. You can393 also use it to extract media metadata.394

395 ```php396 $ffprobe = FFMpeg\FFProbe::create();397 $ffprobe398 ->streams('/path/to/video/mp4') // extracts streams informations399 ->videos() // filters video streams400 ->first() // returns the first video stream401 ->get('codec_name'); // returns the codec_name property402 ```403

404 ```php405 $ffprobe = FFMpeg\FFProbe::create();406 $ffprobe407 ->format('/path/to/video/mp4') // extracts file informations408 ->get('duration'); // returns the duration property409 ```410

411 ##Using with Silex Microframework412

413 Service provider is easy to set up :414

415 ```php416 $app = new Silex\Application();417 $app->register(new FFMpeg\FFMpegServiceProvider());418

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

422 Available options are as follow :423

424 ```php425 $app->register(new FFMpeg\FFMpegServiceProvider(), 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 Browser438

439 Browse the [API](http://readthedocs.org/docs/ffmpeg-php/en/latest/_static/API/)440

441 ## License442

443 This project is licensed under the [MIT license](http://opensource.org/licenses/MIT).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值