PHP-FFmpeg

什么是FFmpeg

FFmpeg是一个自由软件,可以运行音频和视频多种格式的录影、转换、流功能,包含了libavcodec ─这是一个用于多个项目中音频和视频的解码器库,以及libavformat——一个音频与视频格式转换库。
这个项目最初是由Fabrice Bellard发起的,而现在是由Michael Niedermayer在进行维护。许多FFmpeg的开发者同时也是MPlayer项目的成员,FFmpeg在MPlayer项目中是被设计为服务器版本进行开发。

FFmpeg Git地址

安装ffmpeg

mac下安装

1
brew reinstall ffmpeg --with-faac --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools

ubuntu

1
2
3
sudo add-apt-repository ppa:djcj/hybrid
sudo apt-get update
sudo apt-get install ffmpeg

ubuntu16安装

centOS 6安装

1
2
3
4
sudo yum install epel-release -y
sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el6/x86_64/nux-dextop-release-0-2.el6.nux.noarch.rpm
sudo yum install ffmpeg ffmpeg-devel -y

Debian8

1
vi /etc/apt/sources.list

Add the following lines at the end of the file:

1
2
3
4
5
6
# deb-multimedia
deb http://www.deb-multimedia.org jessie main non-free
deb-src http://www.deb-multimedia.org jessie main non-free

# jessie-backports
deb http://httpredir.debian.org/debian/ jessie-backports main

Update the package list and install deb-multimedia keyring:

1
2
3
sudo apt-get update
sudo apt-get install deb-multimedia-keyring
sudo apt-get update

Install FFmpeg with the following command:

1
sudo apt-get install ffmpeg

源码安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
mkdir software && cd software
curl -O http://ffmpeg.org/releases/ffmpeg-4.1.2.tar.bz2
tar -xvf ffmpeg-4.1.2.tar.bz2

cd ffmpeg-4.1.2

# 安装会缺少不少依赖,缺什么补什么~ apt-install
./configure --prefix=/usr \
--enable-gpl \
--enable-version3 \
--enable-nonfree \
--disable-static \
--enable-shared \
--disable-debug \
--enable-avresample \
--enable-libass \
--enable-libfdk-aac \
--enable-libfreetype \
--enable-libmp3lame \
--enable-libtheora \
--enable-libvorbis \
--enable-libvpx \
--enable-libx264 \
#--enable-libopus \
#optional --enable-libx265

make
sudo make install

可能会遇到以下问题

  1. Cannot retrieve metalink for repository: epel.

    1
    run  yum --disablerepo=epel -y update  ca-certificates
  2. /primary.sqlite.bz2: [Errno -1]

    1
    run  yun clean all

安装成功后测试:

1
ffmpeg -version

安装php-ffmpeg

1
composer require php-ffmpeg/php-ffmpeg

基本用法

实例对象

1
2
3
4
5
6
7
$ffmpeg = FFMpeg\FFMpeg::create(array(
//ffmpeg指令文件的引用,按实际情况设置,引用失败会提示Unable to load FFMpeg
'ffmpeg.binaries' => '/usr/local/Cellar/ffmpeg/3.2.4/bin/ffmpeg',
'ffprobe.binaries' => '/usr/local/Cellar/ffmpeg/3.2.4/bin/ffprobe',
'timeout' => 3600, // The timeout for the underlying process
'ffmpeg.threads' => 12, // The number of threads that FFMpeg should use
));

视频转码

1
2
3
4
5
6
7
8
9
10
11
12
13
$video = $ffmpeg->open(public_path().'/upload/audio.mp4');

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

$format
->setKiloBitrate(1000)
->setAudioChannels(2)
->setAudioKiloBitrate(256);

$video->save($format, public_path().'/upload/video.mp4');

相关问题

  1. Unable to load FFMpeg
    先安装ffmpeg,正确引用bin文件下的命令文件
  2. Mac环境下Encoding Failed
    原因是缺少libfaac,实例化format,传libmp3lame

    1
    $format = new FFMpeg\Format\Video\X264('libmp3lame', 'libx264');

    CentOs 6 使用libfaac

-------------本文结束感谢您的阅读-------------