PHP Classes

PHP Media File Info: Extract metadata from audio and video files

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 64%Total: 751 This week: 1All time: 4,482 This week: 61Up
Version License PHP version Categories
mediafile 0.1.0MIT/X Consortium ...5.3.6Tools, Files and Folders
Description 

Author

This package can extract metadata from audio and video files.

It can parse different formats of audio and video and extract metadata.

The package can detect the file types if they are audio or video, and what is the format, WAV, FLAC, AAC, OGG, MP3, AMR, WMA for audio, and AVI, WMV and MP4 for video.

It can also return stream length, bit bate, sample rate, channels, width, height, frame rate, number of streams and type of streams.

Innovation Award
PHP Programming Innovation award nominee
February 2017
Number 17
PHP provides good file type detection methods using the fileinfo extension. However, that extension only detect the type of file and does not give much more information.

This package provides means to not only detect the file types of audio and video files, but also extract several types of metadata like audio and video length, resolution, etc.. It supports many types of well known video and audio formats.

Manuel Lemos
Picture of Sergey Vanyushin
  Performance   Level  
Name: Sergey Vanyushin is available for providing paid consulting. Contact Sergey Vanyushin .
Classes: 15 packages by
Country: Russian Federation Russian Federation
Age: 28
All time rank: 57216 in Russian Federation Russian Federation
Week rank: 15 Up1 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 15x

Winner: 2x

Example

#!/usr/bin/php
<?php
require __DIR__.'/../vendor/autoload.php';
use
wapmorgan\MediaFile\FileAccessException;
use
wapmorgan\MediaFile\ParsingException;
use
wapmorgan\MediaFile\MediaFile;

if (
$argc == 1) die('Usage: '.__FILE__.' <files...>'.PHP_EOL);
array_shift($argv);
if (
in_array('--debug', $argv)) {
   
define('DEBUG', true);
    unset(
$argv[array_search('--debug', $argv)]);
}

foreach (
$argv as $file) {
    try {
       
$media = MediaFile::open($file);
        if (
$media->isAudio()) {
           
$length = $media->getAudio()->getLength();
            echo
$file.' '.$media->getType().':'.$media->getFormat().' '.sprintf('%d:%02d', floor($length / 60), floor($length % 60)).' at '.($media->getAudio()->getSampleRate() / 1000).'KHz '.($media->getAudio()->getBitRate() / 1000).'Kbps'.PHP_EOL;
        } else if (
$media->isContainer()) {
           
$length = $media->getVideo()->getLength();
            echo
$file.' '.$media->getType().':'.$media->getFormat().' ['.$media->getVideo()->countAudioStreams().' audio / '.$media->getVideo()->countVideoStreams().' video] '.sprintf('%d:%02d', floor($length / 60), floor($length % 60)).' '.$media->getVideo()->getWidth().'x'.$media->getVideo()->getHeight().' '.$media->getVideo()->getFramerate().'fps'.PHP_EOL;
        } else {
           
$length = $media->getVideo()->getLength();
            echo
$file.' '.$media->getType().':'.$media->getFormat().' '.sprintf('%d:%02d', floor($length / 60), floor($length % 60)).' '.$media->getVideo()->getWidth().'x'.$media->getVideo()->getHeight().' '.$media->getVideo()->getFramerate().'fps'.PHP_EOL;
        }
    } catch (
Exception $e) {
        if (
$e instanceof FileAccessException)
            echo
'File '.$file.' is not a media file'.PHP_EOL;
        else {
            echo
'File is propably corrupted: '.$e->getMessage().PHP_EOL;
        }
    }
}


Details

MediaFile

Allows you easily get meta information about any media file with unified interface.

Composer package Latest Stable Version License Latest Unstable Version

It can retrieve following information:

  • For any audio: length, bitRate, sampleRate, channels
  • For any video: length, width, height, frameRate
  • For any container: number of streams, type of streams, formats of streams

Table of contents:

  1. Usage
  2. Supported formats
  3. API
  4. Technical details

Usage

try {
  $media = wapmorgan\MediaFile\MediaFile::open('123.mp3');
  // for audio
  if ($media->isAudio()) {
    // calls to AudioAdapter interface
    echo 'Duration: '.$media->getAudio()->getLength().PHP_EOL;
    echo 'Bit rate: '.$media->getAudio()->getBitRate().PHP_EOL;
    echo 'Sample rate: '.$media->getAudio()->getSampleRate().PHP_EOL;
    echo 'Channels: '.$media->getAudio()->getChannels().PHP_EOL;
  }
  // for video
  else {
    // calls to VideoAdapter interface
    echo 'Duration: '.$media->getVideo()->getLength().PHP_EOL;
    echo 'Dimensions: '.$media->getVideo()->getWidth().'x'.$media->getVideo()->getHeight().PHP_EOL;
    echo 'Framerate: '.$media->getVideo()->getFramerate().PHP_EOL;
  }
} catch (wapmorgan\MediaFile\Exception $e) {
  // not a media or file is corrupted
}

Supported formats

  • Audio - wav - flac - aac - ogg - mp3 - amr - wma
  • Video - avi (also as container) - wmv (also as container) - mp4 (also as container)

Other formats support coming soon.

API

MediaFile

wapmorgan\wapmorgan\MediaFile

| Method | Description | Notes | |------------------------------------------|-----------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------| | static open($filename) | Detects file type and format and calls constructor with these parameters. | Throws an \Exception if file is not a media or is not accessible. | | __construct($filename, $type, $format) | Opens file and reads metadata. | Available $type values: MediaFile::AUDIO, MediaFile::VIDEO. Available $format values see below. | | isAudio() | Returns true if media is just audio. | | | isVideo() | Returns true if media is a video with audio. | | | isContainer() | Returns true if media is also a container (can store multiple audios and videos). | | | getType() | Returns media file type. | | | getFormat() | Returns media file format. | | | getAudio() | Returns an AudioAdapter interface for audio. | | | getVideo() | Returns an VideoAdapter interface for video. | |

Available formats:

  1. For `MediaFile::AUDIO`:

    | `MediaFile::WAV` | `MediaFile::FLAC` | `MediaFile::AAC` | `MediaFile::OGG` | |----------------------|----------------------|----------------------|------------------| | MediaFile::MP3 | MediaFile::AMR | MediaFile::WMA | |

  2. For `MediaFile::VIDEO`:

    | `MediaFile::AVI` | `MediaFile::WMV` | `MediaFile::MP4` | |------------------|------------------|------------------|

AudioAdapter

wapmorgan\MediaFile\AudioAdapter

| Method | Description | |-----------------------|-------------------------------------------------------------------| | getLength() | Returns audio length in seconds and microseconds as _float_. | | getBitRate() | Returns audio bit rate as _int_. | | getSampleRate() | Returns audio sampling rate as _int_. | | getChannels() | Returns number of channels used in audio as _int_. | | isVariableBitRate() | Returns whether format support VBR and file has VBR as _boolean_. | | isLossless() | Returns whether format has compression lossless as _boolean_. |

VideoAdapter

wapmorgan\MediaFile\VideoAdapter

| Method | Description | |------------------|--------------------------------------------------------------| | getLength() | Returns video length in seconds and microseconds as _float_. | | getWidth() | Returns width of video as _int_. | | getHeight() | Returns height of video as _int_. | | getFramerate() | Returns video frame rate of video as _int_. |

ContainerAdapter

wapmorgan\MediaFile\ContainerAdapter

| Method | Description | |-----------------------|--------------------------------------------------| | countStreams() | Returns number of streams in container as _int_. | | countVideoStreams() | Returns number of video streams as _int_. | | countAudioStreams() | Returns number of audio streams as _int_. | | getStreams() | Returns streams information as _array_. |

Technical information

| Format | Full format name | Specifications | Notes | |--------|--------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------| | aac | MPEG 4 Part 12 container with audio only | http://l.web.umkc.edu/lizhu/teaching/2016sp.video-communication/ref/mp4.pdf | Does not provide support of MPEG2-AAC | | amr | AMR-NB | http://hackipedia.org/File%20formats/Containers/AMR,%20Adaptive%20MultiRate/AMR%20format.pdf | Does not provide support of AMR-WB | | avi | - | http://www.alexander-noe.com/video/documentation/avi.pdf | | | flac | - | - | Support based on third-party library | | mp3 | MPEG 1/2 Layer 1/2/3 | https://github.com/wapmorgan/mp3info#technical-information | | | mp4 | MPEG 4 Part 12/14 container with few audio and video streams | Part 12 specification: http://l.web.umkc.edu/lizhu/teaching/2016sp.video-communication/ref/mp4.pdf Part 14 extension: https://www.cmlab.csie.ntu.edu.tw/~cathyp/eBooks/14496_MPEG4/ISO_IEC_14496-14_2003-11-15.pdf | | | ogg | Ogg container with Vorbis audio | https://xiph.org/vorbis/doc/Vorbis_I_spec.html | | | wav | - | - | Support based on third-party library | | wma | ASF container with only one audio stream | http://go.microsoft.com/fwlink/p/?linkid=31334 | | | wmv | ASF container with few audio and video streams | http://go.microsoft.com/fwlink/p/?linkid=31334 | |


  Files folder image Files (24)  
File Role Description
Files folder imagebin (1 file)
Files folder imagesrc (19 files)
Plain text file composer.json Data Auxiliary data
Plain text file LICENSE Lic. License text
Plain text file README.md Doc. Documentation
Plain text file _config.yml Data Auxiliary data

  Files folder image Files (24)  /  bin  
File Role Description
  Plain text file mediafile Example Example script

  Files folder image Files (24)  /  src  
File Role Description
  Plain text file AacAdapter.php Class Class source
  Plain text file AmrAdapter.php Class Class source
  Plain text file AsfAdapter.php Class Class source
  Plain text file AudioAdapter.php Class Class source
  Plain text file AviAdapter.php Class Class source
  Plain text file ContainerAdapter.php Class Class source
  Plain text file Exception.php Class Class source
  Plain text file FileAccessException.php Class Class source
  Plain text file FlacAdapter.php Class Class source
  Plain text file MediaFile.php Class Class source
  Plain text file Mp3Adapter.php Class Class source
  Plain text file Mp4Adapter.php Class Class source
  Plain text file Mpeg4Part12Adapter.php Class Class source
  Plain text file OggAdapter.php Class Class source
  Plain text file ParsingException.php Class Class source
  Plain text file VideoAdapter.php Class Class source
  Plain text file WavAdapter.php Class Class source
  Plain text file WmaAdapter.php Class Class source
  Plain text file WmvAdapter.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:751
This week:1
All time:4,482
This week:61Up
 User Ratings  
 
 All time
Utility:80%StarStarStarStarStar
Consistency:80%StarStarStarStarStar
Documentation:80%StarStarStarStarStar
Examples:85%StarStarStarStarStar
Tests:-
Videos:-
Overall:64%StarStarStarStar
Rank:705