Git Product home page Git Product logo

mediafile's Introduction

MediaFile

Allows you easily get meta information about any media file with unified interface. The library has no requirements of external libs or system unitilies.

Latest Stable Version License Latest Unstable Version Tests

Supported formats

Audio Video
aac, amr, flac, mp3, ogg, wav, wma avi, mkv, mp4, wmv
- length - length
- bitRate - width
- sampleRate - height
- channels - frameRate

Table of contents:

  1. Usage
  2. API
  3. Why not using getID3?
  4. Technical details

Usage

use wapmorgan\MediaFile\MediaFile;

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

API

MediaFile

wapmorgan\wapmorgan\MediaFile

Method Description Notes
static open($filename): MediaFile Detects file type and format and calls constructor with these parameters. Throws an \Exception if file is not a media or is not accessible.
isAudio(): boolean Returns true if media is just audio.
isVideo(): boolean Returns true if media is a video with audio.
isContainer(): boolean Returns true if media is also a container (can store multiple audios and videos).
getFormat(): string Returns media file format.
getAudio(): AudioAdapter Returns an AudioAdapter interface for audio.
getVideo(): VideoAdapter Returns an VideoAdapter interface for video.

AudioAdapter

wapmorgan\MediaFile\AudioAdapter

Method Description
getLength(): float Returns audio length in seconds and microseconds as float.
getBitRate(): int Returns audio bit rate as int.
getSampleRate(): int Returns audio sampling rate as int.
getChannels(): int Returns number of channels used in audio as int.
isVariableBitRate(): boolean Returns whether format support VBR and file has VBR as boolean.
isLossless(): boolean Returns whether format has compression lossless as boolean.

VideoAdapter

wapmorgan\MediaFile\VideoAdapter

Method Description
getLength(): int Returns video length in seconds and microseconds as float.
getWidth(): int Returns width of video as int.
getHeight(): int Returns height of video as int.
getFramerate(): int Returns video frame rate of video as int.

ContainerAdapter

wapmorgan\MediaFile\ContainerAdapter

Method Description
countStreams(): int Returns number of streams in container as int.
countVideoStreams(): int Returns number of video streams as int.
countAudioStreams(): int Returns number of audio streams as int.
getStreams(): array Returns streams information as array.

Why not using getID3?

getID3 library is very popular and has a lot of features, but it's old and too slow.

Following table shows comparation of analyzing speed of fixtures, distributed with first release of MediaFile:

File getID3 MediaFile Speed gain
video.avi 0.215 0.126 1.71x
video.mp4 3.055 0.429 7.12x
video.wmv 0.354 0.372 0.95x
audio.aac 0.560 0.262 2.13x
audio.amr 8.241 12.248 0.67x
audio.flac 1.880 0.071 26.41x
audio.m4a 13.372 0.169 79.14x
audio.mp3 10.931 0.077 141.54x
audio.ogg 0.170 0.096 1.78x
audio.wav 0.114 0.070 1.64x
audio.wma 0.195 0.158 1.23x

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
mkv Matroska container https://www.matroska.org/technical/specs/index.html
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

mediafile's People

Contributors

wapmorgan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mediafile's Issues

MP3 file generates exception

Hello!
I have an MP3 file which is generating an Unknown format for file "/tmp/phpaa3d76"! exception, but works ok in other players and macOS recognizes it without any problem.
I later tried to fix it using using this tool and now it work just fine.

In the archive I'm sharing bot MP3s, the original and the fixed one: Archive.zip

Maybe this can be handled in the library and the exception avoided.

Division by zero for files

Hello,

first I would like to thank you for this awesome project. Unfortunately with some of my files I get a Division by zero error, that is not catchable...

It happens with multiple mp3 files, which i cannot provide publicly due to copyright but I think you should in any case fix the possible division by zero. If you really need the files, feel free to contact me again, I'll try to provide you a sample.

Heres a trace:

... Division by zero (Code: 2) ...wapmorgan/mp3info/src/Mp3Info.php, line 384
#0 ...wapmorgan/mp3info/src/Mp3Info.php(384): {closure}(2, 'Division by zer...', '...', 384, Array)
#1 ...wapmorgan/mp3info/src/Mp3Info.php(279): wapmorgan\Mp3Info\Mp3Info->readMpegFrame(Resource id #24)
#2 ...wapmorgan/mp3info/src/Mp3Info.php(224): wapmorgan\Mp3Info\Mp3Info->parseAudio(Object(SplFileInfo), 8449196, 1)
#3 ...wapmorgan/media-file/src/Adapters/Mp3Adapter.php(15): wapmorgan\Mp3Info\Mp3Info->__construct(Object(SplFileInfo))
#4 ...wapmorgan/media-file/src/MediaFile.php(66): wapmorgan\MediaFile\Adapters\Mp3Adapter->__construct(Object(SplFileInfo))
#5 ...wapmorgan/media-file/src/MediaFile.php(44): wapmorgan\MediaFile\MediaFile->__construct(Object(SplFileInfo), 'audio', 'mp3')
#6 ...: wapmorgan\MediaFile\MediaFile::open(Object(SplFileInfo))
#7 {main}

I think that the $this->sampleRate variable is not set correctly. Since it is not possible to catch this error under normal circumstances, I wrote a little script to, that's why it is a trace and not a fatal error output:

$path = 'path/to/mp3/files';

$directory = new \RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS | FilesystemIterator::CURRENT_AS_FILEINFO);
$files = new \RecursiveIteratorIterator($directory);

set_error_handler(function ($errorCode, $errorMessage, $file, $line) {
    if (!(error_reporting() & $errorCode)) {
        return false;
    }
    throw new Exception(sprintf('%s (Code: %s) in %s, line %s', $errorMessage,$errorCode, $file, $line));
});

foreach ($files as $file) {
    if ($file->getExtension() !== "mp3") {
        continue;
    }
    try {
        $mediaFile = MediaFile::open($file);
    } catch (Throwable $t) {
        echo $file . PHP_EOL;
        echo " ===> exception: " . $t->getMessage() . PHP_EOL;
        echo $t->getTraceAsString().PHP_EOL;
    }
}
restore_error_handler();

Error When Reading MP4 File

Hello,

I am reading an MP4 file, and I am getting this error:

Duration: 612.437
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/mediametadata/src/Adapters/Mp4Adapter.php on line 14

Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/mediametadata/src/Adapters/Mp4Adapter.php on line 21
Dimensions: x
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/mediametadata/src/Adapters/Mp4Adapter.php on line 28
Framerate:

Fatal Error during reading .wav file.

Here is issue text : "fread(): Argument #2 ($length) must be greater than 0";
It happens in analyzeMetadata function inside "if ($size >= 20)";
It happens for some .wav files. And i'm using actual .wav files.
I tried to convert my .wav file to .wav again using random online converter and this issue gone. I have been using this library for 2 years and have never got this issue (processed huge amount of files). And recently i started getting this issue again and again.
Can you suggest what should i do in this situation?

Can't read a video/mov file

When trying to open this file, I get

In MediaFile.php line 42:

  File "C:\Users\tacma\OneDrive\Pictures\JUFJ\Amanda\amanda--1.MOV" is not a supported video/audio, it's "video/mov"!

fail on getLength for some ogg

(MediaFile::open(...))
            ->getAudio()
            ->getLength();

fread() expects parameter 2 to be int, string given in wapmorgan/binary-stream/src/BinaryStream.php because segments_count === ''.

4e721b43.ogg.zip

Make wave support optional untill boyhagemann/Wave has a stable release

Currently the boyhagemann/Wave package is blocking the installation of this package unless forced or one changes there policy to accept dev packages.

boyhagemann hasn't responded to the request to do so for nearly a year and there has been no development since 2013.

A way around this would be to make wave support optional and not include the package by default. I for one don't need wave support and I would much rather use this libery then getID3, both for speed and and I have very little trust in there code as it's a big mess.

Standard GoPro MP4 file fails

Using just the supplied sample code with a simple GoPro MP4 file will output the following:

Duration: 338.304
PHP Warning:  Invalid argument supplied for foreach() in .../vendor/wapmorgan/media-file/src/Adapters/Mp4Adapter.php on line 14
PHP Warning:  Invalid argument supplied for foreach() in .../vendor/wapmorgan/media-file/src/Adapters/Mp4Adapter.php on line 21
Dimensions: x
PHP Warning:  Invalid argument supplied for foreach() in .../wapmorgan/media-file/src/Adapters/Mp4Adapter.php on line 28
Framerate: 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.