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 $type;
function __construct( MP4Reader $reader, $start, $size, $type ) {
public function __construct( MP4Reader $reader, $start, $size, $type ) {
$this->reader = $reader;
$this->start = $start;
$this->size = $size;
$this->type = $type;
}
private function pos() {
public function pos() {
return $this->reader->pos();
}
private function end() {
public function end() {
return $this->start + $this->size;
}
private function remaining() {
public function remaining() {
return $this->end() - $this->pos();
}
private function guard( $length ) {
public function guard( $length ) {
$remaining = $this->remaining();
if ( $remaining < $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 );
$eof = false;
$moof = false;
$init = false;
while ( !$eof ) {
$eof = !$mp4->readBox( function ( $box ) use ( &$segments, &$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";
$eof = !$mp4->readBox( function ( $box ) use ( &$segments, &$moof, &$init ) {
/*
Need to:
- 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
WARNING: opus is not dividing up fragments
*/
if ( $box->type === 'moof' ) {
if ( !$init ) {
$init = [
'start' => 0,
'size' => $box->start + $box->size,
];
$segments['init'] = $init;
}
array_push( $segments, [
'start' => $box->start,
'size' => $box->size,
'timestamp' => 0.0,
'duration' => 0.0,
] );
switch ( $box->type ) {
case 'ftyp':
break;
case 'moof':
if ( !$init ) {
$init = [
'start' => 0,
'size' => $box->end(),
'timestamp' => 0.0,
'duration' => 0.0,
];
$segments['init'] = $init;
}
$moof = $box->start;
break;
case 'mdat':
// @todo use timestamp and duration data
array_push( $segments, [
'start' => $moof,
'size' => $box->end() - $moof,
'timestamp' => 0.0,
'duration' => 0.0,
] );
break;
default:
// ignore
}
return true;
@ -261,4 +265,12 @@ $argv = $_SERVER['argv'];
$self = array_shift( $argv );
$filename = array_shift( $argv );
$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";
}
}