wapmorgan / MediaFile

A unified reader of metadata from audio & video files.
https://wapmorgan.github.io/MediaFile/
MIT License
156 stars 17 forks source link

Error When Reading MP4 File #4

Open tetrahydra opened 6 years ago

tetrahydra commented 6 years ago

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:

wapmorgan commented 6 years ago

Can you provide this file?

wapmorgan commented 6 years ago

@tetrahydra ping

tetrahydra commented 6 years ago

the code:

`` require('BinaryStream.php');

require('FileTypeDetector/ContentStream.php'); require('FileTypeDetector/Detector.php'); require('FileTypeDetector/TerminalInfo.php');

require('src/Exceptions/Exception.php'); require('src/Exceptions/FileAccessException.php'); require('src/Exceptions/ParsingException.php');

require('src/AudioAdapter.php'); require('src/ContainerAdapter.php'); require('src/VideoAdapter.php');

require('src/Adapters/Mpeg4Part12Adapter.php'); require('src/Adapters/Containers/MatroskaContainer.php'); require('src/Adapters/AacAdapter.php'); require('src/Adapters/AmrAdapter.php'); require('src/Adapters/AsfAdapter.php'); require('src/Adapters/AviAdapter.php'); require('src/Adapters/FlacAdapter.php'); require('src/Adapters/MkvAdapter.php'); require('src/Adapters/Mp3Adapter.php'); require('src/Adapters/Mp4Adapter.php'); require('src/Adapters/OggAdapter.php'); require('src/Adapters/WavAdapter.php'); require('src/Adapters/WmaAdapter.php'); require('src/Adapters/WmvAdapter.php');

require('src/MediaFile.php');

try { $media = wapmorgan\MediaFile\MediaFile::open('2018-03-30 114535am.mp4');

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\Exceptions\Exception $e) { // not a media or file is corrupted if ($e instanceof wapmorgan\MediaFile\Exceptions\ParsingException) echo 'File is propably corrupted: '.$e->getMessage().PHP_EOL; else if ($e instanceof wapmorgan\MediaFile\Exceptions\FileAccessException) {} // file is not a media. Just skip }

``

wapmorgan commented 6 years ago

It seems to be ok. Which file used?

wapmorgan commented 6 years ago

To resolve this problem I need file sample.

alex-enchi commented 5 years ago

Hi, i can confirm that this bug exists Video sample with this stream error:

http://file-examples.com/wp-content/uploads/2017/04/file_example_MP4_480_1_5MG.mp4

Another video example with File is propably corrupted: This file does not have "moov" box!

http://techslides.com/demos/sample-videos/small.mp4 from article

PS: Checked these files with id3 lib, width/height info there is accurate

justtoprogram commented 5 years ago

Hi Guys,

I can confirm that I am also receiving this bug.

Message: Warning: Invalid argument supplied for foreach() in .../Plugins/vendor/wapmorgan/media-file/src/Adapters/Mp4Adapter.php on line 14

Here are is an example video I had been testing:

https://drive.google.com/file/d/1h_W80ocTKd-DBc8oowbdxAuaRts1p6X5/view?usp=sharing

Edit:

I was trying to figure out what information was being parsed to the foreach loop with a var_dump & it is was outputting null

capture

capture

justtoprogram commented 5 years ago

Hi Guys, how are we going for a fix on this?

It's getting kind of urgent for our organisation?

alex-enchi commented 5 years ago

@justtoprogram if it is really urgent i would suggest to use https://github.com/JamesHeinrich/getID3

justtoprogram commented 5 years ago

I was avoiding that because I like this library. It does only what I need it to do.

alex-enchi commented 5 years ago

I am as well, but had to use it. I have strong suspect that the reason why it is not working properly is because around Mpeg4Part12Adapter.php#L173 code depends on order of the markers. And I think in real life it can be different. Unfortunately I don't have enough knowledge to fix this. Maybe someone else can dig it.

wapmorgan commented 5 years ago

Going to discover it ...

wapmorgan commented 5 years ago

Really, I don't have the wish and time to fix it.

Maxyandr commented 4 years ago

I have the same problem with the .mp4 files. File is propably corrupted: This file does not have "moov" box! I think, that error caused because the moov atom is located at the end of mp4 file. If you scan your mp4 file with the utility https://gpac.github.io/mp4box.js/test/filereader.html. You can see the location of the moov atom.

But the library try to find moov atom at the beginning. I will try to fix.

elonchen commented 3 years ago

add/edit lines in Mpeg4Part12Adapter.php may help


public function __construct

  $this->stream->saveGroup('mdat_box_header', array(
        'i:size' => 32,
        's:type' => 4,
        'i:extended_size' => 64,
    ));

protected function scan()

   if ($this->getNextBoxType() == 'free'){
        $this->stream->skip(8);
    }

    if ($this->getNextBoxType() == 'mdat') {
        $this->stream->mark('mdat');;
        $this->mdat = $this->stream->readGroup('mdat_box_header');
        $len = $this->mdat['size'] - 16;
        if ($this->mdat['size'] == 1)
            $len = $this->mdat['extended_size'] - 16;
        $this->stream->skip($len); 
    }