perl调用curl下载m3u8
1 use strict; use warnings; 2 #格式: prefix,file1,file2... 3 my $fn = $ARGV[0]; 4 print "File=$fn\n"; 5 6 sub slurp{ 7 # 一次性读取文件, 参数是文件名 8 open(my $f, '<', shift); my $c = ''; while (<$f>){ $c = $c.$_; } close($f); return $c; 9 } 10 11 my $doc = slurp($fn); 12 my @tns = split(/,/, $doc); 13 my $prefix = shift(@tns); 14 my @names = (); 15 # 检查是否有后缀名 16 if (index($tns[0], '.ts') == -1){ 17 print "auto edit url\n"; 18 foreach my $tn (@tns){ 19 $tn =~ s/\&/A/g; 20 $tn =~ s/\?/B/g; 21 $tn =~ s/\=/C/g; 22 $tn = $tn.'.ts'; 23 if (-e $tn) {} else { push(@names, $tn); } 24 } 25 } else { 26 foreach my $tn (@tns) { 27 if (-e $tn) {} else { push(@names, $tn); } 28 } 29 } 30 31 my $tm = time; 32 33 my @errs = (); 34 my $app = "curl.exe -L -k -A \"Mozilla/5.0 (Windows NT 6.1; WOW64) Chrome/72.0.3626.96\" "; 35 print "[0|0]\n"; 36 for (my $j = 0; $j < scalar(@names); $j++){ 37 my $name = $names[$j]; 38 if (-e $name.".err"){ 39 unlink($name.".err"); 40 unlink($name); 41 } 42 my $cmd = $app." --stderr ".$name.".err -o ".$name.' "'.$prefix.'/'.$name.'"'; 43 system($cmd); 44 45 if (-e $name.".err"){ 46 my $err = slurp($name.".err"); 47 if (index($err, 'curl:') > -1){ 48 #curl 下载错误 49 push(@errs, $name); 50 51 if (-e $name){ 52 unlink($name); 53 } 54 } 55 unlink($name.".err"); 56 57 } 58 if (time - $tm >= 150){ 59 print '['.$j.'|'.scalar(@errs)."]\n"; 60 $tm = time; 61 } 62 63 } 64 65 print "\n"; 66 if (scalar(@errs) > 0){ 67 print "Error:".join(',', @errs)."\n"; 68 } 69 print 'FINISH';