-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrip_and_direct_playback.sh
executable file
·281 lines (237 loc) · 6.98 KB
/
rip_and_direct_playback.sh
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/bash
function init_stations {
local tmp_station_file=.tmp_${station_file}
/bin/rm -fr "${tmp_station_file}"
./print_internet_radios.sh "${station_file}" > "${tmp_station_file}"
station_index=0
local line
local j=0
while read -r line; do
if [ $j -eq 0 ]; then
station[$station_index]="$line"
j=1
elif [ $j -eq 1 ]; then
url[$station_index]="$line"
j=2
elif [ $j -eq 2 ]; then
suffix[$station_index]="$line"
j=0
station_index=$(( station_index + 1 ))
fi
done < "${tmp_station_file}"
/bin/rm -fr "${tmp_station_file}"
}
function get_next_station_index {
local next_index=$(( $1 + 1 ))
if [ $next_index -ge ${#url[@]} ]; then
echo 0
else
echo $next_index
fi
}
function get_station_number {
echo $(( $station_index + 1 ))
}
function record_and_play {
# Terminate old recording job
stop_recording_job
echo
echo "Finding the real stream address:"
echo -n "${url[${station_index}]} -> "
local stream_address=$(./stream_address_finder.sh "${url[${station_index}]}")
echo "$stream_address"
# Create filename for recording
local now=$(date +"%Y-%m-%d_%H-%M-%S")
local sfx=${suffix[${station_index}]}
if [ -z "$sfx" ]; then
sfx=audio
fi
recording="${recordings_folder}/${now} ${station[${station_index}]}.${sfx}"
# Create recording folder if not exists
mkdir -p "$recordings_folder"
echo -n "Starting recording.. "
wget -q -O "$recording" "$stream_address" &
job_wget_id=$!
echo OK
fix_start_seconds=$(date +%s)
echo "Writing file ./${recording}"
# In case of large (e.g .flac) audio streams it's possible that your hear no playback.
# You can either increase wait_seconds to 4 or just restart the playback manually by entering 'r' after few seconds.
local wait_seconds=1
sleep $wait_seconds
restarting_playback
playback_status="Direct playback."
}
function restarting_playback {
stop_playback_job
if [ -n "${recording}" ]; then
echo -n "Starting playback.. "
screen -D -m -S my-vlc-server cvlc -I rc "${recording}" &
job_cvlc_id=$!
is_playing=true
start_seconds=$(date +%s)
echo OK
fi
}
function get_input_from_user {
calculate_next_station_index
local next_number=$(( $next_index + 1 ))
echo -n "Enter command key or station number ($next_number).. "
read -r input
user_input=$input
}
function calculate_next_station_index {
if [ $job_wget_id -lt 0 ]; then
next_index=0
else
local possible_next_index=$(( $station_index + 1 ))
if [ $possible_next_index -ge ${#url[@]} ]; then
next_index=0
else
next_index=$possible_next_index
fi
fi
}
function execute_command {
if [[ $user_input == [qQ] ]]; then
quit
elif [[ $user_input == [rR] ]]; then
restarting_playback
playback_status="Playback was restarted and is delayed."
elif [[ $user_input == [pP] ]]; then
toggle_pause
elif [[ $user_input =~ ^[wW]+$ ]]; then
# TODO check what happens if pause is active and user wants to skip to past
seek_playback "$user_input"
elif [[ $user_input =~ ^\++$ ]]; then
# number of plus characters -> steps
local steps=${#input}
screen -S my-vlc-server -p 0 -X stuff "volup ${steps}^M"
elif [[ $user_input =~ ^-+$ ]]; then
# number of minus characters -> steps
local steps=${#input}
screen -S my-vlc-server -p 0 -X stuff "voldown ${steps}^M"
elif [[ $user_input == [bB] ]]; then
local si=$station_index
station_index=$previous_station_index
previous_station_index=$si
record_and_play
elif [[ $user_input =~ ^[0-9]+$ ]]; then
previous_station_index=$station_index
station_index=$(( $user_input - 1))
record_and_play
else
previous_station_index=$station_index
station_index=$next_index
record_and_play
fi
}
function toggle_pause {
screen -S my-vlc-server -p 0 -X stuff "pause^M"
if $is_playing; then
echo "PAUSE! -> Enter p again to go on with playback"
is_playing=false
playback_status="Playback is paused."
else
is_playing=true
playback_status=$DELAYED_PLAYBACK
fi
}
function seek_playback {
local seconds=15
local input=$1
# number of characters -> step
local step=$(( ${#input} * $seconds ))
local current_date=$(date +%s)
local elapsed_seconds=$(( $current_date - $start_seconds ))
local seek_value=$(( $elapsed_seconds - $step ))
if [ $seek_value -lt 0 ]; then
seek_value=0
fi
start_seconds=$(( $start_seconds + $step ))
if [ $start_seconds -gt "$current_date" ]; then
start_seconds=$current_date
fi
screen -S my-vlc-server -p 0 -X stuff "seek ${seek_value}^M"
playback_status=$DELAYED_PLAYBACK
}
function stop_recording_job {
if [ $job_wget_id -gt -1 ]; then
kill $job_wget_id 2> /dev/null
fi
}
function stop_playback_job {
if [ $job_cvlc_id -gt -1 ]; then
kill $job_cvlc_id 2> /dev/null
fi
}
function quit {
stop_playback_job
stop_recording_job
echo
echo "Goodbye."
exit 0
}
function print_commands_and_stations {
echo "RIP-AND-DIRECT-PLAYBACK ***************************************** (C) TS CUSTER"
echo
echo "p) Pause playback"
echo "r) Restart playback"
echo "b) Select previous station"
echo "w) Rewind 15 seconds"
echo "+) Volume up"
echo "-) Volume down"
echo "q) Quit"
echo
print_stations
}
function print_stations {
local i
for ((i=0; i < ${#url[@]}; i++)); do
local number=$(( $i + 1 ))
echo "$number) ${station[${i}]}"
done
}
function print_status {
echo "SELECTED: $(get_station_number)) ${station[station_index]}"
local file_size=$(stat --printf="%s" "$recording")
echo "Current size of \"$recording\": $file_size"
echo "$playback_status"
}
##################### START #########################
! which tr > /dev/null && echo tr must be installed. && exit 1
! which wget > /dev/null && echo wget must be installed. && exit 1
! which screen > /dev/null && echo screen must be installed. && exit 1
! which vlc > /dev/null && echo vlc must be installed. && exit 1
script_name=$(basename "$0")
if [ ${#} -lt 1 ]; then
echo "Usage: ${script_name} <internet radios file>"
exit 1
fi
# Initiate variables
station_file=$1
recordings_folder=recordings
declare -a station
declare -a url
declare -a suffix
station_index=0
recording=""
job_wget_id=-1
job_cvlc_id=-1
old_file_size=0
DELAYED_PLAYBACK="Delayed playback."
init_stations
clear
print_commands_and_stations
echo
trap quit SIGINT
station_index=0
previous_station_index=0
while get_input_from_user; do
execute_command
clear
print_commands_and_stations
echo
print_status
echo
done