-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmpv-play
More file actions
executable file
·194 lines (176 loc) · 4.75 KB
/
mpv-play
File metadata and controls
executable file
·194 lines (176 loc) · 4.75 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash
# SPDX-FileCopyrightText: 2021 - 2024 sudorook <daemon@nullcodon.com>
#
# 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 ffprobe find mpv xargs && exit 3
#
# Functions
#
check_inputs() {
local item
for item in "${@}"; do
if [ -L "${item}" ]; then
item="$(readlink -f "${item}")"
fi
if [ -d "${item}" ] || [ -f "${item}" ]; then
continue
else
return 1
fi
done
return 0
}
check_duration() {
local duration
duration="$(ffprobe -v error \
-show_entries stream=duration \
-select_streams a:0 \
-of default=noprint_wrappers=1:nk=1 file:"${1}")"
if (("${duration%.*}" >= "${CUTOFF}")); then
return 0
else
return 1
fi
}
export -f check_duration
print_list() {
if [[ "${CUTOFF}" = 0 ]]; then
if [[ -v FILETYPE ]]; then
find "${IN[@]}" -type f \
\( -iname "*.${FILETYPE}" \) -print
else
find "${IN[@]}" -type f \
\( -iname "*.aac" -o \
-iname "*.alac" -o \
-iname "*.flac" -o \
-iname "*.mka" -o \
-iname "*.mp3" -o \
-iname "*.opus" -o \
-iname "*.wav" \) -print
fi
else
if [[ -v FILETYPE ]]; then
find "${IN[@]}" -type f \
\( -iname "*.${FILETYPE}" \) -print0 |
xargs -0 -P "$(nproc)" -I{} bash -c 'check_duration "{}" && echo "{}"'
else
find "${IN[@]}" -type f \
\( -iname "*.aac" -o \
-iname "*.alac" -o \
-iname "*.flac" -o \
-iname "*.mka" -o \
-iname "*.mp3" -o \
-iname "*.opus" -o \
-iname "*.wav" \) -print0 |
xargs -0 -P "$(nproc)" -I{} bash -c 'check_duration "{}" && echo "{}"'
fi
fi
}
print_usage() {
show_header "Usage: mpv-play <path/to/file(s)>"
show_listitem "\
-c|--cutoff <int> exclude files shorter than cutoff in sec. (default: 0)
-l|--loop loop playlist infinitely (default: false)
-f|--filetype <string> filter files by extension, e.g. 'mp3' (optional)
-r|--replaygain <string> replaygain mode (default: album)
-s|--shuffle flag to shuffle playlist (default: false)
-p|--pause pause mpv at start (default: false)
-h|--help print (this) help message"
}
#
# Globals
#
CUTOFF=0
SHUFFLE=false
#
# Parse command line variables.
#
OPTIONS=sf:lc:hpr:
LONGOPTIONS=shuffle,filetype:,loop,cutoff:,help,pause,replaygain:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ "${#}" -ge 1 ]; do
case "${1}" in
-c | --cutoff)
CUTOFF="${2}"
shift 2
;;
-f | --filetype)
FILETYPE="${2}"
shift 2
;;
-s | --shuffle)
SHUFFLE=true
shift
;;
-l | --loop)
LOOP=true
shift
;;
-h | --help)
print_usage
exit
;;
-p | --pause)
PAUSE="--pause"
shift
;;
-r | --replaygain)
case "${2,,}" in
album | a)
REPLAYGAIN=album
;;
track | t)
REPLAYGAIN=track
;;
no | none | off)
REPLAYGAIN=no
;;
*)
show_error "ERROR: ReplayGain options are 'track', 'album', and 'no'. See \`man mpv\`."
exit 3
;;
esac
shift 2
;;
--)
shift
break
;;
*)
show_error "ERROR: Unknown argument ${1@Q}. Exiting."
exit 3
;;
esac
done
#
# Main
#
IN=("${@}")
! check_inputs "${IN[@]}" && exit 3
export CUTOFF
if "${SHUFFLE}"; then
# Don't quote ${PAUSE:-} so that when PAUSE is unset, the expression
# evaluates to nothing instead of a file named "".
print_list | mpv ${LOOP:+--loop-playlist} ${PAUSE:-} --shuffle --no-audio-display --replaygain="${REPLAYGAIN:-album}" --playlist=-
else
# Don't quote ${PAUSE:-} so that when PAUSE is unset, the expression
# evaluates to nothing instead of a file named "".
print_list | sort -V | mpv ${LOOP:+--loop-playlist} ${PAUSE:-} --no-audio-display --replaygain="${REPLAYGAIN:-album}" --playlist=-
fi