-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.php
492 lines (381 loc) · 13.5 KB
/
search.php
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
<?php
error_reporting( E_ALL );
set_time_limit( 0 );
require "dropseeker.php";
$default_options = default_options();
$default_episode_dir = __DIR__ . '/episodes/';
$default_transcript_dir = __DIR__ . '/transcripts/';
$options = getopt( '', array(
'after:',
'before:',
'context:',
'context_exclude:',
'episode_dir:',
'extract',
'help',
'limit:',
'limit_per_episode:',
'match:',
'min_duration:',
'output_dir:',
'prefix_words:',
'podcast:',
'search:',
'suffix_words:',
'transcript_dir:',
) );
$options = array_merge( $default_options, $options );
if ( isset( $options['help'] ) ) {
usage();
die;
}
if ( ! isset( $options['search'] ) ) {
$options['search'] = array();
} else if ( ! is_array( $options['search'] ) ) {
$options['search'] = array( $options['search'] );
}
$options['search'] = array_unique( $options['search'] );
$options['search'] = array_filter( $options['search'] );
if ( empty( $options['podcast'] ) ) {
$options['podcast'] = array();
}
if ( isset( $options['match'] ) ) {
if ( ! is_array( $options['match'] ) ) {
$options['match'] = array( $options['match'] );
}
}
if ( ! isset( $options['before'] ) ) {
$options['before'] = .1;
}
else {
$options['before'] = floatval( $options['before'] );
}
if ( ! isset( $options['after'] ) ) {
$options['after'] = .1;
}
else {
$options['after'] = floatval( $options['after'] );
}
if ( empty( $options['output_dir'] ) ) {
$options['output_dir'] = 'search-results/';
}
if ( isset( $options['episode_dir'] ) ) {
$episode_dir = $options['episode_dir'];
if ( substr( $episode_dir, 0, 1 ) != '/' ) {
$episode_dir = getcwd() . '/' . $episode_dir;
}
} else {
$episode_dir = $default_episode_dir;
}
$episode_dir = rtrim( $episode_dir, '/' ) . '/';
if ( isset( $options['transcript_dir'] ) ) {
$transcript_dir = $options['transcript_dir'];
if ( substr( $transcript_dir, 0, 1 ) != '/' ) {
$transcript_dir = getcwd() . '/' . $transcript_dir;
}
} else {
$transcript_dir = $default_transcript_dir;
}
$transcript_dir = rtrim( $transcript_dir, '/' ) . '/';
if ( ! isset( $options['prefix_words'] ) ) {
$options['prefix_words'] = 5;
}
if ( ! isset( $options['suffix_words'] ) ) {
$options['suffix_words'] = 15;
}
if ( isset( $options['context_exclude'] ) ) {
if ( ! is_array( $options['context_exclude'] ) ) {
$options['context_exclude'] = array( $options['context_exclude'] );
}
}
if ( isset( $options['context'] ) ) {
if ( ! is_array( $options['context'] ) ) {
$options['context'] = array( $options['context'] );
}
}
if ( isset( $options['min_duration'] ) ) {
if ( is_array( $options['min_duration'] ) ) {
$options['min_duration'] = array_pop( $options['min_duration'] );
}
}
if ( empty( $options['search'] ) ) {
usage();
die( "You must supply at least one search term.\n\n" );
}
foreach ( array( "before", "after", "output_dir", "file" ) as $arg ) {
if ( isset( $options[ $arg ] ) && is_array( $options[ $arg ] ) ) {
$options[ $arg ] = array_pop( $options[ $arg ] );
}
}
foreach ( $options as $arg => $val ) {
if ( is_array( $val ) ) {
$val = array_map( 'trim', $val );
} else {
$val = trim( $val );
}
$options[ $arg ] = $val;
}
$options['output_dir'] = trim( $options['output_dir'] );
$options['output_dir'] = preg_replace( '/^~/', $_SERVER['HOME'], $options['output_dir'] );
$options['output_dir'] = rtrim( $options['output_dir'], '/' ) . '/';
if ( ! file_exists( $options['output_dir'] ) ) {
mkdir( $options['output_dir'] );
}
if ( ! file_exists( $options['output_dir'] ) ) {
die( "Could not create directory: " . $options['output_dir'] . "\n" );
}
if ( ! is_array( $options['podcast'] ) ) {
$options['podcast'] = array( $options['podcast'] );
}
$all_podcast_dirs = glob( $transcript_dir . "*" );
$matching_transcripts = array();
foreach ( $all_podcast_dirs as $podcast_dir ) {
$podcast_title = substr( $podcast_dir, strlen( $transcript_dir ) );
if ( empty( $options['podcast'] ) ) {
$matching_transcripts = array_merge( $matching_transcripts, glob( $podcast_dir . "/*.vtt" ) );
} else {
foreach ( $options['podcast'] as $podcast_title_pattern ) {
if ( stripos( $podcast_title, $podcast_title_pattern ) !== false ) {
$matching_transcripts = array_merge( $matching_transcripts, glob( $podcast_dir . "/*.vtt" ) );
}
}
}
}
sort( $matching_transcripts );
$matching_transcripts = array_reverse( $matching_transcripts );
$transcripts = array();
foreach ( $matching_transcripts as $transcript ) {
$filename = basename( $transcript );
if ( isset( $options['match'] ) ) {
foreach ( $options['match'] as $match ) {
if ( false !== stripos( $filename, $match ) ) {
$transcripts[] = $transcript;
continue 2;
}
}
}
else {
$transcripts[] = $transcript;
}
}
$last_start_time = '0:00.000';
$last_end_time = '0:00.000';
$matches_found = 0;
foreach ( $transcripts as $transcript_file ) {
$matches_found_in_episode = 0;
$parsed_transcript = array();
$lines = file( $transcript_file );
$lines = array_map( 'trim', $lines );
foreach ( $lines as $line ) {
if ( strpos( $line, 'WEBVTT' ) === 0 ) {
continue;
}
if ( empty( $line ) ) {
continue;
}
if ( preg_match( '/^((?:[0-9]+:)*[0-9]+\.[0-9]{3}) --> ((?:[0-9]+:)*[0-9]+\.[0-9]{3})$/', $line, $m ) ) {
$last_start_time = $m[1];
$last_end_time = $m[2];
} else {
$words = preg_split( '/\s/', $line );
foreach ( $words as $word ) {
$parsed_transcript[] = array(
$word,
preg_replace( '/[^a-z0-9\#]/', '', strtolower( $word ) ),
$last_start_time,
$last_end_time,
);
}
}
}
foreach ( $options['search'] as $search_term ) {
$search_term = strtolower( $search_term );
$suffix_word_count = $options['suffix_words'] + substr_count( $search_term, ' ' );
$keywords = explode( " ", $search_term );
$num_words = count( $parsed_transcript );
$suffix_words = array();
$prefix_words = array();
for ( $i = 0; $i < min( $num_words, $suffix_word_count ); $i++ ) {
$suffix_words[] = $parsed_transcript[ $i ];
}
$start = INF;
$end = 0;
foreach ( $parsed_transcript as $idx => $word_entry ) {
$prefix_words[] = $word_entry[0];
if ( $num_words > $idx + $suffix_word_count ) {
$suffix_words[] = $parsed_transcript[ $idx + $suffix_word_count ];
}
array_shift( $suffix_words );
if ( count( $prefix_words ) > $options['prefix_words'] + 1) {
array_shift( $prefix_words );
}
if ( matches_search_term( $word_entry[1], $keywords[0] ) ) {
$start = $word_entry[2];
$end = $word_entry[3];
if ( count( $keywords ) > 1 ) {
if ( count( $suffix_words ) < count( $keywords ) - 1 ) {
break;
}
for ( $k = 1; $k < count( $keywords ); $k++ ) {
if ( ! matches_search_term( $suffix_words[ $k - 1 ][1], $keywords[ $k ] ) ) {
continue 2;
}
$end = $suffix_words[ $k - 1 ][3];
}
}
$suffix_string = '';
foreach ( $suffix_words as $suffix_word ) { $suffix_string .= $suffix_word[0] . ' '; }
$exclusion_search_string = join( " ", $prefix_words ) . " " . trim( $suffix_string );
if ( ! empty( $options['context_exclude'] ) ) {
foreach ( $options['context_exclude'] as $exclude_option ) {
if ( false !== stripos( $exclusion_search_string, $exclude_option ) ) {
continue 2;
}
}
}
if ( ! empty( $options['context'] ) ) {
foreach ( $options['context'] as $context_includes ) {
if ( false === stripos( $exclusion_search_string, $context_includes ) ) {
continue 2;
}
}
}
$start_in_seconds = 0;
$start_parts = explode( ":", $start );
if ( count( $start_parts ) < 3 ) {
array_unshift( $start_parts, '0' );
}
if ( count( $start_parts ) < 3 ) {
array_unshift( $start_parts, '0' );
}
$timestamp_in_filename = '';
for ( $i = 0, $len = count( $start_parts ); $i < $len; $i++ ) {
$start_part = array_pop( $start_parts );
$start_in_seconds += $start_part * ( pow( 60, $i ) );
// Use a 01h2m3s timestamp in the filename to avoid using :, which shows up as / on Mac.
if ( $i == 0 ) {
$timestamp_in_filename = str_pad( round( $start_part ), 2, '0', STR_PAD_LEFT ) . "s";
} else if ( $i == 1 ) {
$timestamp_in_filename = str_pad( round( $start_part ), 2, '0', STR_PAD_LEFT ) . "m" . $timestamp_in_filename;
} else if ( $i == 2 ) {
$timestamp_in_filename = str_pad( round( $start_part ), 2, '0', STR_PAD_LEFT ) . "h" . $timestamp_in_filename;
}
}
$end_in_seconds = 0;
$end_parts = explode( ":", $end );
for ( $i = 0, $len = count( $end_parts ); $i < $len; $i++ ) {
$end_in_seconds += array_pop( $end_parts ) * ( pow( 60, $i ) );
}
if ( ! empty( $options['min_duration'] ) ) {
if ( $end_in_seconds - $start_in_seconds < $options['min_duration'] ) {
continue;
} else {
echo "Duration: " . ( $end_in_seconds - $start_in_seconds ) . " seconds\n";
}
}
$matches_found_in_episode++;
$matches_found++;
echo preg_replace( '/\s\(guid.*$/', '', str_replace( $transcript_dir, '', $transcript_file ) ) . " @ " . $start . ":\n\t" . $exclusion_search_string . "\n";
if ( isset( $options['extract'] ) ) {
preg_match_all( '/\(guid=(.+)\)/', $transcript_file, $m );
$guid = $m[1][0];
if ( ! $guid ) {
die( "Could not extract guid from " . $transcript_file . "\n" );
}
$audio_files = array();
foreach ( $options['podcast'] as $podcast ) {
$audio_files = array_merge( $audio_files, glob( $episode_dir . "*" . $podcast . "*/*guid=" . $guid . "*.*" ) );
}
if ( empty( $audio_files ) ) {
die( "Could not find audio for " . $transcript_file . "\n" );
}
$audio_file = $audio_files[0];
shell_exec(
"ffmpeg -hide_banner -loglevel error -y -ss "
. escapeshellarg( floatval( $start_in_seconds - $options['before'] ) )
. " -t "
. ( $end_in_seconds - $start_in_seconds + $options['before'] + $options['after'] )
. " -i " . escapeshellarg( $audio_file )
. " "
. escapeshellarg(
rtrim( $options['output_dir'], '/' )
. '/' . substr( $search_term
. " - " . basename( $audio_file ), 0, 200 )
. " - " . $timestamp_in_filename . ".aif"
)
);
}
if ( isset( $options['limit'] ) && $matches_found == $options['limit'] ) {
break 3;
}
if ( isset( $options['limit_per_episode'] ) && $matches_found_in_episode == $options['limit_per_episode'] ) {
continue 2;
}
}
}
}
}
function seconds_to_minutes( $seconds ) {
$string = floor( $seconds / ( 60 * 60 ) );
$seconds = intval( $seconds ) % ( 60 * 60 );
$string .= ':' . str_pad( floor( $seconds / 60 ), 2, "0", STR_PAD_LEFT );
$seconds = $seconds % 60;
$string .= ':' . str_pad( $seconds, 2, "0", STR_PAD_LEFT );
return $string;
}
function matches_search_term( $word, $search_term ) {
$search_term = preg_replace( '/[^a-z0-9\*\#]/', '', strtolower( $search_term ) );
if ( $word === $search_term ) {
return true;
}
if ( '*' === $search_term ) {
return true;
}
if ( '.' === $search_term && strlen( $word ) == 1 ) {
return true;
}
if ( strpos( $search_term, '*' ) !== false ) {
$last_match_end = 0;
$parts = explode( "*", $search_term );
foreach ( $parts as $idx => $part ) {
$match_location = strpos( $word, $part, $last_match_end );
if ( $match_location === false ) {
return false;
} else if ( $idx === 0 && $match_location > 0 ) {
return false;
} else {
$last_match_end = $match_location + strlen( $part );
}
}
if ( $last_match_end == strlen( $word ) || $part === '' ) {
return true;
}
}
return false;
}
function usage() {
echo "Usage: php " . $_SERVER['PHP_SELF'] . " --search [search term]\n";
echo "\n";
echo "Required arguments:
--search [string] What to search for in transcripts. Supports wildcards like 'foo*' (words that start
with foo), 'foo * bar' ('foo' and 'bar' separated by one word), or 'foo*baz*bar' (any
word starting with 'foo', containing 'baz', and ending with 'bar').
Optional arguments:
--after [float] Extract an additional __ seconds from after each match.
--before [float] Extract an additional __ seconds from before each match.
--episode_dir [path] The directory in which the episode directories are stored, if not in the default location.
--extract Extract audio clips of each match.
--context [string] Only consider a match if the full prefix + match + suffix also includes this string.
--context_exclude [string] A search string that, if it matches text around the search result, will be excluded from the final results.
--help Show the usage instructions.
--output_dir [path] The directory in which to store the extracted audio clips.
--limit [int] Stop searching entirely after finding this many total matches.
--limit_per_episode [int] Stop searching an episode after finding this many matches in it.
--match [string] Only check episodes that include this string in their filename.
--min_duration [float] If extracting audio, only extract a clip if it will be at least this long.
--podcast [string] Only search transcripts from podcasts that include this string in their title.
--prefix_words [int] Show this many words before the matching string in the text search results.
--suffix_words [int] Show this many words after the matching string in the text search results.
--transcript_dir [path] The directory in which the transcript directories are stored, if not in the default location.
";
}