ok got mp3 working

This commit is contained in:
Brooke Vibber 2023-03-07 15:17:30 -08:00
parent 6f9d18670d
commit cd2dfcd91e

View file

@ -527,14 +527,10 @@ class MP3FrameHeader {
if ( $this->sync ) { if ( $this->sync ) {
if ( $this->bitrate == 0 ) { if ( $this->bitrate == 0 ) {
$this->valid = false; $this->valid = false;
var_dump( $this );
echo "br: $br\n";
throw new Exception( "Invalid bitrate" ); throw new Exception( "Invalid bitrate" );
} }
if ( $this->sampleRate == 1 ) { if ( $this->sampleRate == 1 ) {
$this->valid = false; $this->valid = false;
var_dump( $this );
echo "sr: $sr\n";
throw new Exception( "Invalid sample rate" ); throw new Exception( "Invalid sample rate" );
} }
$this->valid = true; $this->valid = true;
@ -618,7 +614,6 @@ class MP3Reader {
$hex = hexdump( $bytes ); $hex = hexdump( $bytes );
$safe = safestr ( $bytes ); $safe = safestr ( $bytes );
var_dump($segments);
throw new Exception("Invalid packet at $start? $hex $safe"); throw new Exception("Invalid packet at $start? $hex $safe");
// back up and try again // back up and try again
@ -639,13 +634,11 @@ function extractMP3( $filename ) {
while ( true ) { while ( true ) {
$start = $reader->pos(); $start = $reader->pos();
$segment = $reader->readSegment(); $segment = $reader->readSegment();
var_dump($segment);
if ( !$segment ) { if ( !$segment ) {
break; break;
} }
$segments[] = $segment; $segments[] = $segment;
} }
var_dump($segments);
return $segments; return $segments;
} finally { } finally {
fclose( $file ); fclose( $file );
@ -661,19 +654,26 @@ function consolidate( $target, $segments ) {
return $segments; return $segments;
} }
$n = count( $segments ) - 1; $n = count( $segments );
if ( isset( $segments['init'] ) ) {
$n--;
}
$start = $segments[0]['start']; $start = $segments[0]['start'];
$size = $segments[0]['size']; $size = $segments[0]['size'];
$timestamp = $segments[0]['timestamp']; $timestamp = $segments[0]['timestamp'];
$duration = $segments[0]['duration']; $duration = $segments[0]['duration'];
$nextTarget = $timestamp + $target;
$nextDuration = $target;
$i = 1; $i = 1;
while ( $i < $n ) { while ( $i < $n - 1 ) {
// Append segments until we get close // Append segments until we get close
while ( $i < $n && $duration < $target ) { while ( $i < $n - 1 && $duration < $nextDuration ) {
$total = $duration + $segments[$i]['duration']; $total = $duration + $segments[$i]['duration'];
if ( $total > $target ) { if ( $total >= $nextDuration ) {
$after = $total - $target; $after = $total - $nextDuration;
$before = $target - $duration; $before = $nextDuration - $duration;
echo "$before $after\n";
echo "($total $nextDuration)\n";
if ( $before < $after ) { if ( $before < $after ) {
// Break segment early // Break segment early
break; break;
@ -691,6 +691,9 @@ function consolidate( $target, $segments ) {
'timestamp' => $timestamp, 'timestamp' => $timestamp,
'duration' => $duration, 'duration' => $duration,
]; ];
$nextTarget += $target;
$nextDuration = $nextTarget - $timestamp - $duration;
echo "[$nextTarget, $nextDuration]\n";
if ( $i < $n ) { if ( $i < $n ) {
$segment = $segments[$i]; $segment = $segments[$i];
@ -701,6 +704,12 @@ function consolidate( $target, $segments ) {
$i++; $i++;
} }
} }
$out[] = [
'start' => $start,
'size' => $size,
'timestamp' => $timestamp,
'duration' => $duration,
];
return $out; return $out;
} }