2017-06-21 18:28:37 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
=pod
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
ytv - Start downloading youtube video and play it immediately
|
|
|
|
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
2018-09-02 16:35:51 +00:00
|
|
|
B<ytv> [--tor] I<youtube-url>
|
2017-06-21 18:28:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
B<ytv> starts B<youtube-dl> in the background. When the partial
|
|
|
|
downloaded file exists, it is played by B<vlc>.
|
|
|
|
|
2018-09-02 16:35:51 +00:00
|
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item B<--tor>
|
|
|
|
|
|
|
|
Use tor proxy to download. It assumes you are running a tor proxy on
|
|
|
|
127.0.0.1:9050.
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
|
2017-06-21 18:28:37 +00:00
|
|
|
=head1 AUTHOR
|
|
|
|
|
|
|
|
Copyright (C) 2017 Ole Tange,
|
|
|
|
http://ole.tange.dk and Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
|
|
=head1 LICENSE
|
|
|
|
|
|
|
|
Copyright (C) 2012 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
at your option any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
=head1 DEPENDENCIES
|
|
|
|
|
|
|
|
B<ytv> uses B<youtube-dl> and B<vlc>.
|
|
|
|
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
B<ytv>, B<vlc>
|
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2018-09-02 16:35:51 +00:00
|
|
|
use Getopt::Long;
|
|
|
|
Getopt::Long::Configure("bundling","require_order");
|
|
|
|
get_options_from_array(\@ARGV) || die_usage();
|
|
|
|
my @tor = $opt::tor ? qw(--proxy socks4a://127.0.0.1:9050/) : ();
|
|
|
|
|
2017-06-21 18:28:37 +00:00
|
|
|
`mkdir -p ~/tmp/Videos`;
|
|
|
|
chdir($ENV{'HOME'}."/tmp/Videos");
|
2017-06-29 17:12:57 +00:00
|
|
|
map { $before{$_} = -M $_ } <*>;
|
2017-06-21 18:28:37 +00:00
|
|
|
for my $url (@ARGV) {
|
|
|
|
if(not fork()) {
|
|
|
|
# Download in the background
|
2018-09-02 16:35:51 +00:00
|
|
|
system(qw(youtube-dl -f best), @tor,$url);
|
2017-06-21 18:28:37 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 17:12:57 +00:00
|
|
|
$SIG{'CHLD'} = sub { exit; };
|
2017-06-21 18:28:37 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
# Sleep until there are matching files
|
|
|
|
sleep(1);
|
2018-06-06 19:00:09 +00:00
|
|
|
@new = (grep { !/Frag\d/ } grep { -s $_ > 1000000 }
|
2018-04-28 19:17:34 +00:00
|
|
|
grep { not $before{$_} or $before{$_} > -M $_ } <*>);
|
2017-06-29 17:12:57 +00:00
|
|
|
} until @new;
|
2017-06-21 18:28:37 +00:00
|
|
|
|
2017-06-29 17:12:57 +00:00
|
|
|
while(@new) {
|
|
|
|
# Mark as seen
|
|
|
|
map { $before{$_} = -M $_ } @new;
|
2017-06-21 18:28:37 +00:00
|
|
|
# Run VLC on the matching files
|
2018-06-06 19:00:09 +00:00
|
|
|
system("vlc", map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @new);
|
2017-06-29 17:12:57 +00:00
|
|
|
@new = grep { not $before{$_} or $before{$_} > -M $_ } <*>;
|
2017-06-21 18:28:37 +00:00
|
|
|
}
|
2018-09-02 16:35:51 +00:00
|
|
|
|
|
|
|
sub get_options_from_array {
|
|
|
|
# Run GetOptions on @array
|
|
|
|
# Returns:
|
|
|
|
# true if parsing worked
|
|
|
|
# false if parsing failed
|
|
|
|
# @array is changed
|
|
|
|
my $array_ref = shift;
|
|
|
|
# A bit of shuffling of @ARGV needed as GetOptionsFromArray is not
|
|
|
|
# supported everywhere
|
|
|
|
my @save_argv;
|
|
|
|
my $this_is_ARGV = (\@::ARGV == $array_ref);
|
|
|
|
if(not $this_is_ARGV) {
|
|
|
|
@save_argv = @::ARGV;
|
|
|
|
@::ARGV = @{$array_ref};
|
|
|
|
}
|
|
|
|
my @retval = GetOptions
|
|
|
|
("debug|D" => \$opt::debug,
|
|
|
|
"tor" => \$opt::tor,
|
|
|
|
"version|V" => \$opt::version,
|
|
|
|
);
|
|
|
|
if(not $this_is_ARGV) {
|
|
|
|
@{$array_ref} = @::ARGV;
|
|
|
|
@::ARGV = @save_argv;
|
|
|
|
}
|
|
|
|
return @retval;
|
|
|
|
}
|