PHP code example of mhor / php-mediainfo

1. Go to this page and download the library: Download mhor/php-mediainfo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mhor / php-mediainfo example snippets



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

$general = $mediaInfoContainer->getGeneral();

$videos = $mediaInfoContainer->getVideos();

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

$audios = $mediaInfoContainer->getAudios();

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

$subtitles = $mediaInfoContainer->getSubtitles();

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

$images = $mediaInfoContainer->getImages();

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

$menus = $mediaInfoContainer->getMenus();

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



hor\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";
}

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

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

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

$informationArray = $general->get();

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

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

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

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

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

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

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

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

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

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

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

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

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',
        ];
    }
}

class CustomDuration extends \Mhor\MediaInfo\Attribute\Duration
{
    public function getSeconds()
    {
        return $this->getMilliseconds() / 1000;
    }
}

$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();
}
bash
$ composer