tangetools/ytv/ytv
2017-06-29 19:12:57 +02:00

82 lines
1.7 KiB
Perl
Executable file

#!/usr/bin/perl
=pod
=head1 NAME
ytv - Start downloading youtube video and play it immediately
=head1 SYNOPSIS
B<ytv> I<youtube-url>
=head1 DESCRIPTION
B<ytv> starts B<youtube-dl> in the background. When the partial
downloaded file exists, it is played by B<vlc>.
=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
`mkdir -p ~/tmp/Videos`;
chdir($ENV{'HOME'}."/tmp/Videos");
map { $before{$_} = -M $_ } <*>;
for my $url (@ARGV) {
if(not fork()) {
# Download in the background
system(qw(youtube-dl -f best),$url);
exit;
}
}
$SIG{'CHLD'} = sub { exit; };
do {
# Sleep until there are matching files
sleep(1);
@new = grep { not $before{$_} or $before{$_} > -M $_ } <*>;
} until @new;
while(@new) {
# Mark as seen
map { $before{$_} = -M $_ } @new;
# Run VLC on the matching files
system("vlc",@new);
@new = grep { not $before{$_} or $before{$_} > -M $_ } <*>;
}