Thank you, I am new to this forum and if I could apologize in advance if this post is not in the correct location.

I have having trouble reading an external m3u file (playlist) into my php code, thought someone has done this before and could give me an idea or lined solution example of what my incomplete line should look like.

I do know that, M3U files are just plain text, with each line being either a comment (starts with a #) or a path to a file (doesn't start with a #). I still dont know how to load each line of the M3U file into an array, then loop over it and of course only use the lines that don't start with a "#".

The way things work like this...
You start a stream. This reads an M3U file with a list of songs.
The steam plays the songs in the order they are listed in the M3U file.

The question which i have somewhat resolved is I need to know what time every song will play during the next week assuming the stream is not restarted. If it is restarted then the entire week prediction needs to be recalculated.

The first problem was that the songs are not likely to have lengths which are perfect seconds, and the streamer may drop a bit of time between songs, so the longer the stream is up, the less accurate the prediction will be. This could be corrected by observing the current song and the current time and fixing up the calculation periodically. I am skipping this problem for now.

The main thing I need to do is, given a time T, show what song will be playing at time T + X. I believe this should be as simple as counting the number of seconds in a week and working my way through the playlist over and over for the week.

I have attached a sample script that you might be able to assist on. Right now It contains one function that generates a schedule given a time, a length and a playlist. There are also an example playlist with some made up song titles and lengths.

I will need to figure out how to convert the m3u playlist into an array like this
$playlist = array();
$playlist[] = array('title' => 'Junk Monster - MPG', 'length' => 180);
$playlist[] = array('title' => 'A Bell Tolls - MR', 'length' => 625);
$playlist[] = array('title' => 'Enter The Wagon - Soulless', 'length' => 76);
$playlist[] = array('title' => 'Conjoined - Twister', 'length' => 523);
$playlist[] = array('title' => 'Poutine - The Canucks', 'length' => 23);
$playlist[] = array('title' => 'Autobus - Exploradoras', 'length' => 758);
$playlist[] = array('title' => 'Rusty Shovel Medley - Ramshackle', 'length' => 254);
$playlist[] = array('title' => 'Purple Lung - Twin Mountain Match', 'length' => 876);
$playlist[] = array('title' => 'Cold Pizza, Hot Cola - Pacifica', 'length' => 234);
$playlist[] = array('title' => 'Nothing Works - MTL', 'length' => 68);

Thanking for you assistance in advance....


Full code below
<?php

function calculate_schedule($start_timestamp, $length_in_seconds, $playlist) {
$schedule = array();

$i = 0;
$song = 0;
while($i < $length_in_seconds) {
$schedule[] = array('timestamp' => $start_timestamp + $i, 'title' => $playlist[$song]['title'], 'length' => $playlist[$song]['length']);
$i += $playlist[$song]['length'];
$song = ($song + 1) % count($playlist);
}

return $schedule;
}

// I will need to figure out how to convert the m3u playlist into an array like this
$playlist = array();
$playlist[] = array('title' => 'Junk Monster - MPG', 'length' => 180);
$playlist[] = array('title' => 'A Bell Tolls - MR', 'length' => 625);
$playlist[] = array('title' => 'Enter The Wagon - Soulless', 'length' => 76);
$playlist[] = array('title' => 'Conjoined - Twister', 'length' => 523);
$playlist[] = array('title' => 'Poutine - The Canucks', 'length' => 23);
$playlist[] = array('title' => 'Autobus - Exploradoras', 'length' => 758);
$playlist[] = array('title' => 'Rusty Shovel Medley - Ramshackle', 'length' => 254);
$playlist[] = array('title' => 'Purple Lung - Twin Mountain Match', 'length' => 876);
$playlist[] = array('title' => 'Cold Pizza, Hot Cola - Pacifica', 'length' => 234);
$playlist[] = array('title' => 'Nothing Works - MTL', 'length' => 68);

// next we get the assume the start time of the stream is the current time
date_default_timezone_set('America/New_York');
$start = time();

// ask the function to calculate one week of programming
$seconds_per_week = 86400 * 7;

// get the schedule
$schedule = calculate_schedule($start, $seconds_per_week, $playlist);

// see http://php.net/date for formatting details
$display_format = 'Y-m-d H:i:s';

// display the schedule
foreach($schedule as $item) {
printf("%s %s (%d)<br />\n", date($display_format, $item['timestamp']), $item['title'], $item['length']);
}

?>

    I decided to just play around with this a bit, and came up with the following, using an example m3u file from Wikipedia. A bit crude, but seems to work:

    <?php
    
    // create some test data:
    $text = <<<EOD
    #EXTM3U
    #EXTINF:419,Alice in Chains - Rotten Apple
    Alice in Chains_Jar of Flies_01_Rotten Apple.mp3
    #EXTINF:260,Alice in Chains - Nutshell
    Alice in Chains_Jar of Flies_02_Nutshell.mp3
    #EXTINF:255,Alice in Chains - I Stay Away
    Alice in Chains_Jar of Flies_03_I Stay Away.mp3
    #EXTINF:256,Alice in Chains - No Excuses
    Alice in Chains_Jar of Flies_04_No Excuses.mp3
    #EXTINF:157,Alice in Chains - Whale And Wasp
    Alice in Chains_Jar of Flies_05_Whale And Wasp.mp3
    #EXTINF:245,Alice in Chains - Swing On This
    Alice in Chains_Jar of Flies_07_Swing On This.mp3
    EOD;
    
    file_put_contents('test.m3u', $text);
    
    // try reading/parsing it:
    $rawData = file('test.m3u', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $data = array();
    foreach($rawData as $line) {
      if(strpos(trim($line), '#EXTM3U') === 0) {
        continue;
      }
      if(strpos(trim($line), '#EXTINF') === 0) {
        preg_match('/#EXTINF:(\d+),(.*) - (.*)/', $line, $matches);
      }
      else {
        $data[] = array(
          'artist'       => $matches[2],
          'title'        => $matches[3],
          'time_seconds' => $matches[1],
          'file_path'    => trim($line)
        );
      }
    }
    
    print_r($data);
    
    // clean-up
    unlink('test.m3u');
    

    Output:

    Array
    (
        [0] => Array
            (
                [artist] => Alice in Chains
                [title] => Rotten Apple
                [time_seconds] => 419
                [file_path] => Alice in Chains_Jar of Flies_01_Rotten Apple.mp3
            )
    
    [1] => Array
        (
            [artist] => Alice in Chains
            [title] => Nutshell
            [time_seconds] => 260
            [file_path] => Alice in Chains_Jar of Flies_02_Nutshell.mp3
        )
    
    [2] => Array
        (
            [artist] => Alice in Chains
            [title] => I Stay Away
            [time_seconds] => 255
            [file_path] => Alice in Chains_Jar of Flies_03_I Stay Away.mp3
        )
    
    [3] => Array
        (
            [artist] => Alice in Chains
            [title] => No Excuses
            [time_seconds] => 256
            [file_path] => Alice in Chains_Jar of Flies_04_No Excuses.mp3
        )
    
    [4] => Array
        (
            [artist] => Alice in Chains
            [title] => Whale And Wasp
            [time_seconds] => 157
            [file_path] => Alice in Chains_Jar of Flies_05_Whale And Wasp.mp3
        )
    
    [5] => Array
        (
            [artist] => Alice in Chains
            [title] => Swing On This
            [time_seconds] => 245
            [file_path] => Alice in Chains_Jar of Flies_07_Swing On This.mp3
        )
    
    )
    
      8 days later

      Appreciate your help, based on what you provided and a little insight i was able to come up with this.
      http://mpg.dnset.com/radio/sch/mix.html

      And would like to share the development of this code as it will be some what custom.

      With some changes and output to html
      my next challenge will be to..and with some help from others who are willing to get invloved.

      Project-1. wrap into a tab like framework in css or otherwise separating the days as the schedule is to big right now an needs to be broken down.
      like embedded this sample code - http://http://mpg.dnset.com/radio/sch/tab.html

      Project-2. convert seconds into minutes playback times

      Project-3. excluding playlist.m3u from output generation as it interferes with the complete schedules timeline.

      Project-4. the time line seems to be a bit off with system clock but i'm not sure why yet, might be just the gap between songs...
      you can test that here by
      a). comparing the song in the stream played: http://mpg.dnset.com/radio/mix/index.php?#player?catid=0&trackid=0
      with
      b). the generated schedule : http://mpg.dnset.com/radio/sch/tab.html

      Also to note the timezone is set to america/newyork in servers config php.
      The $start = strtotime is set to the servers stream launch date. (Wed, 08 Feb 2017 14:17:58)
      You can also confirm that here: http://mpg.dnset.com/radio/bc/
      This the stream I am working with right now "Blended Music Mix" it is at the top of page.

      Here is the updated generator code now for those interested in this adventure. (sharing is fun)
      (also you can see the complete working project here in root folder: http://mpg.dnset.com/radio/sch/)

      <?php
      include 'mp3file.class.php';
      function calculate_schedule($start_timestamp, $length_in_seconds, $playlist) {
      $schedule = array();

      $i = 0;
      $song = 0;
      while($i < $length_in_seconds) {
      	$schedule[] = array('timestamp' => $start_timestamp + $i, 'title' => $playlist[$song]['title'], 'length' => $playlist[$song]['length']);
      	$i += $playlist[$song]['length'];
      	$song = ($song + 1) % count($playlist);
      }
      
      return $schedule;

      }ini_set('display_errors', 1);

      $playlist = array();
      $m3uArray = explode("\r\n", file_get_contents("E:\ezstream\Playlist\Playlist.m3u"));
      foreach($m3uArray as $entry) {
      $entry = trim($entry);
      if($entry != "") {
      $mp3file = new MP3File("E:\ezstream\Playlist\".$entry);//
      $length = $mp3file->getDuration();
      $playlist[] = array('title' => $entry, 'length' => $length);
      }

      }
      // you will need to figure out how to convert your m3u playlist into an array like this
      /$playlist = array();
      $playlist[] = array('title' => 'Junk Monster - MPG', 'length' => 180);
      $playlist[] = array('title' => 'A Bell Tolls - MR', 'length' => 625);
      $playlist[] = array('title' => 'Enter The Wagon - Soulless', 'length' => 76);
      $playlist[] = array('title' => 'Conjoined - Twister', 'length' => 523);
      $playlist[] = array('title' => 'Poutine - The Canucks', 'length' => 23);
      $playlist[] = array('title' => 'Autobus - Exploradoras', 'length' => 758);
      $playlist[] = array('title' => 'Rusty Shovel Medley - Ramshackle', 'length' => 254);
      $playlist[] = array('title' => 'Purple Lung - Twin Mountain Match', 'length' => 876);
      $playlist[] = array('title' => 'Cold Pizza, Hot Cola - Pacifica', 'length' => 234);
      $playlist[] = array('title' => 'Nothing Works - MTL', 'length' => 68);
      /

      // next we get the assume the start time of the stream is the current time
      date_default_timezone_set('America/New_York');
      $start = strtotime('Wed, 08 Feb 2017 14:17:58'); //time();
      // ask the function to calculate one week of programming
      $seconds_per_week = 86400 * 7;

      // get the schedule
      $schedule = calculate_schedule($start, $seconds_per_week, $playlist);

      // see http://php.net/date for formatting details
      //$display_format = 'D-F-d h:i A';
      $display_format = 'D h:i A';

      // display the schedule
      $output = "<table><tr><thead><th>Date</th><th>Song</th><th>Duration</th></tr></thead><tbody>\n";
      foreach($schedule as $item) {
      printf("%s %s (%d)<br />\n", date($display_format, $item['timestamp']), $item['title'], $item['length']);
      $output .= "<tr>\n"
      ." <td>".date($display_format, $item['timestamp'])."</td>\n"
      ." <td>".$item['title']."</td>\n"
      ." <td>".$item['length']."</td>\n"
      ."</tr>\n";
      //if(!isset($outputByDate[date('Y-m-d', $item['timestamp'])])) $outputByDate[date('Y-m-d', $item['timestamp'])] = "<table><tr><thead><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody>\n";
      $outputByDate[date('D-d', $item['timestamp'])] .= "<tr>\n"
      ." <td>".date($display_format, $item['timestamp'])."</td>\n"
      ." <td>".$item['title']."</td>\n"
      ." <td>".$item['length']."</td>\n"
      ."</tr>\n";
      }
      $output .="</tbody></table>\n";
      file_put_contents("mixschedule.html", $output);
      $output2 = '';
      $output2Headers = '';
      foreach($outputByDate as $date=>$rows) {
      $output2Headers .= "<th>".$date."</th>";
      $rows = "<table><tr><thead><th>Date</th><th>Song</th><th>Duration</th></tr></thead><tbody>\n".$rows."</tbody></table>\n";
      $output2 .= "<td valign=top>".$rows."</td>";
      }
      $output2 = "<table><tr><thead>".$output2Headers."</thead><tbody>".$output2;
      $output2 .="</tbody></table>\n";

      file_put_contents("mix.html", $output2);
      ?>

        Write a Reply...