Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally Ignore unknown TrackTypes #18

Merged
merged 14 commits into from
Jun 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Builder/MediaInfoContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function setVersion($version)
/**
* @param $typeName
* @param array $attributes
* @throws Mhor\MediaInfo\Exception\UnknownTrackTypeException
*/
public function addTrackType($typeName, array $attributes)
{
Expand Down
28 changes: 27 additions & 1 deletion src/Container/MediaInfoContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Mhor\MediaInfo\Type\Image;
use Mhor\MediaInfo\Type\Subtitle;
use Mhor\MediaInfo\Type\Video;
use Mhor\MediaInfo\Type\Other;

class MediaInfoContainer
{
Expand All @@ -16,6 +17,7 @@ class MediaInfoContainer
const IMAGE_CLASS = 'Mhor\MediaInfo\Type\Image';
const VIDEO_CLASS = 'Mhor\MediaInfo\Type\Video';
const SUBTITLE_CLASS = 'Mhor\MediaInfo\Type\Subtitle';
const OTHER_CLASS = 'Mhor\MediaInfo\Type\Other';

/**
* @var string
Expand Down Expand Up @@ -47,6 +49,11 @@ class MediaInfoContainer
*/
private $images = array();

/**
* @var Other[]
*/
private $others = array();

/**
* @return General
*/
Expand All @@ -71,6 +78,14 @@ public function getImages()
return $this->images;
}

/**
* @return Other[]
*/
public function getOthers()
{
return $this->others;
}

/**
* @param string $version
*/
Expand Down Expand Up @@ -133,8 +148,11 @@ public function add(AbstractType $trackType)
case self::SUBTITLE_CLASS:
$this->addSubtitle($trackType);
break;
case self::OTHER_CLASS:
$this->addOther($trackType);
break;
default:
throw new \Exception('Unknow type');
throw new \Exception('Unknown type');
}
}

Expand Down Expand Up @@ -169,4 +187,12 @@ private function addSubtitle(Subtitle $subtitle)
{
$this->subtitles[] = $subtitle;
}

/**
* @param Other $other
*/
private function addOther(Other $other)
{
$this->others[] = $other;
}
}
19 changes: 19 additions & 0 deletions src/Exception/UnknownTrackTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Mhor\MediaInfo\Exception;

class UnknownTrackTypeException extends \Exception
{
private $trackType = null;

public function __construct($trackType, $code = 0)
{
parent::__construct(sprintf('Type doesn\'t exist: %s', $trackType), $code, null);
$this->trackType = $trackType;
}

public function getTrackType()
{
return $this->trackType;
}
}
9 changes: 7 additions & 2 deletions src/Factory/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Mhor\MediaInfo\Type\Image;
use Mhor\MediaInfo\Type\Subtitle;
use Mhor\MediaInfo\Type\Video;
use Mhor\MediaInfo\Type\Other;
use Mhor\MediaInfo\Exception\UnknownTrackTypeException;

class TypeFactory
{
Expand All @@ -16,11 +18,12 @@ class TypeFactory
const GENERAL = 'General';
const VIDEO = 'Video';
const SUBTITLE = 'Text';
const OTHER = 'Other';

/**
* @param $type
* @return AbstractType
* @throws \Exception
* @throws Mhor\MediaInfo\Exception\UnknownTrackTypeException
*/
public function create($type)
{
Expand All @@ -35,8 +38,10 @@ public function create($type)
return new Video();
case self::SUBTITLE:
return new Subtitle();
case self::OTHER:
return new Other();
default:
throw new \Exception('Type doesn\'t exist');
throw new UnknownTrackTypeException($type);
}
}
}
7 changes: 5 additions & 2 deletions src/MediaInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ class MediaInfo
{
/**
* @param $filePath
* @param bool $ignoreUnknownTrackTypes Optional parameter used to skip unknown track types by passing true. The
default behavior (false) is throw an exception on unknown track types.
* @throws Mhor\MediaInfo\Exception\UnknownTrackTypeException
* @return MediaInfoContainer
*/
public function getInfo($filePath)
public function getInfo($filePath, $ignoreUnknownTrackTypes = false)
{
$mediaInfoCommandBuilder = new MediaInfoCommandBuilder();
$output = $mediaInfoCommandBuilder->buildMediaInfoCommandRunner($filePath)->run();

$mediaInfoOutputParser = new MediaInfoOutputParser();
$mediaInfoOutputParser->parse($output);

return $mediaInfoOutputParser->getMediaInfoContainer();
return $mediaInfoOutputParser->getMediaInfoContainer($ignoreUnknownTrackTypes);
}
}
19 changes: 15 additions & 4 deletions src/Parser/MediaInfoOutputParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Mhor\MediaInfo\Builder\MediaInfoContainerBuilder;
use Mhor\MediaInfo\Container\MediaInfoContainer;
use Mhor\MediaInfo\Exception\UnknownTrackTypeException;

class MediaInfoOutputParser extends AbstractXmlOutputParser
{
Expand All @@ -21,20 +22,30 @@ public function parse($output)
}

/**
* @throws \Exception
* @param bool $ignoreUnknownTrackTypes Optional parameter used to skip unknown track types by passing true. The
default behavior (false) is throw an exception on unknown track types.
* @throws Mhor\MediaInfo\Exception\UnknownTrackTypeException
* @return MediaInfoContainer
*/
public function getMediaInfoContainer()
public function getMediaInfoContainer($ignoreUnknownTrackTypes = false)
{
if ($this->parsedOutput === null) {
throw new \Exception('You must run `parse` before running `getMediaInfoContainer`');
}

$mediaInfoContainerBuilder = new MediaInfoContainerBuilder();
$mediaInfoContainerBuilder = new MediaInfoContainerBuilder($ignoreUnknownTrackTypes);
$mediaInfoContainerBuilder->setVersion($this->parsedOutput['@attributes']['version']);

foreach ($this->parsedOutput['File']['track'] as $trackType) {
$mediaInfoContainerBuilder->addTrackType($trackType['@attributes']['type'], $trackType);
try {
$mediaInfoContainerBuilder->addTrackType($trackType['@attributes']['type'], $trackType);
} catch (UnknownTrackTypeException $ex) {
if (!$ignoreUnknownTrackTypes) {
// rethrow exception
throw $ex;
}
// else ignore
}
}

return $mediaInfoContainerBuilder->build();
Expand Down
7 changes: 7 additions & 0 deletions src/Type/Other.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Mhor\MediaInfo\Type;

class Other extends AbstractType
{
}
7 changes: 6 additions & 1 deletion test/Builder/MediaInfoContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ public function testAddTrackType()
$mediaContainer = $mediaInfoContainerBuilder->build();
$subtitles = $mediaContainer->getSubtitles();
$this->assertEquals(0, count($subtitles[0]->get()));

$mediaInfoContainerBuilder->addTrackType(TypeFactory::OTHER, array());
$mediaContainer = $mediaInfoContainerBuilder->build();
$others = $mediaContainer->getOthers();
$this->assertEquals(0, count($others[0]->get()));
}

/**
* @expectedException \Exception
* @expectedException Mhor\MediaInfo\Exception\UnknownTrackTypeException
*/
public function testAddInvalidType()
{
Expand Down
23 changes: 23 additions & 0 deletions test/Parser/MediaInfoOutputParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class MediaInfoOutputParserTest extends \PHPUnit_Framework_TestCase
*/
private $outputPath;

private $invalidOutputPath;

public function setUp()
{
$this->outputPath = __DIR__.'/../fixtures/mediainfo-output.xml';
$this->invalidOutputPath = __DIR__.'/../fixtures/mediainfo-output-invalid-types.xml';
}

/**
Expand Down Expand Up @@ -48,4 +51,24 @@ public function testGetMediaInfoContainer()
$subtitles = $mediaInfoContainer->getSubtitles();
$this->assertEquals(16, count($subtitles[0]->get()));
}

public function testIgnoreInvalidTrackType()
{
$mediaInfoOutputParser = new MediaInfoOutputParser();
$mediaInfoOutputParser->parse(file_get_contents($this->invalidOutputPath));
// the xml specifically has an unknown type in it
// when passing true we want to ignore/skip unknown track types
$mediaInfoContainer = $mediaInfoOutputParser->getMediaInfoContainer(true);
}

/**
* @expectedException Mhor\MediaInfo\Exception\UnknownTrackTypeException
*/
public function testThrowInvalidTrackType()
{
$mediaInfoOutputParser = new MediaInfoOutputParser();
$mediaInfoOutputParser->parse(file_get_contents($this->invalidOutputPath));
// will throw exception here as default behavior
$mediaInfoContainer = $mediaInfoOutputParser->getMediaInfoContainer();
}
}
136 changes: 136 additions & 0 deletions test/fixtures/mediainfo-output-invalid-types.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?xml version="1.0" encoding="UTF-8"?>
<Mediainfo version="0.7.69">
<File>
<track type="General">
<Count>284</Count>
<Count_of_stream_of_this_kind>1</Count_of_stream_of_this_kind>
<Kind_of_stream>General</Kind_of_stream>
<Kind_of_stream>General</Kind_of_stream>
<Stream_identifier>0</Stream_identifier>
<Count_of_audio_streams>1</Count_of_audio_streams>
<Audio_Format_List>MPEG Audio</Audio_Format_List>
<Audio_Format_WithHint_List>MPEG Audio</Audio_Format_WithHint_List>
<Audio_codecs>MPEG-1 Audio layer 3</Audio_codecs>
<Complete_name>test.mp3</Complete_name>
<File_name>test.mp3</File_name>
<File_extension>mp3</File_extension>
<Format>MPEG Audio</Format>
<Format>MPEG Audio</Format>
<Format_Extensions_usually_used>m1a mpa1 mp1 m2a mpa2 mp2 mp3</Format_Extensions_usually_used>
<Commercial_name>MPEG Audio</Commercial_name>
<Internet_media_type>audio/mpeg</Internet_media_type>
<Codec>MPEG Audio</Codec>
<Codec>MPEG Audio</Codec>
<Codec_Extensions_usually_used>m1a mpa1 mp1 m2a mpa2 mp2 mp3</Codec_Extensions_usually_used>
<File_size>19316079</File_size>
<File_size>18.4 MiB</File_size>
<File_size>18 MiB</File_size>
<File_size>18 MiB</File_size>
<File_size>18.4 MiB</File_size>
<File_size>18.42 MiB</File_size>
<Duration>475193</Duration>
<Duration>7mn 55s</Duration>
<Duration>7mn 55s 193ms</Duration>
<Duration>7mn 55s</Duration>
<Duration>00:07:55.193</Duration>
<Overall_bit_rate_mode>CBR</Overall_bit_rate_mode>
<Overall_bit_rate_mode>Constant</Overall_bit_rate_mode>
<Overall_bit_rate>320000</Overall_bit_rate>
<Overall_bit_rate>320 Kbps</Overall_bit_rate>
<Stream_size>308340</Stream_size>
<Stream_size>301 KiB (2%)</Stream_size>
<Stream_size>301 KiB</Stream_size>
<Stream_size>301 KiB</Stream_size>
<Stream_size>301 KiB</Stream_size>
<Stream_size>301.1 KiB</Stream_size>
<Stream_size>301 KiB (2%)</Stream_size>
<Proportion_of_this_stream>0.01596</Proportion_of_this_stream>
<Title>Track Title</Title>
<Album>Album Title</Album>
<Album_Performer>Various Artists</Album_Performer>
<Track_name>Track Title</Track_name>
<Track_name_Position>2</Track_name_Position>
<Track_name_Total>2</Track_name_Total>
<Performer>Track Artist</Performer>
<Recorded_date>2014</Recorded_date>
<Cover_Data>sample_binary_cover</Cover_Data>
<File_last_modification_date>UTC 2014-12-06 16:43:30</File_last_modification_date>
<File_last_modification_date__local_>2014-12-06 17:43:30</File_last_modification_date__local_>
</track>

<track type="Audio">
<Count>222</Count>
<Count_of_stream_of_this_kind>1</Count_of_stream_of_this_kind>
<Kind_of_stream>Audio</Kind_of_stream>
<Kind_of_stream>Audio</Kind_of_stream>
<Stream_identifier>0</Stream_identifier>
<Format>MPEG Audio</Format>
<Commercial_name>MPEG Audio</Commercial_name>
<Format_version>Version 1</Format_version>
<Format_profile>Layer 3</Format_profile>
<Internet_media_type>audio/mpeg</Internet_media_type>
<Codec>MPA1L3</Codec>
<Codec>MPEG-1 Audio layer 3</Codec>
<Duration>475611</Duration>
<Duration>7mn 55s</Duration>
<Duration>7mn 55s 611ms</Duration>
<Duration>7mn 55s</Duration>
<Duration>00:07:55.611</Duration>
<Bit_rate_mode>CBR</Bit_rate_mode>
<Bit_rate_mode>Constant</Bit_rate_mode>
<Bit_rate>320000</Bit_rate>
<Bit_rate>320 Kbps</Bit_rate>
<Channel_s_>2</Channel_s_>
<Channel_s_>2 channels</Channel_s_>
<Sampling_rate>44100</Sampling_rate>
<Sampling_rate>44.1 KHz</Sampling_rate>
<Samples_count>20974464</Samples_count>
<Frame_count>18207</Frame_count>
<Compression_mode>Lossy</Compression_mode>
<Compression_mode>Lossy</Compression_mode>
<Stream_size>19007739</Stream_size>
<Stream_size>18.1 MiB (98%)</Stream_size>
<Stream_size>18 MiB</Stream_size>
<Stream_size>18 MiB</Stream_size>
<Stream_size>18.1 MiB</Stream_size>
<Stream_size>18.13 MiB</Stream_size>
<Stream_size>18.1 MiB (98%)</Stream_size>
<Proportion_of_this_stream>0.98404</Proportion_of_this_stream>
</track>
<track type="Text" streamid="7">
<Count>195</Count>
<Count_of_stream_of_this_kind>8</Count_of_stream_of_this_kind>
<Kind_of_stream>Text</Kind_of_stream>
<Kind_of_stream>Text</Kind_of_stream>
<Stream_identifier>6</Stream_identifier>
<Stream_identifier>7</Stream_identifier>
<StreamOrder>9</StreamOrder>
<ID>11</ID>
<ID>11</ID>
<Unique_ID>652628868</Unique_ID>
<Format>UTF-8</Format>
<Commercial_name>UTF-8</Commercial_name>
<Codec_ID>S_TEXT/UTF8</Codec_ID>
<Codec_ID_Info>UTF-8 Plain Text</Codec_ID_Info>
<Codec>S_TEXT/UTF8</Codec>
<Codec>UTF-8</Codec>
<Codec_Info>UTF-8 Plain Text</Codec_Info>
<Language>ja</Language>
<Language>Japanese</Language>
<Language>Japanese</Language>
<Language>ja</Language>
<Language>jpn</Language>
<Language>ja</Language>
<Default>No</Default>
<Default>No</Default>
<Forced>No</Forced>
<Forced>No</Forced>
</track>

<!-- not a real section, just for testing parsing purposes and throwing exceptions -->
<track type="RandomUnknownType" streamid="8">
<UnknownAttrib>1</UnknownAttrib>
<OtherUnknownAttribStr>test</OtherUnknownAttribStr>
</track>
</File>
</Mediainfo>