forked from NapoleonWils0n/kodi-playercorefactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrip-stats
executable file
·111 lines (90 loc) · 6.24 KB
/
rip-stats
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
#!/usr/bin/env bash
#+========================================================+#
# rip-stats #
#+========================================================+#
# 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 argument is 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')
#+========================================================+#
# ffrobe bash functions +#
#+========================================================+#
statistics_file="$HOME/Desktop/video-stats-$(date +"%Y-%m-%d-%H-%M-%S").txt"
stats () {
ffprobe -hide_banner -stats -v panic -pretty -show_entries \
format=size,duration:stream=filename,index,codec_type,codec_name,profile,bit_rate,width,height -of compact \
"${@:2}" -i "$1" | tee "$statistics_file"
}
stats_rtmp () {
rtmpdump -q -i "$1" | ffprobe -hide_banner -stats -v panic -pretty -show_entries \
format=size,duration:stream=filename,index,codec_type,codec_name,profile,bit_rate,width,height -of compact \
-i - | tee "$statistics_file"
}
#+========================================================+#
# + case statement with regular expression #
# - to execute ffprobe commands based on matching url #
#+========================================================+#
printf "%s\n" "Statistics for: '$videourl'"
case "$videourl" in
$videofile) stats "$videourl";;
$m3u8) stats "$videourl";;
$cookiem3u8) stats "$videourl" -cookies "$cookienltid; path=/; $cookiedomain;";;
$videoqmark) stats "$videourl";;
$xforward) stats "$videofile_grep" -headers 'X-Forwarded-For: '"$xforwardip"''$'\r\n';;
$useref) stats "$videofile_grep" -user-agent "$useragent" -headers 'Referer: '"$referer"''$'\r\n';;
$viduseragent) stats "$videofile_grep" -user-agent "$useragent";;
$useragentreferer) stats "$videofile_grep" -user-agent "$useragent" -headers 'Referer: '"$referer"''$'\r\n';;
$refuseragent) stats "$videofile_grep" -headers 'Referer: '"$referer"''$'\r\n' -user-agent "$useragent";;
$rtmp) stats_rtmp "$videourl";;
$cookiem3u8uagent) stats "$videofile_grep" -user-agent "$useragent2" -cookies "$cookieurl; path=/; $cookiedomain;";;
$http) stats "$videourl";;
*) stats "$videourl";;
esac
shopt -u extglob # turn off shell globbing
exit "$SUCCESS" # exit status SUCCESS if script runs without errors