FFMpeg for PHP


PHP使用FFMpeg來轉換視頻格式。Github上搜索FFMPEG,到https://github.com/PHP-FFMpeg/PHP-FFMpeg。

For Windows users : Please find the binaries at http://ffmpeg.zeranoe.com/builds/.詳細使用過程,見我上一篇博客。

以下操作,默認是在Linux服務器環境下進行的。

安裝

建議通過Composer來安裝PHP-FFMpeg 

$ composer require php-ffmpeg/php-ffmpeg

基本使用

$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');

文檔

 這個文檔是對API進行的引導介紹。建議去閱讀源代碼和配套的文檔。

FFMpeg

FFMpeg\FFMpeg 是操作媒體主要的對象。可以使用靜態調用FFMpeg\FFMpeg::create來創建。

$ffmpeg = FFMpeg\FFMpeg::create();

FFMpeg會自動探測 ffmpeg和ffprobe的二進制文件。如果你想給出確切的二進制文件路徑, 可以通過一個數組來設置。 A Psr\Logger\LoggerInterface can also be passed to log binary executions.

$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);

媒體操作

FFMpeg\FFMpeg根據URIs來創建媒體. URIs可以是本地的系統資源, HTTP網絡資源,FFmpeg支持的任意資源。

注意:如果你想列出你用的FFmpeg支持的所有資源類型, 使用 -protocols 命令:

ffmpeg -protocols 
使用 FFMpeg\FFMpeg::open 方法來打開一個資源。
$ffmpeg->open('video.mpeg');

兩種類型的視頻可以被處理: FFMpeg\Media\Audio FFMpeg\Media\Video。第三種類型 FFMpeg\Media\Frame, 可以通過視頻來處理。

Video

FFMpeg\Media\Video 可以被轉碼, ie: change codec, isolate audio or video. 幀可以提取。

轉碼

你可以通過FFMpeg\Media\Video:save方法對視頻進行轉碼,你將通過 FFMpeg\Format\FormatInterface來實現。

請注意視頻或音頻的比特率需要在格式中設置。

$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');

 轉碼的過程可以被實時監控, 看下面有關Format的文檔了解更過信息。

提取圖片

你可以用FFMpeg\Media\Video::frame 方法來提取任意時間的幀畫面。

代碼返回了一個媒體文件第42秒所對應的FFMpeg\Media\Frame 實例。你可以使用 FFMpeg\Coordinate\TimeCode 作為參數, 看專業文檔了解更多。

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

 如果你想從視頻中獲取多種圖片,你可以使用下邊的過濾器

$video  ->filters()  ->extractMultipleFrames(FFMpeg\Filters\Video\ExtractMultipleFramesFilter::FRAMERATE_EVERY_10SEC, '/path/to/destination/folder/')  ->synchronize();  $video  ->save(new FFMpeg\Format\Video\X264(), '/path/to/new/file');
生成一個波

你可以使用 FFMpeg\Media\Audio::waveform 方法來從音頻文件中生成一個波。

代碼返回一個 FFMpeg\Media\Waveform 實例。你可以設置不同的尺寸作為參數,  查看API文檔了解更多。

輸出文件必須使用PNG extension.

$waveform = $audio->waveform(640, 120); $waveform->save('waveform.png');

 如果你想從視頻文件中得到一個波, 需要先把視頻文件轉換成音頻文件。

// 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' );
Filters

 你可以使用 FFMpeg\Media\Video::addFilter 方法在 FFMpeg\Media\Video上使用過濾器。 視頻接受視頻和音頻的過濾器。

你可以創建你自己的過濾器並且PHP-FFMpeg也附帶了一些,你可以通過 FFMpeg\Media\Video::filters 方法來使用它們。

過濾器是可以鏈接的

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

 用給定的角度來旋轉視頻。

$video->filters()->rotate($angle);

 $angle 的參數必須是下面的一個常量:

  • FFMpeg\Filters\Video\RotateFilter::ROTATE_90: 90° clockwise
  • FFMpeg\Filters\Video\RotateFilter::ROTATE_180: 180°
  • FFMpeg\Filters\Video\RotateFilter::ROTATE_270: 90° counterclockwise
調整尺寸

按照給定的尺寸調整視頻的大小。

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

調整尺寸的過濾器需要三個參數:

  • $dimension, 一個 FFMpeg\Coordinate\Dimension實例
  • $mode, FFMpeg\Filters\Video\ResizeFilter::RESIZEMODE_* constants常量中的一個。
  • $useStandards, 是否強制使用最新的比率標准的布爾

 如果你想要視頻處在一個非標准的比率, 你可以使用padding 過濾器 調整視頻大小到想要的尺寸,包上黑色的條。

$video->filters()->pad($dimension);

 pad 過濾器需要一個參數 :

  • $dimension, FFMpeg\Coordinate\Dimension的實例

 然后的話不要忘記保存。

$video->save(new FFMpeg\Format\Video\X264(), $new_file);
水印

用給定的圖片給視頻文件添加水印。

$video  ->filters()  ->watermark($watermarkPath, array(  'position' => 'relative',  'bottom' => 50,  'right' => 50,  ));

 watermark 過濾器需要兩個參數:

$watermarkPath, 你水印文件的路徑; $coordinates, 一個定義水印放置位置的數組。你可以使用相對路徑像上面展示的那樣。或者使用絕對路徑像這樣:

$video  ->filters()  ->watermark($watermarkPath, array(  'position' => 'absolute',  'x' => 1180,  'y' => 620,  ));
幀頻

 改變視頻的幀頻

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

framerate(幀頻)過濾器需要兩個參數:

  • $framerate FFMpeg\Coordinate\Framerate的實例
  • $gop, a GOP value (integer)
Synchronize(同步)

同步的音頻和視頻

一些容器使用延遲可能會導致不同步的輸出。過濾器可以解決這個問題。

$video->filters()->synchronize();
Clip(修剪)

在想要的點減掉視頻。

$video->filters()->clip(FFMpeg\Coordinate\TimeCode::fromSeconds(30), FFMpeg\Coordinate\TimeCode::fromSeconds(15));

The clip filter(修剪的過濾器)需要兩個參數:

  • $start一個FFMpeg\Coordinate\TimeCode實例, 指定開始剪切的點
  • $duration, 可選, 一個 FFMpeg\Coordinate\TimeCode實例, 指定要持續的時間

Audio(音頻)

FFMpeg\Media\Audio 可以被轉碼, ie: 改變編碼解碼器, 隔離視頻和音頻。幀可以被提取。

Transcoding(轉碼)

你可以用FFMpeg\Media\Audio:save 方法來給音頻文件轉碼。你可以通過FFMpeg\Format\FormatInterface 來實現。

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

$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');

轉碼過程可以被實時監控, 看API文檔了解更多。

Filters(過濾器)

你可以用FFMpeg\Media\Audio::addFilterFFMpeg\Media\Audio上應用過濾器的效果。只接受音頻過濾器。

你可以創建你自己的過濾器並且PHP-FFMpeg也附帶了一些,你可以通過  FFMpeg\Media\Audio::filters 方法來使用它們。

Metadata(元數據)

向音頻文件中添加元數據。你可以數組的key=value鍵值對的形式添加你想要的元數據。如果過濾器中沒有任何參數傳遞進來,那么所有的元數據將被從輸入文件中刪除。目前支持的數據有 title, artist, album, artist, composer, track, year, description, artwork

$audio->filters()->addMetadata(["title" => "Some Title", "track" => 1]);  //remove all metadata and video streams from audio file $audio->filters()->addMetadata();

 向音頻文件中添加artwork

$audio->filters()->addMetadata(["artwork" => "/path/to/image/file.jpg"]);

注意: 當前ffmpeg (version 3.2.2) only supports 只支持 .mp3 文件的artwork輸出

Resample(重采樣)

 重采樣一個音頻文件。

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

 重采樣過濾器需要兩個參數:

  • $rate, a valid audio sample rate value (integer)

幀是視頻文件在某一個時間點的圖片,看API了解更多。

你可以使用 FFMpeg\Media\Frame::save 方法來保存幀。

$frame->save('target.jpg');

這個方法還有一個可選的布爾類型的參數。設置這個參數為true可以獲得更加精確的圖片,可能稍微花多一點的時間去執行。

Gif

GIf是從視頻文件中提取的一系列的動畫圖片。

 你可以使用 FFMpeg\Media\Gif::save 方法來保存gif文件。

$video = $ffmpeg->open( '/path/to/video' ); $video  ->gif(FFMpeg\Coordinate\TimeCode::fromSeconds(2), new FFMpeg\Coordinate\Dimension(640, 480), 3)  ->save($new_file);

 這個方法有一個可選的布爾類型的參數,就是動畫的持續時間。如果設置的話,可以得到一個確切的gif圖片。

Concatenation(串聯)

這個特性能夠讓你用混合的資源來生成一個視頻或者音頻。

現在有兩種串聯視頻的方式,根據資源的編碼解碼器。如果你的資源都是采用的同一種編碼解碼器的話,你使用  FFMpeg\Media\Concatenate::saveFromSameCodecs 更好一些。 如果你的資源采用不同的編碼解碼器, 你要用到 FFMpeg\Media\Concatenate::saveFromDifferentCodecs.

The first function will use the initial codec as the one for the generated file. With the second function, you will be able to choose which codec you want for the generated file.

You also need to pay attention to the fact that, when using the saveFromDifferentCodecs method, your files MUST have video and audio streams.

In both cases, you will have to provide an array of files.

To concatenate videos encoded with the same codec, do as follow:串聯采用相同編碼解碼器的視頻文件的話,參照下邊的方法:

// In order to instantiate the video object, you HAVE TO pass a path to a valid video file. // We recommand that you put there the path of any of the video you want to use in this concatenation. $video = $ffmpeg->open( '/path/to/video' ); $video  ->concat(array('/path/to/video1', '/path/to/video2'))  ->saveFromSameCodecs('/path/to/new_file', TRUE);

保存函數中的布爾參數能讓你使用拷貝的參數,這個參數可以大福度提高編碼文件的生成過程。

To concatenate videos encoded with the same codec, do as follow:

// In order to instantiate the video object, you HAVE TO pass a path to a valid video file. // We recommand that you put there the path of any of the video you want to use in this concatenation. $video = $ffmpeg->open( '/path/to/video' );  $format = new FFMpeg\Format\Video\X264(); $format->setAudioCodec("libmp3lame");  $video  ->concat(array('/path/to/video1', '/path/to/video2'))  ->saveFromDifferentCodecs($format, '/path/to/new_file');

  在 FFMPEG中更多關於串聯的細節 可看這里 here, here and here.

Formats(格式)

格式要實現 FFMpeg\Format\FormatInterface。FFMpeg\Format\VideoInterface來保存視頻文件, 用 FFMpeg\Format\AudioInterface 來保存音頻文件。

 FFMpeg\Format\ProgressableInterface的話可以獲得轉碼的實時信息。

預定義的格式已經提供了事件的過程信息。

$format = new Format\Video\X264(); $format->on('progress', function ($video, $format, $percentage) {  echo "$percentage % transcoded"; });  $video->save($format, 'video.avi');

為事件提供的回調函數可以隨時調用。

Add additional parameters

你可以根據你的視頻格式為你的編碼提供額外的參數。

setAdditionalParameters 方法的參數是一個數組。

$format = new Format\Video\X264(); $format->setAdditionalParameters(array('foo', 'bar')); $video->save($format, 'video.avi');
創建你自己的格式

 創建一種新的格式最簡單的方法就是集成抽象類 FFMpeg\Format\Video\DefaultVideo and FFMpeg\Format\Audio\DefaultAudio,並且實現下面的方法。

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 使用很多單元來確定時間和空間坐標。

  • FFMpeg\Coordinate\AspectRatio  表示長寬比
  • FFMpeg\Coordinate\Dimension  表示一個維度
  • FFMpeg\Coordinate\FrameRate  表示幀速率
  • FFMpeg\Coordinate\Point  表示一個點
  • FFMpeg\Coordinate\TimeCode  表示一個時間碼

FFProbe

FFMpeg\FFMpeg 內部使用FFMpeg\FFProbe來檢查媒體文件。你也可以用它提取元數據。

$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
$ffprobe = FFMpeg\FFProbe::create(); $ffprobe  ->format('/path/to/video/mp4') // extracts file informations  ->get('duration'); // returns the duration property

使用 Silex Microframework

Service provider is easy to set up:

$app = new Silex\Application(); $app->register(new FFMpeg\FFMpegServiceProvider());  $video = $app['ffmpeg']->open('video.mpeg');

下面是一些可能的選項:

$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, ));


------------------------------------------------------------------------------------------------------------------------------ 

 
         

購買阿里雲服務優惠鏈接:

https://chuangke.aliyun.com/invite?userCode=ff3mpiie

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM