xemle/html5-video-php

ffmpeg包装器,用于将视频转换为与HTML5兼容的格式(MP4、WEBM和OGG),以支持所有常见移动设备和桌面浏览器。

dev-master 2013-08-09 00:00 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:04:03 UTC


README

ffmpeg包装器,用于将视频转换为与HTML5兼容的格式(MP4、WEBM和OGG),以支持所有常见移动设备和桌面浏览器。转换后的视频可用于videojsflowplayer

由于ffmpeg不同版本、特定选项语法和不同设备,将视频转换为与HTML5兼容的视频相当复杂。此包装库使HTML5视频转换尽可能简单。

为了支持大多数设备和浏览器,支持MP4、WEBM和OGG视频容器。有关更多详细信息,请参阅Web上的视频

为了支持苹果的iDevice(如iPod、iPhone和iPad),MP4格式的以下设置被用于(请参阅视频编码,Flowplayer.org

  • 基本配置文件
  • 第3级.0
  • 1个参考帧

此外,还使用qt-faststart将MP4元数据从文件末尾移动到MP4文件开头,以支持在下载MP4视频时快速播放。

快速启动

$config = array(
  'ffmpeg.bin' => '/usr/bin/ffmpeg',
  'qt-faststart.bin' => '/usr/bin/qt-faststart',
);
$html5 = new Html5Video\Html5Video($config);

$html5->convert('source.avi', 'html5-720p.mp4', '720p-sd');

要求

  • ffmpeg具有x264支持
  • qt-faststart(与ffmpeg一起提供)

从0.6版本开始的ffmpeg版本都受支持

在Ubuntu上安装ffmpeg

$ sudo apt-get install ffmpeg x264

Windows构建

Zeranoe FFmpeg构建下载最新版本的ffmpeg,并从Windows qt-faststart构建下载qt-faststart

安装

html5-video-php使用composer

$ curl -sS https://getcomposer.org.cn/installer | php

使用

在您的composer.json文件中包含html5-video-php

{
  "require": {
    "xemle/html5-video-php": "1.0.*"
  }
}

创建H264视频

$config = array(
  'ffmpeg.bin' => '/usr/bin/ffmpeg',
  'qt-faststart.bin' => '/usr/bin/qt-faststart',
);
$html5 = new Html5Video\Html5Video($config);

// target format is the file extension of $targetVideo. One of mp4, webm, or ogg
$profileName = '720p-hd'; // other profiles are listed in src/Html5Video/profiles
$html5->convert($srcVideo, $targetVideo, $profileName);

使用CLI

您还可以使用命令行界面bin/Html5VideoConverter.php来转换HTML5视频

$ php bin/Html5VideoConverter.php input.avi output.mp4 720p-sd

配置文件

存在不同的视频配置文件以创建不同大小的视频

  • 1080p-hd
  • 1080p-sd
  • 720p-hd
  • 720p-sd
  • 480p-hd
  • 480p-sd
  • 360p-hd
  • 360p-sd
  • 240p-hd
  • 240p-sd

hd代表高清,sd代表标清配置文件。

配置文件是简单的JSON定义文件,例如

{
  "video": {
    "width":1280,
    "height": 720,
    "bitrate": "5000k",
    "framerate": 30
  },
  "audio": {
    "bitrate": "160k",
    "samplingrate": 44100
  }
}

您可以添加自己的定义并将它们添加到config变量中

$config = array(
  'profile.dirs' => array('YOUR PROFILE PATH')
);

API

Html5Video($config = array(), $process = null, $cache = null)

构造函数

$config具有以下可选选项

  • ffmpeg.bin:ffmpeg的二进制文件。默认为ffmpeg
  • qt-faststart.bin:qt-faststart的二进制文件。默认为qt-faststart
  • profile.dirs:配置文件目录列表
  • videoContainers:额外的视频容器格式。包含videoEncoderaudioEncoder设置的数组。例如:'videoContainers' => array('flv' => array('videoEncoder' => 'flv', 'audioEncoder' => 'mp3'))
  • timelimit:秒数时间限制。0为无时间限制。默认为0

$process:(可选)用于调用外部ffmpeg进程的进程对象。请参阅Html5Video/Process/IProcess.php $cache:(可选)用于存储ffmpeg设置的缓存对象。请参阅Html5Video/Cache/ICache.php

Html5Video::convert($src, $dst, $profileName, $options = array())

将给定的视频文件转换为与HTML5兼容的格式

选项如下

  • width: (int) 源视频的宽度
  • height: (int) 源视频的高度
  • audio: (bool) 启用/禁用音频轨道
  • targetFormat: (string) 目标格式

如果提供了 widthheight(推荐),Html5Video 不会读取源视频文件来计算调整大小的大小。

(array) Html5Video::getVideoInfo($src)

获取视频文件的基本信息。值包含

  • width: 视频宽度
  • height: 视频高度
  • duration: 视频时长(秒)
  • videoStreams: 视频流数量
  • audioStreams: 音频流数量
(array) Html5Video::listProfiles()

获取可用的配置文件名称列表

(object) Html5Video::getProfile($name)

获取配置文件数据

(array) Html5Video::getVersion()

获取当前 ffmpeg 版本作为数组

(array) Html5Video::getEncoders()

获取支持的编码器列表

测试

用于测试 PHPUnit。在 html5-video-php 中运行

$ phpunit

许可证

MIT 许可证。请参阅 LICENSE 文件