/usr/share/perl5/FlashVideo/HLSXDownloader.pm is in get-flash-videos 1.25.98-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | # Part of get-flash-videos. See get_flash_videos for copyright.
package FlashVideo::HLSXDownloader;
use strict;
use warnings;
use base 'FlashVideo::Downloader';
use FlashVideo::Utils;
use FlashVideo::JSON;
use FlashVideo::Mechanize;
use Term::ProgressBar;
use Crypt::Rijndael;
my $bitrate_index = {
high => 0,
medium => 1,
low => 2
};
sub cleanup_audio {
my ($in_file, $out_file) = @_;
my @args = {};
# Look for executable (ffmpeg or avconv)
if (!is_program_on_path("ffmpeg")) {
if (!is_program_on_path("avconv")) {
die "Could not find ffmpeg nor avconv executable!";
} else {
@args = (
"avconv",
"-i", $in_file,
"-bsf:a", "aac_adtstoasc",
"-c", "copy",
"-f", "mp4",
$out_file
);
}
} else {
@args = (
"ffmpeg",
"-i", $in_file,
"-absf", "aac_adtstoasc",
"-c", "copy",
"-f", "mp4",
$out_file
);
}
# Execute command
if (system(@args) != 0) {
die "Calling @args failed: $?";
}
return 1;
}
sub m3u8_attributes {
my $a = shift;
my $info = shift;
while ($a =~ m/([A-Z0-9-]+)=(\"[^\"]+\"|[^\",]+)(?:,|$)/g) {
my $key = $1;
my $val = $2;
$val =~ s/^\"(.*)\"$/$1/;
$info->{$key} = $val;
}
}
sub read_hls_playlist {
my($browser, $url) = @_;
$browser->get($url);
if (!$browser->success) {
die "Couldn't download m3u file, $url: " . $browser->response->status_line;
}
debug $browser->content;
debug $browser->cookie_jar->as_string();
my @lines = split(/\r?\n/, $browser->content);
my %urltable = ();
my $i = 0;
# Fill the url table
foreach my $line (@lines) {
if ($line =~ /EXT-X-STREAM-INF/ && $line =~ /BANDWIDTH/) {
$line =~ /BANDWIDTH=([0-9]*)/;
$urltable{int($1)} = { url => $lines[$i + 1],
inf => $line};
}
$i++;
}
return %urltable;
}
sub download {
my ($self, $args, $file, $browser) = @_;
my $hls_url = $args->{args}->{hls_url};
my $prefs = $args->{args}->{prefs};
$browser->cookie_jar( {} );
$browser->add_header( Referer => undef);
$browser->get($hls_url);
my %urls = read_hls_playlist($browser, $hls_url);
# Sort the urls and select the suitable one based upon quality preference
my @bandwidths = sort {int($a) <=> int($b)} keys %urls;
my $cnt = $#bandwidths;
my $key = $prefs->{quality};
$key //= 'high';
if ($key =~ /^\s*\d+\s*$/) {
my $bandwidth = $bandwidths[0];
foreach (@bandwidths) {
if ($key >= $_/1000.00) {
$bandwidth = $_;
}
}
$key = $bandwidth;
} else {
my $num = {high => int($cnt), medium => int(($cnt+1)/2), low => 0}->{$key};
$num //= int($cnt);
$key = $bandwidths[$num];
}
my ($hls_base, $trail) = ($hls_url =~ m/(.*\/)(.*)\.m3u8/);
my $filename_mp4 = $args->{flv};
my $filename_ts = $args->{flv} . ".ts";
my $filename_ts_segment = $args->{flv} . ".tsx";
debug "Select URL ".$urls{$key}->{url};
debug "STREAM INF ".$urls{$key}->{inf};
my $video_url = $urls{$key}->{url} =~ m/http(s?):\/\// ? $urls{$key}->{url} : $hls_base.$urls{$key}->{url};
$browser->add_header( Referer => undef);
$browser->get($video_url);
if (! $browser->success) {
die "Unable to read segments" . $browser->response->status_line;
}
my @lines = split(/\r?\n/, $browser->content);
my @segments = ();
# Fill the url table
my $hls_key;
my $decrypt;
foreach my $line (@lines) {
if ($line !~ /#/) {
# push non-blank lines
push @segments, $line if $line !~ /^\s*$/;
}
}
my $i = 1;
my $num_segs = @segments;
info "Downloading $num_segs segments";
my $progress_bar = Term::ProgressBar->new($num_segs);
open(my $fh_app, '>', $filename_ts) or die "Could not open file $filename_ts";
binmode($fh_app);
my $buffer;
my $media_sequence = 0;
my %decrypt_info = ( 'METHOD', 'NONE');
my %byte_range = ();
my $segment_index = 0;
foreach my $line (@lines) {
# skip empty lines
if ($line !~ /^\s*$/) {
if ( $line !~ /#/) {
# segment line
$segment_index += 1;
# to do skip if restarted
# and havent reaach last segment added yet.
my $url = $line;
if ($line !~ m%https?://%) {
# to do add manifest url to front to form url
$url = $hls_base.$line;
}
if (%byte_range) {
$browser->add_header('Range' => 'bytes='.$byte_range{'start'}.'-'. $byte_range{'end'});
} else {
$browser->delete_header('Range');
}
# Download and save each segment in a re-used segment file.
# Otherwise, the process memory expands monotonically. Large downloads would use up
# all memory and kill the process.
$browser->get($url, ":content_file" => $filename_ts_segment);
if (! $browser->success()) {
die "filed to download segment - aborting";
}
# Open the segment and append it to the TS file.
open(SEG, '<', $filename_ts_segment) or die "Could not open file $filename_ts_segment";
binmode(SEG);
my $crypt;
if ($decrypt_info{'METHOD'} eq 'AES-128') {
my $iv;
if (defined $decrypt_info{'IV'}) {
$iv =$decrypt_info{'IV'}
} else {
# must be packed correctly may need changing
# if no 64bit support in perl.
$iv = pack('x8q>', $media_sequence);
}
if (! defined $decrypt_info{'KEY'}) {
if (defined $decrypt_info{'URI'}) {
$browser->get($decrypt_info{'URI'});
my $hls_key = $browser->content;
# pad key if needed.
my $len = length($hls_key);
if ($len < 16) {
$hls_key = "\0" x (16 - $len) . $hls_key;
}
$decrypt_info{'KEY'} = $hls_key;
}
}
$crypt = Crypt::Rijndael->new($decrypt_info{'KEY'}, Crypt::Rijndael::MODE_CBC() );
$crypt->set_iv($iv);
my $size = ( stat SEG)[7];
# 16384 previously had issue with decrypt
# decrypt whole segment in one go
while (read(SEG, $buffer, $size)) {
print $fh_app $crypt->decrypt($buffer);
}
} else {
while (read(SEG, $buffer, 16384)) {
print $fh_app $buffer;
}
}
close(SEG);
$progress_bar->update($i);
$i++;
$media_sequence++;
} else {
# line begins with #
if ($line =~ /#EXT-X-KEY:/) {
my %m3u8_info;
m3u8_attributes($line, \%m3u8_info);
$decrypt_info{'METHOD'} = $m3u8_info{'METHOD'};
$decrypt_info{'KEY'} = $m3u8_info{'KEY'};
$decrypt_info{'IV'} = $m3u8_info{'IV'};
$decrypt_info{'URI'} = $m3u8_info{'URI'};
# info "Method ".$decrypt_info{'METHOD'} if defined $decrypt_info{'METHOD'};
# info "Key ".$decrypt_info{'KEY'} if defined $decrypt_info{'KEY'};
# info "IV ".$decrypt_info{'IV'} if defined $decrypt_info{'IV'};
# info "URI ".$decrypt_info{'URI'} if defined $decrypt_info{'URI'};
} elsif ($line =~ /#EXT-X-MEDIA-SEQUENCE/) {
my $cmd;
($cmd, $media_sequence) = split(/:/, $line);
debug "Media sequence = $media_sequence";
} elsif ($line =~ /#EXT-X-BYTERANGE/) {
my ($cmd, $range) = split(/:/, $line);
if ($range =~ /@/) {
my ($seg_len, $start) = split(/@/, $range);
$byte_range{'start'} = $start;
$byte_range{'end'} = $start + $seg_len;
} else {
$byte_range{'start'} = $byte_range{'end'};
$byte_range{'end'} += $range;
}
# info "Byte Range : ".$byte_range{'start'}." to ".$byte_range{'end'};
} elsif ($line =~ /#EXTINF/) {
my ($cmd, $dt) = split(/:/, $line);
my ($dur, $stitle) = split(/,/, $dt);
# info "Seg duration $dur title $stitle";
} else {
# info "Ignored line : $line";
}
}
}
}
# Remove the segment file as it is no longer needed.
unlink $filename_ts_segment;
close($fh_app);
if ($prefs->{raw}) {
$self->{printable_filename} = $filename_ts;
return -s $filename_ts;
} else {
cleanup_audio($filename_ts, $filename_mp4);
$self->{printable_filename} = $filename_mp4;
unlink $filename_ts;
return -s $filename_mp4;
}
}
1;
|