mhor/php-mediainfo 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mhor/php-mediainfo

Composer 安装命令:

composer require mhor/php-mediainfo

包简介

PHP wrapper around the mediainfo command

README 文档

README

Introduction

PHP wrapper around the mediainfo command

Table of contents:

Installation

1 - Install mediainfo

You should install mediainfo:

On linux:

$ sudo apt-get install mediainfo

On Mac:

$ brew install mediainfo

2 - Integration in your php project

To use this library install it through Composer, run:

$ composer require mhor/php-mediainfo

How to use

Retrieve media information container

<?php
//...
use Mhor\MediaInfo\MediaInfo;
//...
$mediaInfo = new MediaInfo();
$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');
//...

Get general information from media information container

$general = $mediaInfoContainer->getGeneral();

Get videos information from media information container

$videos = $mediaInfoContainer->getVideos();

foreach ($videos as $video) {
    // ... do something
}

Get audios information from media information container

$audios = $mediaInfoContainer->getAudios();

foreach ($audios as $audio) {
    // ... do something
}

Get subtitles information from media information container

$subtitles = $mediaInfoContainer->getSubtitles();

foreach ($subtitles as $subtitle) {
    // ... do something
}

Get images information from media information container

$images = $mediaInfoContainer->getImages();

foreach ($images as $image) {
    // ... do something
}

Get menus information from media information container

$menus = $mediaInfoContainer->getMenus();

foreach ($menus as $menu) {
    // ... do something
}

Example

<?php

require './vendor/autoload.php';

use Mhor\MediaInfo\MediaInfo;

$mediaInfo = new MediaInfo();
$mediaInfoContainer = $mediaInfo->getInfo('./SampleVideo_1280x720_5mb.mkv');

echo "Videos channel: \n";
echo "=======================\n";
foreach ($mediaInfoContainer->getVideos() as $video) {
    if ($video->has('format')) {
        echo 'format: '.(string)$video->get('format')."\n";
    }

    if ($video->has('height')) {
        echo 'height: '.$video->get('height')->getAbsoluteValue()."\n";
    }

    echo "\n---------------------\n";
}

echo "Audios channel: \n";
echo "=======================\n";
foreach ($mediaInfoContainer->getAudios() as $audio) {
    $availableInfo = $audio->list();
    foreach ($availableInfo as $key) {
        echo $audio->get($key);
    }
    echo "\n---------------------\n";
}

Ignore unknown types

By default, unknown type throw an error this, to avoid this behavior, you can do:

$mediaInfo = new MediaInfo();
$mediaInfo->setConfig('ignore_unknown_track_types', true);

$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');

$others = $mediaInfoContainer->getOthers();
foreach ($others as $other) {
    // ... do something
}

Access to information

Get all information into an array

$informationArray = $general->get();

Get one information by field name

Field Name are in lower case separated by "_"

$oneInformation = $general->get('count_of_audio_streams');

Check if information exists

Field Name are in lower case separated by "_"

if ($general->has('count_of_audio_streams')) {
    echo $general->get('count_of_audio_streams');
}

List available information

$availableInfo = $general->list();
foreach ($availableInfo as $key) {
    echo $general->get($key);
}

Specials types

Cover

For field:

  • cover_data

Cover type will be applied

Duration

For fields:

  • duration
  • delay_relative_to_video
  • video0_delay
  • delay

Duration type will be applied

Mode

For fields:

  • overall_bit_rate_mode
  • overall_bit_rate
  • bit_rate_mode
  • compression_mode
  • codec
  • format
  • kind_of_stream
  • writing_library
  • id
  • format_settings_sbr
  • channel_positions
  • default
  • forced
  • delay_origin
  • scan_type
  • interlacement
  • scan_type
  • frame_rate_mode
  • format_settings_cabac
  • unique_id

Mode type will be applied

Rate

For fields:

  • channel_s
  • bit_rate
  • sampling_rate
  • bit_depth
  • width
  • nominal_bit_rate
  • format_settings_reframes
  • height
  • resolution
  • maximum_bit_rate

Rate type will be applied

FloatRate

For fields:

  • frame_rate

FloatRate type will be applied

Ratio

For fields:

  • display_aspect_ratio
  • original_display_aspect_ratio

Ratio type will be applied

Size

For fields:

  • file_size
  • stream_size

Size type will be applied

Others

  • All date fields will be transformed into Datetime php object

Extra

Use custom mediainfo path

$mediaInfo = new MediaInfo();
$mediaInfo->setConfig('command', '/usr/local/bin/mediainfo');
$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');

Support old mediainfo version (<17.10)

$mediaInfo = new MediaInfo();
$mediaInfo->setConfig('use_oldxml_mediainfo_output_format', false);
$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');

Use url as filepath

$mediaInfo = new MediaInfo();
$mediaInfoContainer = $mediaInfo->getInfo('http://example.org/music/test.mp3');

MediaInfoContainer to JSON, Array or XML

$mediaInfo = new MediaInfo();
$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');

$json = json_encode($mediaInfoContainer);
$array = $mediaInfoContainer->__toArray();
$xml = $mediaInfoContainer->__toXML();

Usage for WindowsOS

Download MediaInfo CLI from here. Extract zip-archive and place MediaInfo.exe somewhere. Use it:

$mediaInfo = new MediaInfo();
$mediaInfo->setConfig('command', 'C:\path\to\directory\MediaInfo.exe');
$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');

Urlencode Config

By default, MediaInfo tries to detect if a URL is already percent-encode and encodes the URL when it's not. Setting the 'urlencode' config setting to true forces MediaInfo to encode the URL despite the presence of percentage signs in the URL. This is for example required when using pre-signed URLs for AWS S3 objects.

$mediaInfo = new MediaInfo();
$mediaInfo->setConfig('urlencode', true);
$mediaInfoContainer = $mediaInfo->getInfo('https://demo.us-west-1.amazonaws.com/video.mp4?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ABC%2F123%2Fus-west-1%2Fs3%2Faws4_request&X-Amz-Date=20200721T114451Z&X-Amz-SignedHeaders=host&X-Amz-Expires=600&X-Amz-Signature=123');

This setting requires MediaInfo 20.03 minimum

Cover data

Recent versions of MediaInfo don't include cover data by default, without passing an additional flag. To include any available cover data, set the 'include_cover_data' config setting to true. See the cover type for details on retrieving the base64 encoded image from cover_data.

Originally this cover data was always included in the MediaInfo output, so this option is unnecessary for older versions. But around version 18 cover data was removed from the output by default, unless you also pass the --Cover_Data=base64 flag.

$mediaInfo = new MediaInfo();
$mediaInfo->setConfig('include_cover_data', true);
$mediaInfoContainer = $mediaInfo->getInfo('music.mp3');

$general = $mediaInfoContainer->getGeneral();
if ($general->has('cover_data')) {
    $attributeCover = $general->get('cover_data');
    $base64EncodedImage = $attributeCover->getBinaryCover();
}

Note: Older versions of MediaInfo will print the following error if passed this flag:

$ mediainfo ./music.mp3 -f --OUTPUT=OLDXML --Cover_Data=base64
Option not known

Override attribute checkers/types

This configuration allows you to customize the return values of attributes in php-mediainfo by creating custom checker and attribute classes. You can extend existing classes, override methods, and add additional functionality to provide more comprehensive or specialized information in the attribute objects returned by php-mediainfo.

  1. Create a new class that implements the AttributeCheckerInterface
class CustomDurationChecker extends \Mhor\MediaInfo\Checker\DurationChecker
{
    public function create($durations): \Mhor\MediaInfo\Attribute\Duration
    {
        return new CustomDuration($durations[0]);
    }

    public function getMembersFields(): array
    {
        return [
            'duration',
        ];
    }
}
  1. Create a new class that implements the AttributeInterface
class CustomDuration extends \Mhor\MediaInfo\Attribute\Duration
{
    public function getSeconds()
    {
        return $this->getMilliseconds() / 1000;
    }
}
  1. Set the new list of attribute checkers into the config
$mediaInfo = new MediaInfo();
$mediaInfo->setConfig('attribute_checkers', [new CustomDurationChecker()]);
$mediaInfoContainer = $mediaInfo->getInfo(
    './SampleVideo_1280x720_5mb.mkv'
);

foreach ($mediaInfoContainer->getAudios() as $audio) {
    echo $audio->get('duration')->getSeconds();
}

Symfony integration

Look at this bundle: MhorMediaInfoBunde

Codeigniter integration

Look at this to use composer with Codeigniter

License

See LICENSE for more information

mhor/php-mediainfo 适用场景与选型建议

mhor/php-mediainfo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 598.68k 次下载、GitHub Stars 达 120, 最近一次更新时间为 2014 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「metadata」 「wrapper」 「id3」 「mediainfo」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mhor/php-mediainfo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mhor/php-mediainfo 我们能提供哪些服务?
定制开发 / 二次开发

基于 mhor/php-mediainfo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 598.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 120
  • 点击次数: 26
  • 依赖项目数: 9
  • 推荐数: 2

GitHub 信息

  • Stars: 120
  • Watchers: 7
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-12-11