-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpv
162 lines (145 loc) · 4.25 KB
/
mpv
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
#!/bin/sh
# mpv-android-argparse
#
# Github: https://github.com/Rendevior/mpv-android-argparse
VERSION="v0.1.7"
app_readable_dir="/storage/emulated/0/mpv"
app_readable_file="${app_readable_dir}/mpv.tmp.mp4" # mp4 extension to bypass restriction for multimedia only permission
[ ! -t 0 ] && stdinurl="$(awk '{print $0}')"
# Input Colors
err(){
printf "\033[38;5;1m%b\033[0m\n" "${1}" >&2
exit 1
}
suc(){
printf "\033[38;5;2m%b\033[0m\n" "${1}"
}
kill_jobs(){
kill "${lc_pid}" >/dev/null 2>&1
kill "${tail_pid}" >/dev/null 2>&1
if [ -n "$(jobs -p)" ]; then
jobs -p | while read -r jobs; do
kill "${jobs}"
done
fi
}
if [ -w "${app_readable_dir%*/mpv}" ]; then
[ -d "${app_readable_dir}" ] || mkdir "${app_readable_dir}"
[ -e "${app_readable_dir}/var.log" ] && : > "${app_readable_dir}/var.log"
else
err "Exception: \"${app_readable_dir%*/mpv}\" is unwritable, make sure you have storage permission"
fi
show_help(){
while IFS= read -r line; do
printf "%b\n" "${line}"
done <<-EOF
Program that lets you pass argument to mpv-android.
Usage:
${0##*/} [URL|-] <args>
${0##*/} -v | -h | -l
Options:
-v Print Version and Exit
-q Only output errors
-lc Use http server (for local termux files)
-h Print Help
-l Dump Log in stdout (default: disabled)
Use "-" to receive url from stdin.
"--http-header-fields" headers was delimited using "|".
More info: <https://mpv.io/>.
Check out manual: <https://mpv.io/manual/master/>.
EOF
}
# main
mainstripper(){
printf '%s\n' "${@}" | \
sed -E 's_---__g
s_--__g' | \
sed -E "/^(file:\/\/|http:\/\/|https:\/\/|ytdl:\/\/|smb:\/\/|bd:\/\/)|^-$|^-(h|v|l|q|lc)$/d
s_\x27__g
s_\x22__g
/.*=/ {
s_(=)(.*)_\1\x22\2\x22_
s_[[:space:]]\x22\$_\x22_g
}
/msg-level/ {
s_\x22__g
}
/http-header/ {
s_\x22__g
s_(=)(.*)_\1\x27\2\x27_
s_\|_\x27, \x27_g
}
s_[[:space:]]\$__g
s_\x27[[:space:]]_\x27_g"
}
inter_loop(){
tail -f "${app_readable_dir}/var.log" 2>/dev/null | grep -a --line-buffer -v "vo/gpu/opengl" &
tail_pid="${!}"
trap "kill_jobs ; exit 0" INT HUP
while true; do
sleep 3
grep -a -Pq '\[(main|cplayer)\] (Exiting.*|.*quit\,)|(EOF reached.)' "${app_readable_dir}/var.log" && { kill_jobs ; exit 0 ;}
done
}
start_mpv(){
for iter; do
if printf '%s' "${iter}" | grep -qEo "^(file://|http://|https://|ytdl://|smb://|bd://)[^'\"<>]*|^-$"; then
[ "${iter}" = "-" ] && set -- "${stdinurl}" && break
set -- "${iter}" && break
fi
done
[ -z "${1}" ] && err 'Exception: No Video URL provided'
printf '%s' "${1}" | grep -qEo "^(file://|http://|https://|ytdl://|smb://|bd://)[^'\"<>]*|^-$" || err "Invalid type of URL"
if printf '%s' "${1}" | grep -qEo "file://[^'\"<>]*"; then
if printf '%s' "${1#file://}" | grep -qE '~'; then
path_file=~/"${1#*~/}"
path_file="$(realpath -q "${path_file#file://}")"
else
path_file="$(realpath -q "${1#file://}")"
fi
[ -e "${path_file}" ] || err "mpv: ${path_file#file://}: No such file or directory"
set -- "file://${path_file}"
fi
if [ "${lc_use}" = "1" ] && printf '%s' "${1}" | grep -qEo "file://[^'\"<>]*"; then
command -v "darkhttpd" >/dev/null || err "Install darkhttpd first!"
random_port="$(awk -v lh="3000" -v rh="7000" 'BEGIN{srand(); print int(lh+rand()*(rh-lh+1))}')"
dir_file="${1#file://}" dir_file="${dir_file%/*}"
name_file="${1##*/}"
darkhttpd "${dir_file}" --index "${name_file}" --port "${random_port}" >/dev/null 2>&1 &
lc_pid="${!}"
[ "${quiet}" = "1" ] || suc 'press "CTRL + C" to quit'
set -- "http://0.0.0.0:${random_port}"
trap "kill_jobs ; exit 0" INT HUP
fi
am start --user current -R 2 -a android.intent.action.VIEW -d "${1}" -n 'is.xyz.mpv/.MPVActivity' >/dev/null 2>&1
[ "${quiet}" = "1" ] || suc "Video Successfully Launched!"
# Delay to start activity first before clearing the temp file
(sleep 5 && : > "${app_readable_file}") &
}
arg_parser(){
for arg; do
case "${arg}" in
-v)
printf '%s\n' "Version: ${VERSION}"
exit 0
;;
-q)
quiet=1
;;
-lc)
lc_use=1
;;
-h)
show_help
exit 0
;;
-l)
show_log="true"
;;
esac
done
}
arg_parser "${@}"
mainstripper "${@}" "--log-file=${app_readable_dir}/var.log" > "${app_readable_file}"
start_mpv "${@}"
[ "${show_log}" = "true" ] && inter_loop || [ -n "${lc_pid}" ] && wait "${lc_pid}"