This commit is contained in:
Brooke Vibber 2023-03-02 16:04:38 -08:00
parent 2b670f30fd
commit e8177c3d2d

View file

@ -377,15 +377,11 @@ function extractFragmentedMP4( $filename ) {
$max_pts = max( $max_pts, $pts + $sample_duration ); $max_pts = max( $max_pts, $pts + $sample_duration );
$dts += $sample_duration; $dts += $sample_duration;
} }
echo "$first_pts - $max_pts\n";
} }
], ],
] ); ] );
}, },
'mdat' => function ( $box ) use ( &$segments, &$moof, &$first_pts, &$max_pts, &$timescale ) { 'mdat' => function ( $box ) use ( &$segments, &$moof, &$first_pts, &$max_pts, &$timescale ) {
var_dump( $first_pts );
var_dump( $max_pts );
var_dump( $timescale );
array_push( $segments, [ array_push( $segments, [
'start' => $moof, 'start' => $moof,
'size' => $box->end() - $moof, 'size' => $box->end() - $moof,
@ -398,6 +394,53 @@ function extractFragmentedMP4( $filename ) {
return $segments; return $segments;
} }
function consolidate( $target, $segments ) {
$out = [
'init' => $segments['init'],
];
$n = count( $segments ) - 1;
$start = $segments[0]['start'];
$size = $segments[0]['size'];
$timestamp = $segments[0]['timestamp'];
$duration = $segments[0]['duration'];
$i = 1;
while ( $i < $n ) {
// Append segments until we get close
while ( $i < $n && $duration < $target ) {
$total = $duration + $segments[$i]['duration'];
if ( $total > $target ) {
$after = $total - $target;
$before = $target - $duration;
if ( $before < $after ) {
// Break segment early
break;
}
}
$duration += $segments[$i]['duration'];
$size += $segments[$i]['size'];
$i++;
}
// Save out a segment
$out[] = [
'start' => $start,
'size' => $size,
'timestamp' => $timestamp,
'duration' => $duration,
];
if ( $i < $n ) {
$segment = $segments[$i];
$start = $segment['start'];
$size = $segment['size'];
$timestamp = $segment['timestamp'];
$duration = $segment['duration'];
$i++;
}
}
return $out;
}
function playlist( $filename, $segments ) { function playlist( $filename, $segments ) {
/* /*
#EXTM3U #EXTM3U
@ -433,7 +476,6 @@ function playlist( $filename, $segments ) {
$init = $segments['init'] ?? false; $init = $segments['init'] ?? false;
if ( $init ) { if ( $init ) {
$lines[] = "#EXT-X-MAP:URI=\"$filename\",BYTERANGE=\"{$init['size']}@{$init['start']}\""; $lines[] = "#EXT-X-MAP:URI=\"$filename\",BYTERANGE=\"{$init['size']}@{$init['start']}\"";
$lines[] = $filename;
} }
$n = count( $segments ) - 1; $n = count( $segments ) - 1;
@ -451,7 +493,7 @@ $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 ); $segments = consolidate( 10, $segments );
/* /*
foreach ( $segments as $key => $segment ) { foreach ( $segments as $key => $segment ) {