@@ -359,6 +359,7 @@ sub new {
359359 channels => undef ,
360360 channel_base_paths => {},
361361 channel_cookies => {}, # Store per-channel cookie jars
362+
362363 ua => undef ,
363364 json => JSON::XS-> new-> utf8-> canonical,
364365 };
@@ -746,9 +747,9 @@ sub get_playlist_url {
746747 $use_cache //= 1;
747748 $max_attempts //= 5;
748749
749- if ($use_cache && exists $self -> {playlists }-> {$channel_id }) {
750+ if ($use_cache && exists $self -> {playlists }-> {$channel_id }-> { ' url ' } ) {
750751 main::log_trace(" Using cached playlist for channel: $channel_id " );
751- return $self -> {playlists }-> {$channel_id };
752+ return $self -> {playlists }-> {$channel_id }-> { ' url ' } ;
752753 }
753754
754755 my $timestamp = sprintf (" %.0f" , time () * 1000);
@@ -819,7 +820,7 @@ sub get_playlist_url {
819820
820821 my $variant_url = $self -> get_playlist_variant_url($playlist_url , $channel_id );
821822 if ($variant_url ) {
822- $self -> {playlists }-> {$channel_id } = $variant_url ;
823+ $self -> {playlists }-> {$channel_id }-> { ' url ' } = $variant_url ;
823824 main::log_debug(" Cached playlist URL for channel: $channel_id " );
824825 return $variant_url ;
825826 }
@@ -980,14 +981,33 @@ sub get_playlist {
980981 main::log_trace(" Processing playlist - Base URL: $base_url " );
981982 main::log_trace(" Processing playlist - Base path: $base_path " );
982983 main::log_trace(" Stored base path for channel $channel_id : $base_path " );
983-
984-
985-
986- my @lines = split /\n/, $content ;
987-
988984
985+ # Remove the last 3 segments from the playlist if this is the first time weve seen it
986+ # This will make ffmpeg cache a bit more without needing to use command lines options
987+ if ( not exists $self -> {playlists }-> {$channel_id }-> {' First' } or $self -> {playlists }-> {$channel_id }-> {' First' } != 1 ) {
988+ my @lines = split /\n/, $content ;
989+ # Find all lines ending with ".aac"
990+ my @aac_lines ;
991+ for my $i (0 .. $#lines ) {
992+ if ($lines [$i ] =~ / \. aac/ ) {
993+ push @aac_lines , $i ;
994+ }
995+ }
989996
990- return join (" \n " , @lines );
997+ # Stop output 4 lines BEFORE the last ".aac" line
998+ my @removed_lines ;
999+ if (@aac_lines >= 3) {
1000+ # 4th from the end of .aac lines
1001+ my $cutoff_line = $aac_lines [-4];
1002+ @removed_lines = @lines [($cutoff_line + 1) .. $#lines ]; # Get the lines being removed
1003+ @lines = @lines [0 .. $cutoff_line ]; # Keep only the lines up to that cutoff
1004+ }
1005+ my $rlines = join (" \n " , @removed_lines );
1006+ main::log_trace(" First Playlist, Removed $rlines " );
1007+ $self -> {playlists }-> {$channel_id }-> {' First' } = 1;
1008+ $content = join (" \n " , @lines );
1009+ }
1010+ return $content ;
9911011}
9921012
9931013sub select_quality_variant {
0 commit comments