-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpv_wrapper.sh
executable file
·139 lines (129 loc) · 4.12 KB
/
mpv_wrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# Wrapper script for mpv
# Usage: ${0} path/to/directory [path/to/directoriy, ...]
declare -a PARAMS
PARAM_COUNT=0
while (( "$#" )); do
case "$1" in
-s|--shuffle) # shuffle path passed
SHUFFLE=1
shift
;;
-v|--video-only) # shuffle path passed
VID_ONLY=1
#FIXME probably best to include video formats only
EXCLUDE="jpg,jpeg,png,bmp,tiff,zip,rar,7z,blend,py,pdf,txt"
shift
;;
-z|--sort-size) # sort by descending file sizes
SORTED=1
shift
;;
-x|--exclude) # exclude these comma separated extensions
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
EXCLUDE=$2
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-xp|--exclude-path) # exclude this path (can have multiple)
if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
EXCLUDE_PATHS+=(${2})
shift 2
else
echo "Error: Argument for $1 is missing" >&2
exit 1
fi
;;
-p|--no-find) # don't use find, only generate plain playlist of directories and let mpv autoload
PLAIN=1
shift
;;
-l|--limited) # use limited_autoload.so script
LIMITED=1
shift
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
PARAMS+=("$1")
PARAM_COUNT=$((PARAM_COUNT+1))
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "${PARAMS[@]}"
# Set a default sane exclusion filter
if [[ -z ${EXCLUDE} ]]; then
EXCLUDE="blend,py,pdf,txt"
fi
echo -e "DEBUG POS PARAMS:${PARAMS[@]}\nPARAM_COUNT=${PARAM_COUNT}\nEXCLUDE:${EXCLUDE}"
if [[ ! -z ${EXCLUDE} ]]; then
# replace commas with spaces
for EXT in ${EXCLUDE//,/ }; do
if [ -z "${FIND_OPTS}" ]; then
FIND_OPTS="-not -iname \"*.${EXT}\"";
else
FIND_OPTS="${FIND_OPTS} -a -not -iname \"*.${EXT}\"";
fi
done;
FIND_CMD="find ${PARAMS[@]} -type f \( ${FIND_OPTS} \)"
if [[ "${SORTED}" -eq 1 ]]; then
# sorted by decreasing file size https://stackoverflow.com/questions/22598205/how-sort-find-result-by-file-sizes
FIND_CMD="${FIND_CMD} -printf '%s\t%p\n' | sort -nr | cut -f2-"
fi
else
FIND_CMD="find ${PARAMS[@]} -type f";
fi
if [[ ! -z ${EXCLUDE_PATHS} ]]; then
FIND_OPTS=""
for EPATH in "${EXCLUDE_PATHS[@]}"; do
FIND_OPTS="${FIND_OPTS} -and -not -ipath \"*${EPATH}\"*"
done;
FIND_CMD="${FIND_CMD} ${FIND_OPTS}"
fi
if [[ "${SHUFFLE}" -eq 1 ]]; then
if [[ ${PARAM_COUNT} -gt 1 ]]; then # gather files THEN shuffle
echo "DEBUG:$FIND_CMD"; eval ${FIND_CMD};
eval ${FIND_CMD} | mpv --shuffle --playlist=-;
elif [[ -z ${EXCLUDE_PATH} && ${EXCLUDE} == "" ]]; then
# only one dir passed and no special rule, let mpv load & shuffle
# can't seem to pass exclusion filters to mpv?
mpv --shuffle -- "${PARAMS[@]}"
else
echo "DEBUG:$FIND_CMD"; eval ${FIND_CMD};
eval ${FIND_CMD} | mpv ${OPTIONS} --playlist=-;
fi
elif [[ "${PLAIN}" -eq 1 ]]; then #&& [[ ${#PARAMS[@]} -eq 1 ]]; then
# Build string from arguments, separated by \n characters
# and feed it to mpv as a playlist. mpv will then auto-load files
# as they are being read.
#PAR="";
#for p in "$@"; do # or ${PARAMS[@]}
# PAR="${p}$'\n'${PAR}";
#done;
# echo "DEBUG: PAR: ${PAR}";
OPTIONS="--script-opts=limited_autoload-enabled=0";
mpv ${OPTIONS} "${PARAMS[@]}";
elif [[ "${LIMITED}" -eq 1 ]]; then
OPTIONS="--script-opts=limited_autoload-enabled=1";
mpv ${OPTIONS} "${PARAMS[@]}";
else
#echo "DEBUG:$FIND_CMD"; eval ${FIND_CMD};
eval ${FIND_CMD} | mpv ${OPTIONS} --playlist=- --;
fi
# TODO
# * save playlist into /tmp, named from hash of path + mtime; load it if present
# * limit find output with | head -500
# Old SHUFFLE version means the shell is expanding, which implies race conditions
#cd "${1}" || echo "couldn't change dir to ${1}";
#mpv --shuffle -- *
# DIR version, only takes one directory path, loads the files in it
# https://github.com/mpv-player/mpv/tree/master/TOOLS/lua
#loads all other files in directory
#mpv --script=~/.config/mpv/scripts/autoload.lua -- *