PHP M3u8
M3u8 file parser / dumper
Installation
$ composer require 'chrisyue/php-m3u8'
Usage
Parser
$m3u8Content = <<<'M3U8'
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:6
#EXT-X-MEDIA-SEQUENCE:12
#EXTINF:5.000,title
stream12.ts
#EXTINF:4.000,
stream13.ts
#EXTINF:3.000,
stream14.ts
#EXTINF:6.000,
stream15.ts
M3U8;
$parser = new \Chrisyue\PhpM3u8\Parser();
$m3u8 = $parser->parse($m3u8Content);
or with loader
class MyLoader implements LoaderInterface
{
public function load($uri)
{
return file_get_contents('http://example.com/path/to/m3u8');
}
}
$parser->setLoader(new MyLoader());
$m3u8 = $parser->parseFromUri($uri);
now you can get information from $m3u8
$m3u8->getVersion();
$m3u8->getTargetDuration();
$m3u8->getDuration();
// get certain media segment inforatiom
$mediaSegment = $m3u8->getPlaylist()->offsetGet(0);
// or
$mediaSegment = $m3u8->getPlaylist()[0];
// get information from $mediaSegment
$mediaSegment->getDuration();
$mediaSegment->getSequence();
for more inforamation please check the M3u8
, PlayList
, MediaSegment
API under \Chrisyue\M3u8
.
Fortunately you don't really need to write a MyLoader
class because there is already a CachableLoader
along with this library
supposing you are using psr6 compatible cache utils like Symfony cache component:
$cachePool = new \Symfony\Component\Cache\Adapter\ApcuAdapter();
$loader = new \Chrisyue\PhpM3u8\CachableLoader($cachePool);
$parser->setLoader($loader);
$m3u8 = $parser->parseFromUri($uri);
Dumper
now you can try to dump the $m3u8
back into M3U8 text
$dumper = new \Chrisyue\PhpM3u8\Dumper();
echo $dumper->dump($m3u8);
To Contributors
Please follow the gitflow work flow to add a new feature, or fix bugs.