This commit is contained in:
Brooke Vibber 2023-03-01 13:55:30 -08:00
parent 99c57170da
commit 5f942ee917

View file

@ -115,26 +115,26 @@ class MP4Box {
public $size; public $size;
public $type; public $type;
function __construct( MP4Reader $reader, $start, $size, $type ) { public function __construct( MP4Reader $reader, $start, $size, $type ) {
$this->reader = $reader; $this->reader = $reader;
$this->start = $start; $this->start = $start;
$this->size = $size; $this->size = $size;
$this->type = $type; $this->type = $type;
} }
private function pos() { public function pos() {
return $this->reader->pos(); return $this->reader->pos();
} }
private function end() { public function end() {
return $this->start + $this->size; return $this->start + $this->size;
} }
private function remaining() { public function remaining() {
return $this->end() - $this->pos(); return $this->end() - $this->pos();
} }
private function guard( $length ) { public function guard( $length ) {
$remaining = $this->remaining(); $remaining = $this->remaining();
if ( $remaining < $length ) { if ( $remaining < $length ) {
throw new Exception( "Reading beyond end of box; had $remaining bytes, wanted $length" ); throw new Exception( "Reading beyond end of box; had $remaining bytes, wanted $length" );
@ -199,16 +199,11 @@ function extractFragmentedMP4( $filename ) {
$mp4 = new MP4Reader( $filename ); $mp4 = new MP4Reader( $filename );
$eof = false; $eof = false;
$moof = false;
$init = false; $init = false;
while ( !$eof ) { while ( !$eof ) {
$eof = !$mp4->readBox( function ( $box ) use ( &$segments, &$init ) { $eof = !$mp4->readBox( function ( $box ) use ( &$segments, &$moof, &$init ) {
$bytes = $box->readBytes( $box->size - 8 );
$bytes = substr( $bytes, 0, 16 );
$hex = hexdump( $bytes );
$safe = safestr( $bytes );
print "box: {$box->type} at {$box->start}, {$box->size} bytes {$hex} {$safe}\n";
/* /*
Need to: Need to:
- find the end of the moov; everything up to that is the initialization segment - find the end of the moov; everything up to that is the initialization segment
@ -229,25 +224,34 @@ function extractFragmentedMP4( $filename ) {
opus has timescale 48000 in moov.trak.mdia.mdhd opus has timescale 48000 in moov.trak.mdia.mdhd
WARNING: opus is not dividing up fragments
*/ */
if ( $box->type === 'moof' ) { switch ( $box->type ) {
case 'ftyp':
break;
case 'moof':
if ( !$init ) { if ( !$init ) {
$init = [ $init = [
'start' => 0, 'start' => 0,
'size' => $box->start + $box->size, 'size' => $box->end(),
'timestamp' => 0.0,
'duration' => 0.0,
]; ];
$segments['init'] = $init; $segments['init'] = $init;
} }
$moof = $box->start;
break;
case 'mdat':
// @todo use timestamp and duration data
array_push( $segments, [ array_push( $segments, [
'start' => $box->start, 'start' => $moof,
'size' => $box->size, 'size' => $box->end() - $moof,
'timestamp' => 0.0, 'timestamp' => 0.0,
'duration' => 0.0, 'duration' => 0.0,
] ); ] );
break;
default:
// ignore
} }
return true; return true;
@ -261,4 +265,12 @@ $argv = $_SERVER['argv'];
$self = array_shift( $argv ); $self = array_shift( $argv );
$filename = array_shift( $argv ); $filename = array_shift( $argv );
$segments = extractFragmentedMP4( $filename ); $segments = extractFragmentedMP4( $filename );
var_dump( $segments ); //var_dump( $segments );
foreach ( $segments as $key => $segment ) {
if ( $key === 'init' ) {
print "$key {$segment['start']},{$segment['size']}\n";
} else {
print "$key {$segment['timestamp']},{$segment['duration']} @ {$segment['start']},{$segment['size']}\n";
}
}