forked from NapoleonWils0n/kodi-playercorefactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrip-mpv
executable file
·91 lines (75 loc) · 5.46 KB
/
rip-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
#!/usr/bin/env bash
#+========================================================+#
# rip-mpv #
#+========================================================+#
# exit status, error codes
SUCCESS=0 # success exit code
E_WRONGARGS=65 # wrong number of arguments passed to script
E_NOMATCH=66 # url not matched by regular expression
# script usage
script_usage="Usage: $(basename "$0") [(http|https|rtmp|rtmpe)://example.com/video-url]"
#+========================================================+#
# + validate user input for first argument #
# - passed to script #
# + check if 1 or 3 arguments are passed to script #
# - validate user input and enable shell globbing #
#+========================================================+#
# check number of arguments passed to script and exit if not correct
[[ $# -eq 1 ]] || { printf "%s\n" "$script_usage"; exit "$E_WRONGARGS"; }
# check if first argument is a text file or on the command line
[[ "$1" =~ \.txt$ ]] && videourl=$(<"$1") || videourl="$1"
first_arg_validated="^(http|https|rtmp|rtmpe)://*" # matches: http|https|rtmp|rtmpe
if [[ $# -eq 1 ]]; then # + one argument is passed to the script
if ! [[ $videourl =~ $first_arg_validated ]]; then # - if videourl doesnt match validation regular expression
printf "%s\n%s\n" "'$videourl' invalid input" "$script_usage" # - display invalid user input with error message
exit "$E_NOMATCH" # - exit with E_NOMATCH error code
fi
fi
shopt -s extglob # enable shell globbing
#+========================================================+#
# case statement switch on pattern match #
#+========================================================+#
videofile="@(http|https)://[a-zA-Z0-9:0-9./?=_@%-]*\.@(mkv|mp4|avi|flv)"
m3u8="@(http|https)://[a-zA-Z0-9:0-9./?=_,@\&%-]*\.m3u8"
videoqmark="@(http|https)://[a-zA-Z0-9:0-9./?=_@%-]*\.@(mkv|mp4|avi|flv|m3u8)?([^|])[a-zA-Z0-9=?&]*([^|])"
viduseragent="@(http|https)://[a-zA-Z0-9:0-9./?=_@%-]*\.@(mkv|mp4|avi|flv|m3u8)?([a-zA-Z0-9?\&=%*])*\|[uU]ser-[aA]gent=[a-zA-Z0-9/.()[:blank:],:;%+_-]*([^&])"
useragentreferer="@(http|https)://[a-zA-Z0-9:0-9./?=_\&@%-]*\.@(mkv|mp4|avi|flv|m3u8)?([a-zA-Z0-9=?&]*)\|[uU]ser-[aA]gent=[a-zA-Z0-9/.()[:blank:],:;\&@%+-]*\&[rR]eferer=@(http|https)@(://|%3A%2F%2F)[a-zA-Z%0-9./?=_-]*"
useref="@(http|https)://[a-zA-Z0-9:0-9./?=_@%-]*\.@(mkv|mp4|avi|flv)\|[uU]ser-[aA]gent=[a-zA-Z0-9/.()[:blank:],:;%+-]*\&[rR]eferer=@(http|https)@(://|%3A%2F%2F)[a-zA-Z%0-9./?=_-]*"
refuseragent="@(http|https)://[a-zA-Z0-9:0-9./?=_-]*?(.m3u8)\|[rR]eferer=@(http|https)@(://|%3A%2F%2F)[a-zA-Z%0-9./?=_-]*\&[uU]ser-[aA]gent=[a-zA-Z0-9/.()[:blank:],:;%+-]*"
xforward="@(http|https)://[a-zA-Z0-9./?=_@%-]*\.m3u8\|X-Forwarded-For=+([0-9])\.+([0-9])\.+([0-9])\.+([0-9])"
http="@(http|https)://[a-zA-Z0-9:0-9/.\&;,~*?%@_=-]*"
rtmp="@(rtmp|rtmpe)://[a-zA-Z0-9[:blank:]#%@!()_=:-/.\&;,~*?]*"
cookiem3u8="@(http|https)://[a-zA-Z0-9:0-9./?=_,@\&%-]*\.m3u8\?nltid=[a-zA-Z0-9\&%_*=]*"
cookiem3u8uagent="@(http|https)://[a-zA-Z0-9:0-9./?=_,@\&%-]*\.@(mkv|mp4|avi|flv|m3u8)?([a-zA-Z0-9/?\&=%.*_-])*\|[uU]ser-[aA]gent=[a-zA-Z0-9/.()[:blank:],:;%+_-]*\&Cookie[a-zA-Z0-9=_]*"
#+========================================================+#
# grep inside url to extract strings #
#+========================================================+#
videofile_grep=$(grep -Eo '(http|https)://[a-zA-Z0-9:0-9./?=_,@&%-]*\.(m3u8|mkv|mp4|avi|flv)?([a-zA-Z0-9?&=%*_-]*[^|])' <<<"$videourl" | sed q)
useragent=$(grep -Eo 'u?User-a?Agent=[a-zA-Z0-9/.()[:blank:],:;%+_-]*[^&]' <<<"$videourl")
useragent2=$(grep -Eo 'u?User-a?Agent=[a-zA-Z0-9/.()[:blank:],:;%+_-]*[^&]' <<<"$videourl" | sed 's/[uU]ser-[aA]gent=//g')
referer=$(grep -Eo 'r?Referer=(http|https)(://|%3A%2F%2F)[a-zA-Z%0-9./?=_-]*' <<<"$videourl" | sed 's/[rR]eferer=//')
xforwardip=$(grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' <<<"$videourl")
cookieurl=$(grep -Eo '[cC]ookie=[a-zA-Z0-9/&%_*~;=_[:blank:]]*' <<<"$videourl" | sed 's/[cC]ookie=//')
cookienltid=$(grep -Eo 'nltid=[a-zA-Z0-9&%_*=]*' <<<"$videourl")
cookiedomain=$(grep -Eo '(http|https)://[a-zA-Z0-9.-]*[^/]' <<<"$videourl" | sed 's/^http\(\|s\):\/\///g')
#+========================================================+#
# + case statement with regular expression #
# - to execute mpv commands based on matching url #
#+========================================================+#
case "$videourl" in
$videofile) mpv "$videourl";;
$m3u8) mpv "$videourl";;
$cookiem3u8) mpv --http-header-fields="cookie: $cookienltid","path=/: $cookiedomain" "$videourl";;
$videoqmark) mpv "$videourl";;
$xforward) mpv --http-header-fields="X-Forwarded-For: $xforwardip" "$videofile_grep";;
$useref) mpv --user-agent="$useragent" --referrer="$referer" "$videofile_grep";;
$viduseragent) mpv --user-agent="$useragent" "$videofile_grep";;
$useragentreferer) mpv --user-agent="$useragent" --referrer="$referer" "$videofile_grep";;
$refuseragent) mpv --referrer="$referer" --user-agent="$useragent" "$videofile_grep";;
$rtmp) rtmpdump -q -i "$videourl" | mpv -;;
$cookiem3u8uagent) mpv --user-agent="$useragent2" --http-header-fields="cookie: $cookieurl","path=/: $cookiedomain" "$videofile_grep";;
$http) mpv "$videourl";;
*) mpv "$videourl";;
esac
shopt -u extglob # turn off shell globbing
exit "$SUCCESS" # exit status SUCCESS if script runs without errors