-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmpv-ssh
executable file
·158 lines (138 loc) · 3.61 KB
/
mpv-ssh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
# SPDX-FileCopyrightText: 2023 - 2024 sudorook <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# 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 <https://www.gnu.org/licenses/>.
set -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command find fzf mpv sftp ssh && exit 3
#
# Functions
#
print_list() {
ssh \
"${SSH_USER:+${SSH_USER}@}${REMOTE}" \
'find '"${DIR@Q}"' -type f \( \
-iname "*.avi" -o \
-iname "*.m4v" -o \
-iname "*.mkv" -o \
-iname "*.mov" -o \
-iname "*.mp4" -o \
-iname "*.ogg" -o \
-iname "*.ts" -o \
-iname "*.webm" -o \
-iname "*.wmv" \)'
}
print_fuzzy_search() {
ssh \
"${SSH_USER:+${SSH_USER}@}${REMOTE}" \
'find '"${DIR@Q}" | fzf --algo=v2 --scheme=path --filter="${QUERY}" | head -n 1
}
make_filename() {
local file
while read -r file; do
echo sftp://"${SSH_USER:+${SSH_USER}@}${REMOTE}:${PORT:-22}/${file}" || exit 3
done <<< "$(< /dev/stdin)"
}
print_usage() {
show_header "Usage: mpv-ssh"
show_listitem "\
-s|--shuffle flag shuffle streams (default: false)
-r|--remote IP address of SSH server
-u|--user SSH user name
-d|--directory remote directory to search over SSH
-q|--query query string for searching remote directories
-n|--num number of files to stream over SSH
-h|--help print (this) help message"
}
#
# Globals
#
REMOTE=
DIR=
#
# Parse command line variables.
#
OPTIONS=d:shn:q:r:u:
LONGOPTIONS=directory:,shuffle,help,num:,query:,remote:,user:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-d | --directory)
DIR="${2}"
shift 2
;;
-q | --query)
QUERY="${2}"
shift 2
;;
-s | --shuffle)
SHUFFLE=true
shift
;;
-h | --help)
print_usage
exit
;;
-n | --num)
NUM="${2}"
shift 2
;;
-u | --user)
SSH_USER="${2}"
shift 2
;;
-r | --remote)
REMOTE="${2}"
shift 2
;;
--)
shift
break
;;
*)
show_error "ERROR: Unknown argument ${1@Q}. Exiting."
exit 3
;;
esac
done
#
# Main
#
if ! [[ -v REMOTE ]] || [ -z "${REMOTE}" ]; then
show_error "ERROR: No remote IP address given. Exiting."
exit 3
fi
if ! [[ -v DIR ]] || [ -z "${DIR}" ]; then
show_error "ERROR: No path specified. Exiting."
exit 3
fi
if [[ -v QUERY ]] && [ -n "${QUERY}" ]; then
DIR="$(print_fuzzy_search || true)"
if [ -z "${DIR}" ]; then
show_error "ERROR: Failed to find search directory. Exiting."
exit 3
fi
fi
if "${SHUFFLE:-false}"; then
print_list | shuf | head -n "${NUM:--0}" | make_filename |
mpv --playlist=- --load-unsafe-playlists --prefetch-playlist=yes
else
# `find` output is not in any particular order, so pipe though `sort` first
print_list | sort -V | head -n "${NUM:--0}" | make_filename |
mpv --playlist=- --load-unsafe-playlists --prefetch-playlist=yes
fi