Skip to content

Instantly share code, notes, and snippets.

@tateisu
Created May 18, 2020 11:00
Show Gist options
  • Save tateisu/7db1b747cc9a8652d9459fd711779d9f to your computer and use it in GitHub Desktop.
Save tateisu/7db1b747cc9a8652d9459fd711779d9f to your computer and use it in GitHub Desktop.
あのサイトのHLS(TS)の録画
#!/usr/bin/perl --
# usage: perl recordHls.pl (room_url)
use strict;
use warnings;
use LWP::UserAgent;
use JSON::XS;
use File::Path;
use MIME::Base64;
sub e{ encode_base64($_[0]) }
sub d{ decode_base64($_[0]) }
sub test($){
my $encoded = e(@_);
my $decoded = d($encoded);
print "e: $encoded\n";
print "d: $decoded\n";
}
my $userAgent = d 'TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzgxLjAuNDA0NC4xNDEgU2FmYXJpLzUzNy4zNg==';
my $reRoomName = d 'c2hvd3Jvb20tbGl2ZS5jb20vKFteLz8jXSsp';
my $reRoomId = d 'XD9yb29tX2lkPShcZCsp';
my $listUrlBase = d 'aHR0cHM6Ly93d3cuc2hvd3Jvb20tbGl2ZS5jb20vYXBpL2xpdmUvc3RyZWFtaW5nX3VybD9yb29tX2lkPXtyb29tSWR9Jmlnbm9yZV9sb3dfc3RyZWFtPTEmXz0=';
my $ua = LWP::UserAgent->new;
$ua->agent($userAgent);
$ua->timeout(10);
$ua->env_proxy;
sub httpGet($){
my($url)=@_;
my $response = $ua->get( $url);
$response->is_success or die $response->status_line," ",$url,"\n";
return $response->decoded_content;
}
my $roomUrl = shift or die "usage: $0 (room_url)\n";
$roomUrl =~ m|$reRoomName| or die "missing room name in $roomUrl\n";
my $roomName = $1;
mkpath $roomName;
my $html = httpGet($roomUrl);
$html =~ /$reRoomId/ or die "can't find room_id. $roomUrl\n";
my($roomId)=($1);
my $listUrl = $listUrlBase . time();
$listUrl =~ s/\{roomId}/$roomId/;
my $text = httpGet($listUrl);
my $root = decode_json( $text);
my $list = $root->{streaming_url_list} or die "can't find streaming_url_list. maybe not on live.\n";
@$list or die "streaming_url_list is empty.\n";
$list = [ grep{ $_->{type} eq "hls"} @$list ];
@$list or die "no HLS streaming.\n";
sub booleanToInt{
$_[0] ? 1 : 0
}
@$list = sort{ booleanToInt($b->{is_default}) <=> booleanToInt($a->{is_default}) or $b->{quality} <=> $a->{quality} } @$list;
my $streamUrl = $list->[0]{url};
my @lt = localtime;
$lt[5]+=1900;$lt[4]+=1;
my $timeStr = sprintf "%d%02d%02dT%02d%02d%02d",reverse @lt[0..5];
my $file = "$roomName/$timeStr-$roomName.ts";
my $count=1;
while( -e $file){
++$count;
$file = "$roomName/$timeStr-$roomName-$count.ts";
}
my $cmd = qq(ffmpeg -loglevel info -timeout 30000 -user_agent '$userAgent' -i '$streamUrl' -c copy '$file');
system $cmd;
if ($? == -1) {
print "failed to execute: $!\n";
}elsif ($? & 127) {
printf "child died with signal %d, %s coredump\n",($? & 127), ($? & 128) ? 'with' : 'without';
}else{
my $c = $? >> 8;
$c != 0 and printf "child exited with value %d\n", $? >> 8;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment