/
rip-record
executable file
·146 lines (124 loc) · 7.98 KB
/
rip-record
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
#!/bin/bash
#+========================================================+#
# rip-record #
#+========================================================+#
# 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] ([-t] [00:00:00])"
#+========================================================+#
# + validate user input for first, second, third arguments #
# - passed to script #
# + second and third arguments passed to script are used #
# - to specify a recording duration #
# + 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 || $# -eq 3 ]] || { 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
second_arg_validated="^-t$" # matches: -t
third_arg_validated="^[0-9]{2}:[0-9]{2}:[0-9]{2}$" # matches: 00:00:00
tflag="$2" # second argument should be: -t
duration="$3" # third argument should be in this format: 00:00:00
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
else [[ $# -eq 3 ]] # + three arguments are passed to the script, validate user input
if ! [[ $videourl =~ $first_arg_validated && $tflag =~ $second_arg_validated && $duration =~ $third_arg_validated ]]; then
printf "%s\n%s\n" "'$1 $2 $3' 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')
#+========================================================+#
# recording functions using ffmpeg and rtmpdump +#
#+========================================================+#
ripconfig="$HOME/.ripconfig" # config file
[[ -f "$ripconfig" ]] && source "$ripconfig" # check if config file exist
[[ -d "$ripdir" ]] || ripdir="" # check if directory exists
if [[ $ripdir = *[!/] ]]; then ripdir+=/; fi # check if directory has a trailing slash, if not add one
recordingdir="$HOME/Desktop/" # default recording directory
record () {
count=0 # set count to zero
while [[ "$count" -lt 3 ]]; do # while count is less than 3 run the script
recordingfile="${ripdir:=$recordingdir}video-$(date +"%Y-%m-%d-%H-%M-%S").mkv"
ffmpeg \
-hide_banner -stats -v panic "${@:2}" -i "$1" -c:v copy -c:a copy $tflag $duration "$recordingfile"
if [[ $? -eq 0 ]]; then
break # break out of the while loop
exit "$SUCCESS" # exit with $success error code
fi
let count=count+1 # increment count
sleep 1
done
}
record_rtmp () {
count=0 # set count to zero
while [[ "$count" -lt 3 ]]; do # while count is less than 3 run the script
recordingfile="${ripdir:=$recordingdir}video-$(date +"%Y-%m-%d-%H-%M-%S").mkv"
rtmpdump -q -i "$1" | ffmpeg \
-hide_banner -stats -v panic -i - -c:v copy -c:a copy $tflag $duration "$recordingfile"
if [[ $? -eq 0 ]]; then # check the ffmpeg exit code is 0, which means the recording worked
break # break out of the while loop
exit "$SUCCESS" # exit with $success error code
fi
let count=count+1 # increment count
sleep 1
done
}
#+========================================================+#
# + case statement with regular expression #
# - to execute ffmpeg commands based on matching url #
#+========================================================+#
case "$videourl" in
$videofile) record "$videourl";;
$m3u8) record "$videourl";;
$cookiem3u8) record "$videourl" -cookies "$cookienltid; path=/; $cookiedomain;";;
$videoqmark) record "$videourl";;
$xforward) record "$videofile_grep" -headers 'X-Forwarded-For: '"$xforwardip"''$'\r\n';;
$useref) record "$videofile_grep" -user-agent "$useragent" -headers 'Referer: '"$referer"''$'\r\n';;
$viduseragent) record "$videofile_grep" -user-agent "$useragent";;
$useragentreferer) record "$videofile_grep" -user-agent "$useragent" -headers 'Referer: '"$referer"''$'\r\n';;
$refuseragent) record "$videofile_grep" -headers 'Referer: '"$referer"''$'\r\n' -user-agent "$useragent";;
$rtmp) record_rtmp "$videourl";;
$cookiem3u8uagent) record "$videofile_grep" -user-agent "$useragent2" -cookies "$cookieurl; path=/; $cookiedomain;";;
$http) record "$videourl";;
*) record "$videourl";;
esac \
&& type -P notify-send &> /dev/null && [[ $? -eq 0 ]] \
&& notify-send -i kodi "Finished Recording" "$videourl"
shopt -u extglob # turn off shell globbing
exit "$SUCCESS" # exit status SUCCESS if script runs without errors