-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_from_youtube.bash
executable file
·227 lines (193 loc) · 4.28 KB
/
sample_from_youtube.bash
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
#!/usr/bin/env bash
function parse {
awk '
function trim(str) {
# Remove leading and trailing whitespaces
gsub(/^\s*/, "", str)
gsub(/\s*$/, "", str)
return str
}
BEGIN {
FS = "|"
OFS = ","
}
# A Comment:
/^#/ {
next
}
# Video link:
/^-/ {
if (NF != 2) {
print "Check line", NR > "/dev/stderr"
exit 1
}
first = substr($1, 2)
record_name = first
yt_url = $2
record_name = trim(record_name)
yt_url = trim(yt_url)
}
# Sample snippet:
!/^-/ && !/^$/ {
if (NF != 2) {
print "Check line", NR > "/dev/stderr"
exit 2
}
timestamps_string = $1 # Like "0:12 - 0:19"
sample_name = $2
timestamps_string = trim(timestamps_string)
sample_name = trim(sample_name)
if (split(timestamps_string, timestamps, "-") != 2) {
print "Check line", NR > "/dev/stderr"
exit 3
}
for (i in timestamps) {
timestamps[i] = trim(timestamps[i])
}
filename = record_name "__" sample_name
gsub(/ /, "_", filename)
# Output format: filename, youtube url, start timestamp, end timestamp
print filename, yt_url, timestamps[1], timestamps[2]
}
' "$1"
}
function skip_collected {
while IFS=',' read -ra LINE
do
FNAME="${LINE[0]}"
YT_URL="${LINE[1]}"
START="${LINE[2]}"
END="${LINE[3]}"
if test -f "${SAMPLES_DIR}/${FNAME}.wav"
then
echo "Skipping \"$FNAME\", file already exists..." 1>&2
continue
fi
echo $FNAME,$YT_URL,$START,$END
done
}
function append_video_id {
awk '
BEGIN {
FS=","
OFS=","
}
{
# Match video id in youtube url query string:
idx = match($2, /(\?v|&v)[^&]*/)
if (idx == 0 || RLENGTH - 3 != 11) {
printf("Could not extract video id from %s for \"%s\", skipping\n", $2, $1) > "/dev/stderr"
next
}
# +=3 because of "?v" or "&v" prefix:
print $0, substr($2, RSTART + 3, RLENGTH - 3)
}'
}
# NOTE: couldn't I have just deleted temp files with 'sample_from_youtube_downloaded_files_' prefix?
# answer: NO! what about other users who might be downloading samples in parallel (Really???)?
# NOTE: I could also have made per-process unique prefix...
DOWNLOADED_FILES=$(mktemp -t "sample_from_youtube_downloaded_files_XXXXXXX")
trap "(cat $DOWNLOADED_FILES | xargs -r rm) && rm $DOWNLOADED_FILES" EXIT
function download_audio {
declare -A id_to_file
while IFS=',' read -ra LINE
do
FNAME="${LINE[0]}"
YT_URL="${LINE[1]}"
START="${LINE[2]}"
END="${LINE[3]}"
YT_ID="${LINE[4]}"
if test -n "${id_to_file[$YT_ID]}"
then
TMPFILE="${id_to_file[$YT_ID]}"
else
TMPFILE=$(mktemp -t "sample_from_youtube_${YT_ID}_XXXXX.wav")
echo "Starting $YT_ID download..." 1>&2
youtube-dl \
--no-cache-dir \
--quiet \
--no-continue \
--no-playlist \
--extract-audio \
--audio-format wav \
--audio-quality 0 \
--output "$TMPFILE" \
"$YT_URL"
youtube_dl_status="$?"
if test 0 == "$youtube_dl_status"
then
echo "Downloading $YT_ID complete." 1>&2
else
echo "Failed to download $YT_ID. `youtube-dl` exit status: $youtube_dl_status" 1>&2
continue
fi
echo $TMPFILE >> $DOWNLOADED_FILES
id_to_file[$YT_ID]=$TMPFILE
fi
echo $FNAME,$TMPFILE,$START,$END
done
}
function crop_sample {
while IFS=',' read -ra LINE
do
FNAME="${LINE[0]}"
AUDIO_FILE="${LINE[1]}"
START="${LINE[2]}"
END="${LINE[3]}"
# TODO: $END needs +1 second.
ffmpeg \
-nostdin \
-i "$AUDIO_FILE" \
-ss "$START" \
-to "$END" \
-n \
-loglevel "quiet" \
"${SAMPLES_DIR}/${FNAME}.wav" \
2>/dev/null
if test 0 == $?
then
echo "Done: ${FNAME}.wav"
else
echo "There was an error with ${FNAME}.wav"
fi
done
}
if ! command -v youtube-dl 1>/dev/null
then
echo "Please install youtube-dl first!" 1>&2
exit 1
fi
USAGE="Usage: $(basename "$0") [-h] [-s samples_file] [-o output_directory]"
while getopts ":s:o:h" opt; do
case ${opt} in
h)
echo "$USAGE"
exit 0
;;
s)
SAMPLES=$OPTARG
;;
o)
SAMPLES_DIR=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" 1>&2
echo "$USAGE" 1>&2
exit 1
;;
:)
echo "Invalid option: -$OPTARG requires an argument" 1>&2
echo "$USAGE" 1>&2
exit 1
;;
esac
done
shift $((OPTIND-1))
SAMPLES=${SAMPLES:-~/samples}
SAMPLES_DIR=${SAMPLES_DIR:-~/samples.out}
mkdir -p "$SAMPLES_DIR"
parse $SAMPLES \
| skip_collected \
| append_video_id \
| download_audio \
| crop_sample