retool mp3 id3 stuff for single-file

This commit is contained in:
Brooke Vibber 2021-11-03 10:43:29 -07:00
parent 2e3e1a3960
commit a105f49742
3 changed files with 31 additions and 16 deletions

View file

@ -2,7 +2,7 @@
# Define the audio tracks for Opus and MP3
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="opus",NAME="English",LANGUAGE="en-US",AUTOSELECT=YES,DEFAULT=YES,CHANNELS="2",URI="caminandes-llamigos.webm.audio.opus.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="mp3",NAME="English",LANGUAGE="en-US",AUTOSELECT=YES,DEFAULT=YES,CHANNELS="2",URI="caminandes-llamigos.webm.audio.mp3.m3u8"
#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="mp3",NAME="English",LANGUAGE="en-US",AUTOSELECT=YES,DEFAULT=YES,CHANNELS="2",URI="caminandes-llamigos.webm.audio.mp3.combined.m3u8"
# Opus-based preferred
#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080,FRAME-RATE=24.0,CODECS="vp09.00.10.08,opus",AUDIO="opus"
@ -15,8 +15,6 @@ caminandes-llamigos.webm.480p.vp9.fast.m3u8
caminandes-llamigos.webm.360p.vp9.fast.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=250000,RESOLUTION=426x240,FRAME-RATE=24.0,CODECS="vp09.00.10.08,opus",AUDIO="opus"
caminandes-llamigos.webm.240p.vp9.fast.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=96000,CODECS="opus",AUDIO="opus"
caminandes-llamigos.webm.audio.opus.m3u8
# MP3 for Safari
#EXT-X-STREAM-INF:BANDWIDTH=5000000,RESOLUTION=1920x1080,FRAME-RATE=24.0,CODECS="vp09.00.10.08,mp4a.40.34",AUDIO="mp3"
@ -29,5 +27,3 @@ caminandes-llamigos.webm.480p.vp9.fast.m3u8
caminandes-llamigos.webm.360p.vp9.fast.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=275000,RESOLUTION=426x240,FRAME-RATE=24.0,CODECS="vp09.00.10.08,mp4a.40.34",AUDIO="mp3"
caminandes-llamigos.webm.240p.vp9.fast.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=128000,CODECS="mp4a.40.34",AUDIO="mp3"
caminandes-llamigos.webm.audio.mp3.m3u8

View file

@ -100,21 +100,24 @@ function process_mp3( $filename, $pts ) {
print "$filename $pts $hex\n";
$data = file_get_contents( $filename );
if ( substr( $data, 0, 3 ) == 'ID3' ) {
echo "SKIPPING already has ID3\n";
} else {
echo "ADDING ID3\n";
file_put_contents( $filename, "$tag$data" );
}
return "$tag$data";
}
$playlist = "caminandes-llamigos.webm.audio.mp3.m3u8";
$playlist_out = "caminandes-llamigos.webm.audio.mp3.combined.m3u8";
$outfile = "caminandes-llamigos.webm.audio.mp3";
$lines = file( $playlist, FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES );
$pts = 0.0;
$duration = 0.0;
$lines_out = [];
$chunks = [];
$offset = 0;
foreach ( $lines as $line ) {
// todo: create a single-file version
// and rewrite the manifest
/*
#EXTM3U
#EXT-X-VERSION:3
@ -126,16 +129,29 @@ foreach ( $lines as $line ) {
#EXTINF:10.004898,
caminandes-llamigos.webm.audio.mp3.0001.mp3
...
for output:
#EXT-X-BYTERANGE:132872@730
*/
$matches = null;
if ( preg_match( '/^#EXTINF:\s*(\d+(?:\.\d+)?),/', $line, $matches ) ) {
$duration = floatval( $matches[1] );
continue;
} else if (preg_match( '/^#/', $line ) ) {
}
if (preg_match( '/^#/', $line ) ) {
$lines_out[] = $line;
continue;
}
$filename = $line;
process_mp3( $filename, $pts );
$chunk = process_mp3( $filename, $pts );
$len = strlen( $chunk );
$lines_out[] = "#EXT-X-BYTERANGE:$len@$offset";
$lines_out[] = "$outfile";
$chunks[] = $chunk;
$offset += $len;
$pts += $duration;
$duration = 0;
}
file_put_contents( $outfile, implode( '', $chunks ) );
file_put_contents( $playlist_out, implode( "\n", $lines_out ) );

View file

@ -209,12 +209,13 @@ class Transcoder {
private function ffmpeg( $options, $outfile, $container ) {
$playlist = "$outfile.m3u8";
$init = "$outfile.init.$container";
$segment = "$outfile.%04d.$container";
if ( $container == 'mp4' ) {
// HLS muxer seems to give the right options for fMP4
$segment = "$outfile.$container";
$segmentOptions = [
'-f', 'hls',
'-hls_segment_type', 'fmp4',
'-hls_flags', 'single_file',
'-hls_time', '10',
'-hls_playlist_type', 'vod',
'-hls_fmp4_init_filename', $init,
@ -223,7 +224,9 @@ class Transcoder {
];
} elseif ( $container == 'mp3' ) {
// For MP3, segment it raw.
// We'll need to postprocess to add an ID3 tag with timestamp.
// We'll need to postprocess to add an ID3 tag with timestamp
// and to reassemble into a file with byte ranges.
$segment = "$outfile.%04d.$container";
$segmentOptions = [
'-f', 'segment',
'-segment_format_options', 'id3v2_version=0:write_xing=0:write_id3v1=0',