Ruby m3u8 边下边播

crella95 · 2023年03月31日 · 373 次阅读

使用 curl mplayer 和 ffmpeg 的 perl 代码

use strict; use warnings;
use HTTP::Tiny;

my $mpath = '/home/crella/elf/mplayer';
my $fpath = '/home/crella/elf/ffmpeg';

my $rf = $ARGV[0];
open(my $fc, '<', $rf);
my $ct = undef;

my $flink = undef;
my $yi = 0;
my @fns = ();
my $ck = '';
while (<$fc>){
    $ct = $_; chomp($ct);
    $ck = substr($ct, 0, 2);
    if ( $ck eq '#E') {}
    elsif ($ck eq '#p') {
    # prefix link
        $flink = substr($ct, 2);
    } else {

        push(@fns, $ct);

    }
}
close($fc);
if (defined($flink)){} else{
    print "prefix link not defined\n";
    exit(1);
}

sub binrd {
    my $fn = shift;
    open(my $fd, '<', $fn);
    binmode $fd;
    local $/ = undef;
    my $bc = <$fd>;
    close($fd);
    return $bc;

}

sub read_tags {
    open(my $wf, '<ft.txt');
    my $tag = shift;
    while (<$wf>){
        if (index($_, $tag) > -1){
            close($wf);
            return 1;       
        }
    }
    close($wf);

    return 0 ;

}
sub playts {
    my $j = shift; 

    my $rtag = -1;


    while ($j < scalar(@fns)){

        $rtag = read_tags($fns[$j]);
        #print $fns[$i].'   '.$rtag."\n";
        if ( $rtag == 1){

            system($mpath.' '.$fns[$j]);

            $j++;
        } else {

            sleep(1);
        }
    }


}

sub binwt {
    my $fn = shift; my $bc2 = shift;
    open(my $fd, '>', $fn);
    binmode $fd;
    print $fd $bc2;
    close($fd);
}

sub download {
    my $j = shift; 
    my @warnc = ();
    my @wtags = ();
    while ($j < scalar(@fns)){
        if (-e 'stop') { print 'STOP flag'."\n"; last;}
        my $rt = read_tags($fns[$j]);
        if ($rt == 0) {
            my $hresp = HTTP::Tiny->new->get($flink.$fns[$j]);
            if ($hresp->{success}){
                my $bc = $hresp->{content};
                binwt($fns[$j], $bc);
                push(@wtags, $fns[$j]);

            } else {
                push(@warnc, $fns[$j]);

            }

        }
        $j++;
    }
    if (scalar(@warnc) > 0){
        print 'file download error'."\n";
        foreach my $w (@warnc) {
            print $w."\n";
        }
    }
    open(my $fd3, '>>ft.txt');
    foreach my $ww (@wtags){
        print $fd3 $ww."\n";
    }
    close($fd3);
}
# main entry

if ($ARGV[1] eq 'p'){
    # live play
    playts(0);
} elsif ($ARGV[1] eq 'c'){
    open(my $fd2, '>axx.ts');
    binmode $fd2;
    for (my $k = 0; $k < scalar(@fns); $k++){
        my $be = binrd($fns[$k]);
        print $fd2 $be;
    }
    close($fd2);


} else {
    # start download

    download(0);
}



暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号