...
 
Commits (4)
......@@ -98,7 +98,7 @@ for(@ARGV) {
if($opt::g and @cmd) {
my $a = shift @cmd;
my $run = 'find . -type f | parallel -kXq grep -l '.shell_quote(@$a);
my $run = 'find . -type f | parallel --lb -Xq grep -l '.shell_quote(@$a);
if(@cmd) {
$run .= '|' .
join"|", map { 'xargs -d"\n" grep -l '.
......
......@@ -20,6 +20,9 @@ filters out the names matching all the B<grep> expressions.
There can be multiple B<grep> expressions.
The searching is cached in B<.vidlist> in a parent dir or in the
current dir if no parents contain B<.vidlist>.
=head1 EXAMPLE
......@@ -64,12 +67,72 @@ B<G>
=cut
find_find_vidlist() {
# find_find_vidlist(start_dir)
# Find the file .vidlist in . .. ../.. ../../.. etc
# If not found: ./.vidlist
dir="$1"
if [ -f "$1"/.vidlist ] ; then
echo "$1"/.vidlist
return 0
else
if [ "/" == $(readlink -f "$dir") ]; then
echo ./.vidlist
return 0
fi
find_find_vidlist "../$dir"
return $?
fi
}
update_list() {
# update_list(vidlist_file, full_path_vidlist_dir)
# Update vidlist
# Update .vidlist with content from full_path_vidlist_dir
vidlist="$1"
full_path_vidlist_dir="$2"
# Subshell to ignore 'cd'
(cd "$full_path_vidlist_dir" &&
# Find video files, print them with size prepended
ionice -c 3 find . -iregex \
'.*\(webm\|rm\|mov\|mpg\|mpeg\|avi\|wmv\|flv\|mp4\|3gp\)$' \
-type f -printf '%s\t%p\n' ||
# If ionice -c 3 fails, try without
find . -iregex \
'.*\(webm\|rm\|mov\|mpg\|mpeg\|avi\|wmv\|flv\|mp4\|3gp\)$' \
-type f -printf '%s\t%p\n') |
# Sort by size
sort -rn |
# Remove size column
perl -pe 's/^\S+\t//' > "$vidlist".$$
# Replace old vidlist with new
mv "$vidlist".$$ "$vidlist"
}
cat_list() {
# cat the vidlist for file names in current dir
vidlist="$(find_find_vidlist .)"
full_path_vidlist_dir="$(dirname $(readlink -f "$vidlist") )"
full_path_thisdir="$(readlink -f .)"
if [ -f "$vidlist" ] ; then
# find background (>/dev/null to detach from tty)
update_list "$vidlist" "$full_path_vidlist_dir" >/dev/null &
else
# find foreground
update_list "$vidlist" "$full_path_vidlist_dir"
fi
subdir="$(echo "$full_path_thisdir" |
perl -pe 's|\Q'"$full_path_vidlist_dir"'\E|.|')/"
# cat "$vidlist" | grep matching this dir + remove dirs
# echo "$vidlist" "$full_path_thisdir" "$full_path_vidlist_dir" = "$subdir" >&2
cat "$vidlist" |
perl -ne 's|^(\./)?\Q'"$subdir"'\E|| and print'
}
stdin() {
if tty -s ; then
# STDIN is terminal
find . -iregex '.*\(webm\|rm\|mov\|mpg\|mpeg\|avi\|wmv\|flv\|mp4\|3gp\)$' -type f |
# Sort by size - descending
perl -e 'print map {"$_\n"} sort { chomp $a;chomp $b; -s $b <=> -s $a } <>'
cat_list
else
# STDIN redir'ed - read it
cat
......@@ -80,9 +143,9 @@ stdout() {
if [ -t 1 ] ; then
# STDOUT = terminal
# start VLC
parallel -Xj1 vlc
parallel --lb -Xj1 vlc
else
parallel -kXj1 ls -dS
cat
fi
}
......
......@@ -74,30 +74,58 @@ my @tor = $opt::tor ? qw(--proxy socks4a://127.0.0.1:9050/) : ();
`mkdir -p ~/tmp/Videos`;
chdir($ENV{'HOME'}."/tmp/Videos");
map { $before{$_} = -M $_ } <*>;
my @youtubeid;
if(not @ARGV) {
chomp(@ARGV = <>);
}
for my $url (@ARGV) {
print "Playing $url\n";
if(not fork()) {
# Download in the background
system(qw(youtube-dl --all-subs --skip-download), @tor,$url);
system(qw(youtube-dl -f best), @tor,$url);
exit;
}
}
$SIG{'CHLD'} = sub { exit; };
do {
# Sleep until there are matching files
sleep(1);
@new = (grep { !/Frag\d/ } grep { -s $_ > 1000000 }
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", map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @new);
@new = grep { not $before{$_} or $before{$_} > -M $_ } <*>;
do {
# Sleep until there are matching files
@new = ((grep { -e $_ } $url),
(grep { !/Frag\d/ } grep { -s $_ > 1000000 }
grep { not $before{$_} or $before{$_} > -M $_ } <*>),
map { my $id = $_; grep { /$id/ } <*> }
grep { s/^.*watch.v=([a-z0-9]+).*$/$1/i
or
s/^.*youtu.be.([a-z0-9]+).*$/$1/i } $url);
usleep(10);
} until @new;
while(@new) {
# Mark as seen
map { $before{$_} = -M $_ } @new;
# Run VLC on the matching files
if($opt::kodi) {
for(uniq(map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @new)) {
my $answer;
do {
print "KODI playing $_\n";
if(not fork()) {
exec("idok", $_);
}
print "Press ENTER\n";
open(my $tty_fh, "<", "/dev/tty") || ::die_bug("interactive-tty");
$answer = <$tty_fh>;
close $tty_fh;
if($answer =~ /d/i) {
unlink $_;
}
} while($answer =~ /r/i);
}
} else {
system("vlc", map { $a=$b=$_; $b=~s/.part//; s/.temp//; $a,$b,$_ } @new);
}
@new = grep { not $before{$_} or $before{$_} > -M $_ } <*>;
}
}
sub get_options_from_array {
......@@ -118,6 +146,7 @@ sub get_options_from_array {
my @retval = GetOptions
("debug|D" => \$opt::debug,
"tor" => \$opt::tor,
"kodi|k" => \$opt::kodi,
"version|V" => \$opt::version,
);
if(not $this_is_ARGV) {
......@@ -126,3 +155,16 @@ sub get_options_from_array {
}
return @retval;
}
sub usleep {
# Sleep this many milliseconds.
# Input:
# $ms = milliseconds to sleep
my $ms = shift;
select(undef, undef, undef, $ms/1000);
}
sub uniq {
# Remove duplicates and return unique values
return keys %{{ map { $_ => 1 } @_ }};
}