forked from makandra/validate-hls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-hls.rb
More file actions
executable file
·375 lines (299 loc) · 7.45 KB
/
validate-hls.rb
File metadata and controls
executable file
·375 lines (299 loc) · 7.45 KB
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
#!/usr/bin/env ruby
module ValidateHls
class Error < StandardError; end
class CommandFailed < Error; end
class DownloadFailed < Error; end
class Invalid < Error; end
class InvalidChild < Error; end
class MissingDependency < Error; end
module Util
require 'open3'
def run(command, *args)
stdout_str, error_str, status = Open3.capture3(command, *args)
if status.success?
stdout_str
else
raise CommandFailed, "Error running #{command} #{args.inspect}:\n\n#{error_str}"
end
end
def download(url)
run 'wget', url
rescue CommandFailed => e
raise DownloadFailed, e.message
end
end
module WithinTempDir
require 'tmpdir'
def temp_dir
@temp_dir ||= Dir.mktmpdir
end
def within_temp_dir(&block)
Dir.chdir(temp_dir, &block)
end
end
class Resource
include WithinTempDir
include Util
def initialize(url, log)
@url = url
@log = log
end
attr_reader :url, :log
def to_s
name = self.class.name.split('::').last
"#{name}(#{url})"
end
private
def filename
File.basename(@url)
end
def local_path
File.join(temp_dir, filename)
end
def download
within_temp_dir do
run 'wget', url
log.positive_message('Downloadable with 200 OK')
end
end
def data
within_temp_dir do
File.read(filename)
end
end
def parent_url
File.dirname(url)
end
def full_url(url_or_path)
if url_or_path.include?('://')
url_or_path
else
File.join(parent_url, url_or_path)
end
end
end
class Playlist < Resource
def validate!
log.subject_started(self)
download
parse_urls
validate_children
log.subject_passed(self)
rescue Error => e
log.negative_message(e.message)
log.subject_failed(self)
raise Invalid, e.message
end
private
attr_reader :playlist_urls, :fragment_urls
def validate_children
child_error = false
playlist_urls.each do |playlist_url|
begin
playlist = Playlist.new(playlist_url, log)
playlist.validate!
rescue Error => e
child_error = true
end
end
fragment_urls.each do |fragment_url|
begin
fragment = Fragment.new(fragment_url, log)
fragment.validate!
rescue Error => e
child_error = true
end
end
if child_error
# e.message was already printed by child, so just explain that we're failing
# because of a child failure
raise Invalid, 'Error in child resource'
end
end
def parse_urls
@fragment_urls = []
@playlist_urls = []
lines = data.split(/\n/)
lines.each do |line|
line = line.strip
if line.end_with?('.ts')
@fragment_urls << full_url(line)
end
if line.end_with?('.m3u8')
@playlist_urls << full_url(line)
end
end
if playlist_urls.size == 0 && fragment_urls.size == 0
raise Invalid, 'No URLs found in playlist'
end
end
end
class Fragment < Resource
def validate!
log.subject_started(self)
download
validate_frames
log.subject_passed(self)
rescue Error => e
log.negative_message(e.message)
log.subject_failed(self)
raise Invalid, e.message
end
private
def validate_frames
ffprobe_out = run('ffprobe', '-select_streams', 'v:0', '-show_frames', local_path)
keyframe_lines = ffprobe_out.scan(/key_frame=\d/)
if keyframe_lines.size == 0
raise Invalid, "No frames found"
elsif !keyframe_lines.include?('key_frame=1')
raise Invalid, "No keyframes found in any frame"
elsif keyframe_lines[0] != 'key_frame=1'
raise Invalid, "Keyframe is not the first frame"
else
log.positive_message 'Keyframe is first frame'
end
rescue CommandFailed => e
raise Invalid, "Keyframe analysis failed: #{e.message}"
end
end
module Dependencies
include Util
extend self
def check
# Check dependencies
begin
run 'wget', '--help'
rescue CommandFailed
raise MissingDependency, "No wget installed"
end
begin
run 'ffprobe', '-h'
rescue CommandFailed
raise MissingDependency, "No ffprobe installed"
end
end
end
class Log
COLOR_HEAD = "\e[44;97m"
COLOR_WARNING = "\e[33m"
COLOR_POSITIVE = "\e[32m"
COLOR_NEGATIVE = "\e[31m"
COLOR_RESET = "\e[0m"
def initialize
@target = STDOUT
@indent_level = 0
@success = true
end
def subject_started(subject)
puts "Validating: #{subject}"
@indent_level += 1
end
def subject_passed(subject)
# positive_message "Passed"
@indent_level -= 1
end
def subject_failed(subject)
# negative_message "Failed"
@indent_level -= 1
end
def head(message)
puts message, COLOR_HEAD
end
def positive_message(message)
puts "✔ #{message}", COLOR_POSITIVE
end
def negative_message(message)
@success = false
puts "✘ #{message}", COLOR_NEGATIVE
end
def puts(string = '', color = nil)
lines = string.strip.split(/\n/)
lines = [''] if lines.size == 0
indent_string = "| " * @indent_level
lines.each do |line|
@target.print indent_string
@target.print color if color # don't colorize background for the indentation
@target.print line
@target.print COLOR_RESET if color
@target.print "\n"
end
end
def success?
@success
end
end
class PlaylistSet
def initialize(urls, log)
@urls = urls
@log = log
end
attr_reader :urls, :log
def validate!
log.subject_started(self)
validate_children
log.subject_passed(self)
rescue Error => e
log.negative_message(e.message)
log.subject_failed(self)
raise Invalid, e.message
end
def to_s
"Set of #{urls.size} URL(s)"
end
private
def validate_children
child_error = false
@urls.each do |url|
begin
playlist = Playlist.new(url, @log)
playlist.validate!
rescue Error => e
child_error = true
end
end
if child_error
# e.message was already printed by child, so just explain that we're failing
# because of a child failure
raise Invalid, 'One or more playlists had errors'
end
end
end
class Validator
def initialize(urls)
@urls = Array[*urls] # Array.wrap without ActiveSupport
@log = Log.new
end
attr_reader :urls, :log
def run
print_banner
check_urls
check_dependencies
validate!
error_code = @log.success? ? 1 : 0
exit error_code
rescue Error => e
log.negative_message "Validation failed: #{e.message}"
exit 1
end
private
def validate!
set = PlaylistSet.new(urls, log)
set.validate!
end
def check_dependencies
Dependencies.check
end
def check_urls
unless @urls && @urls.size > 0
raise Error, "Must pass one or more URLs to .m3u8 playlists as arguments"
end
end
def print_banner
log.puts
log.head "validate-hls"
log.puts
end
end
end
validator = ValidateHls::Validator.new(ARGV)
validator.run