PHP Classes

Unified PHP Playlist: Read playlists of formats asx, m3u, pls, xspf, etc

Recommend this page to a friend!
  Info   View files Example   View files View files (22)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 65%Total: 191 This week: 1All time: 8,573 This week: 560Up
Version License PHP version Categories
unified-playlist 1.0.3MIT/X Consortium ...5.3PHP 5, Files and Folders, Audio, Parsers
Description 

Author

This class can read playlists of formats aimppl, asx, xspf, zpl, m3u, pls, and upf.

It can take a given play list file and parse it to extract the metadata of the playlist it self and the tracks it contains.

For the playlist it extracts the title, the total track count, duration and file size.

For each track it extracts the URL, artist, song, album, genre, year, bitrate, sample rate, and duration.

Innovation Award
PHP Programming Innovation award nominee
April 2017
Number 7


Prize: PhpStorm IDE 1 year individual subscription
Playlists are lists of audio and video clips that can be watched in a sequence defined by the user. Playlists can be saved in files defined in many different formats.

This class can read playlists in many well known formats. It can extract many types of details of the listed media.

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: 57416 in Russian Federation Russian Federation
Week rank: 106 Up7 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 15x

Winner: 2x

Example

#!/usr/bin/php
<?php
// ? create-project installation : require installation
$vendor_dir = is_dir(dirname(dirname((__FILE__))).'/vendor') ? dirname(dirname((__FILE__))).'/vendor' : dirname(dirname(dirname(dirname(__FILE__))));
require_once
$vendor_dir.'/autoload.php';

use
wapmorgan\UnifiedPlaylist\UnifiedPlaylist;

if (!isset(
$argv[1])) die('Usage: '.basename(__FILE__).' <playlist>'.PHP_EOL);

$playlist = UnifiedPlaylist::open($argv[1]);

foreach (
$playlist as $track) {
   
var_dump($track);
   
// echo '+'.$track->year.PHP_EOL;
}
if (!empty(
$playlist->getTitle())) echo 'Title: '.$playlist->getTitle().PHP_EOL;
echo
'Total duration: '.$playlist->formatTotalDuration().PHP_EOL;
echo
'Total size: '.$playlist->getTotalSize().PHP_EOL;


Details

UnifiedPlaylist

Composer package Latest Stable Version Total Downloads Latest Unstable Version License Tests

A Unified Reader of playlist formats. Supports all popular playlist file formats and all their features.

  1. Features
  2. Installation
  3. Usage - Advanced usage
  4. Formats

Features

  • Support for all popular formats and their variants: aimppl (and aimppl4), asx, xspf, zpl, m3u (and m3u8), pls, upf.
  • Unified interface to all data
  • Easy work with track data: just treat the class as an array of Track objects (support for iteration over class)
  • Support for all additional track data (length, bitrate, file size, genre, release year and so on)
  • Support reading and writing of all formats (writing support is in progress)
  • White and black lists of formats

Installation

Install package via composer:

composer require wapmorgan/unified-playlist

Usage

Firsly, check that file looks like a playlist. Then, try to open it with open() static method. In case of success it will return an instance of UnifiedPlaylist with all needed information.

use wapmorgan\UnifiedPlaylist\UnifiedPlaylist;

if (UnifiedPlaylist::isPlaylist($tmpfile)) {
    $playlist = UnifiedPlaylist::open($tmpfile);
    /// ... operations here
}

Available operations:

  1. Reading a Playlist. If you work with an instance of UnifiedPlaylist like with an array, it will contain all information about tracks as `Track` objects. (For detailed information about all `Track` fields and methods see section below)

    if (UnifiedPlaylist::isPlaylist($tmpfile)) {
        $playlist = UnifiedPlaylist::open($tmpfile);
    
        foreach ($playlist as $track) {
            echo $track->artist . ' - ' . $track->song.PHP_EOL.' ('.$track->formatDuration().')';
        }
    }
    
    // Information about all playlist like title, duration of size
    echo 'Title: '.$playlist->getTitle().PHP_EOL;
    echo 'Total playlist duration: '.$playlist->formatTotalDuration().PHP_EOL;
    echo 'Total playlist size: '.$playlist->getTotalSize().PHP_EOL;
    
  2. Creating a playlist
    $playlist = new UnifiedPlaylist();
    $playlist[] = (new Track('filename.mp3'))->set('duration', 10)->set('artist', 'Abba')->set('song', 'Happy new year');
    $playlist->save('Playlist.m3u');
    
  3. Modifying a playlist
    $playlist = UnifiedPlaylist::open($tmpfile);
    foreach ($playlist as $i => $track) {
        $track->genre = 'Pop';
        $playlist[$i] = $track;
    }
    $playlist->save('Edited.m3u');
    

API

UnifiedPlaylist

Method | Description ----------------------------------------|---------------------------------------------------------------------------------------------- @isPlaylist($filename): boolean | Checks that file format is one of supporting @isAllowed($filename): boolean | Checks white and black lists for restrictions about specified format @open($filename): UnifiedPlaylist | Opens a playlist and returns a UnifiedPlaylist instance getTitle(): string | Returns title of playlist, if present getTotalTracks(): integer | Returns number of tracks in playlist getTotalDuration(): integer | Returns total duration of tracks in playlist getTotalSize(): integer | Returns total size of playlist, if present formatTotalDuration($format = 'auto'): string | Returns formatted duration of playlist. Format can be: m:s, h:m:s or h:m. If auto, the best format will be used. save($filename[, $format]): boolean | Saves the playlist

Track

All available properties (real information can be nulled due to format limitations or generator configuration):

Property | Description -----------------|---------- string $url | File path if file or an url if stream string $artist | Artist of track string $song | Track name string $album | Album of track string $genre | Genre of track int $year | Year of track int $bitrate | Track bitrate (in kb/s) int $samplerate| Track samplerate (in Hz) int $duration | Track duration (in seconds)

Method | Description -----------------------------------|---------------------------------------------------------------------------------------------- formatDuration($format = 'auto') | Formats tracks duration as m:s, h:m:s or h:m. If auto, the best format will be used. isStream() | Checks if track is a network stream

Advanced usage

White and black lists

You can limit the list of formats that will be supported by your service. To allow the use of certain formats, use the whitelist:

UnifiedPlaylist::$whiteList = array(UnifiedPlaylist::M3U, UnifiedPlaylist::PLS);

To prohibit the use of certain formats, use the blacklist. All other formats will be allowed to use:

UnifiedPlaylist::$blackList = array(UnifiedPlaylist::ASX);

Now, it's important to properly handle the situation when a user tries to open a playlist of a prohibited format. In this case, the isAllowed() method returns false:

use wapmorgan\UnifiedPlaylist\FormatRestrictionException;
use wapmorgan\UnifiedPlaylist\UnifiedPlaylist;

UnifiedPlaylist::$blackList = array(UnifiedPlaylist::ASX);

if (UnifiedPlaylist::isPlaylist($tmpfile)) {
    if (UnifiedPlaylist::isAllowed($tmpfile)) {
        $playlist = UnifiedPlaylist::open($tmpfile);

        /// ... import operations here

    } else {
        echo 'Sorry, but we don\'t support this format. Please, try another.'.PHP_EOL;
    }
}

Formats

| Format | aimppl | asx | xpl | xspf | zpl | m3u | pls | upf | |----------|--------|-----|-----|------|-----|-----|-----|-----| | Reading | + | + | - | + | + | + | + | + | | Writing | + | + | - | - | - | + | - | - |


  Files folder image Files  
File Role Description
Files folder imagebin (1 file)
Files folder imagesrc (6 files, 1 directory)
Files folder imagetests (1 file, 1 directory)
Plain text file .travis.yml Data Auxiliary data
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  /  bin  
File Role Description
  Plain text file playlist Example Example script

  Files folder image Files  /  src  
File Role Description
Files folder imageFormats (8 files)
  Plain text file ConfigurationException.php Class Class source
  Plain text file Exception.php Class Class source
  Plain text file FormatRestrictionException.php Class Class source
  Plain text file Track.php Class Class source
  Plain text file UnifiedPlaylist.php Class Class source
  Plain text file UnknownFormatException.php Class Class source

  Files folder image Files  /  src  /  Formats  
File Role Description
  Plain text file AdvancedStreamRedirector.php Class Class source
  Plain text file Aimppl.php Class Class source
  Plain text file BasicFormat.php Class Class source
  Plain text file M3u.php Class Class source
  Plain text file Pls.php Class Class source
  Plain text file UniversalPlaylist.php Class Class source
  Plain text file XmlShareablePlaylist.php Class Class source
  Plain text file ZunePlaylist.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagefixtures (1 file)
  Plain text file UnifiedPlaylistTest.php Class Class source

  Files folder image Files  /  tests  /  fixtures  
File Role Description
  Plain text file Playlist.m3u Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:191
This week:1
All time:8,573
This week:560Up
 User Ratings  
 
 All time
Utility:80%StarStarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:85%StarStarStarStarStar
Examples:80%StarStarStarStarStar
Tests:-
Videos:-
Overall:65%StarStarStarStar
Rank:640