From 725961a24f0d00cdefcdb0d9800d35d4d1ed9154 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Wed, 21 Jun 2017 20:28:37 +0200 Subject: [PATCH] ytv: Initial version. --- Makefile | 6 ++-- ytv/ytv | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 3 deletions(-) create mode 100755 ytv/ytv diff --git a/Makefile b/Makefile index 56d870d..32bd91a 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,9 @@ CMD = blink bsearch em encdir field forever G gitnext goodpasswd \ -histogram neno off pdfman puniq ramusage rand rclean rn rrm \ +histogram mtrr neno off pdfman puniq ramusage rand rclean rn rrm \ sound-reload stdout T timestamp tracefile upsidedown \ -w4it-for-port-open wifi-reload wssh +w4it-for-port-open wifi-reload wssh ytv -all: blink/blink.1 bsearch/bsearch.1 encdir/encdir.1 G/G.1 gitnext/gitnext.1 goodpasswd/goodpasswd.1 histogram/histogram.1 neno/neno.1 off/off.1 pdfman/pdfman.1 puniq/puniq.1 rand/rand.1 rn/rn.1 rrm/rrm.1 sound-reload/sound-reload.1 stdout/stdout.1 timestamp/timestamp.1 tracefile/tracefile.1 T/T.1 upsidedown/upsidedown.1 wifi-reload/wifi-reload.1 wssh/wssh.1 +all: blink/blink.1 bsearch/bsearch.1 encdir/encdir.1 G/G.1 gitnext/gitnext.1 goodpasswd/goodpasswd.1 histogram/histogram.1 neno/neno.1 off/off.1 pdfman/pdfman.1 puniq/puniq.1 rand/rand.1 rn/rn.1 rrm/rrm.1 sound-reload/sound-reload.1 stdout/stdout.1 timestamp/timestamp.1 tracefile/tracefile.1 T/T.1 upsidedown/upsidedown.1 wifi-reload/wifi-reload.1 wssh/wssh.1 ytv/ytv.1 %.1: % pod2man $< > $@ diff --git a/ytv/ytv b/ytv/ytv new file mode 100755 index 0000000..c310fd4 --- /dev/null +++ b/ytv/ytv @@ -0,0 +1,105 @@ +#!/usr/bin/perl + +=pod + +=head1 NAME + +ytv - Start downloading youtube video and play it immediately + + +=head1 SYNOPSIS + +B I + + +=head1 DESCRIPTION + +B starts B in the background. When the partial +downloaded file exists, it is played by B. + +=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 . + + +=head1 DEPENDENCIES + +B uses B and B. + + +=head1 SEE ALSO + +B, B + + +=cut + +`mkdir -p ~/tmp/Videos`; +chdir($ENV{'HOME'}."/tmp/Videos"); +for my $url (@ARGV) { + if(not fork()) { + # Download in the background + system(qw(youtube-dl -f best),$url); + exit; + } + # https://www.youtube.com/watch?v=ID + if($url =~ m{https://www.youtube.com/watch\?v=([^&]+)}) { + # Get the ID from the url + push @id,$1; + $id{$1}++; + } else { + print STDERR "$url not matched\n"; + } +} + +sub match_arr_regexp { + # Match any of @regexp + my ($regexp_arr,$content_arr) = @_; + # my $m; + # return map { $m = $_; grep { /$m/ } @$content_arr } @$regexp_arr; + my $regexp = join'|',map { "($_)" } @$regexp_arr; + return grep { /$regexp/ } @$content_arr; +} + +my (@ls,@files); + +do { + # Sleep until there are matching files + sleep(1); + @ls = <*>; + @files = match_arr_regexp(\@id,\@ls); +} until @files; + +while(@files) { + # Run VLC on the matching files + system("vlc",@files); + # Remove regexps from @id + for my $id (@id) { + if(grep { /$id/ } @ls) { + $id{$id} = 0; + } + } + @id = grep { $id{$_} } @id; + # See if there are new matches + @ls = <*>; + @files = match_arr_regexp(\@id,\@ls); +}