2018-06-06 19:01:16 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
: <<=cut
|
|
|
|
=pod
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
vid - Play videos matching strings in descending order of size
|
|
|
|
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
B<vid> [[grep options][string]]...
|
|
|
|
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
B<vid> recursively searches for videos and sort them by size. It then
|
|
|
|
filters out the names matching all the B<grep> expressions.
|
|
|
|
|
|
|
|
There can be multiple B<grep> expressions.
|
|
|
|
|
2018-11-24 22:49:47 +00:00
|
|
|
The searching is cached in B<.vidlist> in a parent dir or in the
|
|
|
|
current dir if no parents contain B<.vidlist>.
|
|
|
|
|
2018-06-06 19:01:16 +00:00
|
|
|
|
|
|
|
=head1 EXAMPLE
|
|
|
|
|
|
|
|
Play videos matching B<Documentary> but not B<BBC>:
|
|
|
|
|
|
|
|
vid Documentary -v BBC
|
|
|
|
|
|
|
|
|
|
|
|
=head1 AUTHOR
|
|
|
|
|
2019-02-03 16:51:05 +00:00
|
|
|
Copyright (C) 2018-2019 Ole Tange,
|
2018-06-06 19:01:16 +00:00
|
|
|
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<vid> uses B<G>, and B<vlc>.
|
|
|
|
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
|
|
|
B<G>
|
|
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
2018-11-24 22:49:47 +00:00
|
|
|
find_find_vidlist() {
|
2018-12-13 20:02:14 +00:00
|
|
|
# find_find_vidlist(start_dir)
|
|
|
|
# Find the file .vidlist in . .. ../.. ../../.. etc
|
|
|
|
# If not found: ./.vidlist
|
2018-11-24 22:49:47 +00:00
|
|
|
dir="$1"
|
|
|
|
if [ -f "$1"/.vidlist ] ; then
|
|
|
|
echo "$1"/.vidlist
|
|
|
|
return 0
|
|
|
|
else
|
2019-02-03 16:51:05 +00:00
|
|
|
if [ "/" == $(readlink -f "$dir") ] ; then
|
2018-11-24 22:49:47 +00:00
|
|
|
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" &&
|
2018-12-13 20:02:14 +00:00
|
|
|
# Find video files, print them with size prepended
|
|
|
|
ionice -c 3 find . -iregex \
|
2019-01-14 06:53:58 +00:00
|
|
|
'.*\(webm\|rm\|mkv\|mov\|mpg\|mpeg\|asf\|avi\|wmv\|flv\|mp4\|3gp\)$' \
|
2018-12-13 20:02:14 +00:00
|
|
|
-type f -printf '%s\t%p\n' ||
|
|
|
|
# If ionice -c 3 fails, try without
|
|
|
|
find . -iregex \
|
2019-01-14 06:53:58 +00:00
|
|
|
'.*\(webm\|rm\|mkv\|mov\|mpg\|mpeg\|asf\|avi\|wmv\|flv\|mp4\|3gp\)$' \
|
2018-11-24 22:49:47 +00:00
|
|
|
-type f -printf '%s\t%p\n') |
|
|
|
|
# Sort by size
|
|
|
|
sort -rn |
|
2020-03-21 13:11:17 +00:00
|
|
|
# Uniq
|
|
|
|
uniq |
|
2018-12-13 20:02:14 +00:00
|
|
|
# Remove size column
|
2018-11-24 22:49:47 +00:00
|
|
|
perl -pe 's/^\S+\t//' > "$vidlist".$$
|
2018-12-13 20:02:14 +00:00
|
|
|
# Replace old vidlist with new
|
2018-11-24 22:49:47 +00:00
|
|
|
mv "$vidlist".$$ "$vidlist"
|
|
|
|
}
|
|
|
|
|
|
|
|
cat_list() {
|
2018-12-13 20:02:14 +00:00
|
|
|
# cat the vidlist for file names in current dir
|
2018-11-24 22:49:47 +00:00
|
|
|
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" |
|
2018-12-13 20:02:14 +00:00
|
|
|
perl -pe 's|\Q'"$full_path_vidlist_dir"'\E|.|')/"
|
2018-11-24 22:49:47 +00:00
|
|
|
# cat "$vidlist" | grep matching this dir + remove dirs
|
2018-12-13 20:02:14 +00:00
|
|
|
# echo "$vidlist" "$full_path_thisdir" "$full_path_vidlist_dir" = "$subdir" >&2
|
2018-11-24 22:49:47 +00:00
|
|
|
cat "$vidlist" |
|
|
|
|
perl -ne 's|^(\./)?\Q'"$subdir"'\E|| and print'
|
|
|
|
}
|
|
|
|
|
2018-06-06 19:01:16 +00:00
|
|
|
stdin() {
|
|
|
|
if tty -s ; then
|
|
|
|
# STDIN is terminal
|
2018-11-24 22:49:47 +00:00
|
|
|
cat_list
|
2018-06-06 19:01:16 +00:00
|
|
|
else
|
|
|
|
# STDIN redir'ed - read it
|
|
|
|
cat
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout() {
|
|
|
|
if [ -t 1 ] ; then
|
|
|
|
# STDOUT = terminal
|
|
|
|
# start VLC
|
2018-12-13 20:02:14 +00:00
|
|
|
parallel --lb -Xj1 vlc
|
2018-06-06 19:01:16 +00:00
|
|
|
else
|
2018-11-24 22:49:47 +00:00
|
|
|
cat
|
2018-06-06 19:01:16 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
stdin | G "$@" | stdout
|
|
|
|
|
|
|
|
exit $?
|