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
|
# File 'lib/video_transcoding/media.rb', line 49
def info
return @info unless @info.nil?
@info = {}
if @title.nil?
if @scan =~ /\+ title ([0-9]+):\r?\n \+ Main Feature/m
@title = $1.to_i
elsif @scan =~ /^\+ title ([0-9]+):/
@title = $1.to_i
else
@title = 1
end
@scan = Media.scan(@path, @title, @previews)
end
if @scan =~ / libhb: scan thread found ([0-9]+) valid title/
fail 'multiple titles found' if $1.to_i > 1
end
@info[:title] = @title
@info[:size] = @stat.size
@info[:directory] = @stat.directory?
unless @scan =~ /^ \+ duration: ([0-9]{2}):([0-9]{2}):([0-9]{2})/
fail 'media duration not found'
end
@info[:duration] = ($1.to_i * 60 * 60) + ($2.to_i * 60) + $3.to_i
unless @scan =~ / scan: [0-9]+ previews, ([0-9]+)x([0-9]+), ([0-9.]+) fps, autocrop = ([0-9]+)\/([0-9]+)\/([0-9]+)\/([0-9]+), aspect [0-9.]+:[0-9.]+, PAR [0-9]+:[0-9]+/
fail 'video information not found'
end
@info[:width] = $1.to_i
@info[:height] = $2.to_i
@info[:fps] = $3.to_f
if @previews > 2
@info[:autocrop] = {:top => $4.to_i, :bottom => $5.to_i, :left => $6.to_i, :right => $7.to_i}
else
@info[:autocrop] = nil
end
return @info unless @extended
unless @scan =~ / \+ audio tracks:\r?\n(.*) \+ subtitle tracks:\r?\n(.*)HandBrake has exited./m
fail 'audio and subtitle information not found'
end
audio = $1
subtitle = $2
@info[:audio] = {}
@info[:subtitle] = {}
audio.gsub(/\r/, '').each_line do |line|
if line =~ /^ \+ ([0-9]+), [^(]+\(([^)]+)\) .*\(([0-9.]+) ch\) .*\(iso639-2: ([a-z]{3})\)/
track = $1.to_i
track_info = {}
track_info[:format] = $2
track_info[:channels] = $3.to_f
track_info[:language] = $4
if line =~ /([0-9]+)bps/
track_info[:bps] = $1.to_i
else
track_info[:bps] = nil
end
@info[:audio][track] = track_info
end
end
subtitle.gsub(/\r/, '').each_line do |line|
if line =~ /^ \+ ([0-9]+), .*\(iso639-2: ([a-z]{3})\) \((Text|Bitmap)\)\(([^)]+)\)/
track = $1.to_i
track_info = {}
track_info[:language] = $2
track_info[:format] = $3
track_info[:encoding] = $4
@info[:subtitle][track] = track_info
end
end
@info[:h264] = false
@info[:hevc] = false
@info[:mpeg2] = false
audio_track = 0
subtitle_track = 0
@scan.each_line do |line|
if line =~ /[ ]+Stream #0[.:]([0-9]+)[^ ]*: (Video|Audio|Subtitle): (.*)/
stream = $1.to_i
type = $2
attributes = $3
case type
when 'Video'
unless @info.has_key? :stream
@info[:stream] = stream
if attributes =~ /^h264/
@info[:h264] = true
elsif attributes =~ /^hevc/
@info[:hevc] = true
elsif attributes =~ /^mpeg2video/
@info[:mpeg2] = true
end
end
when 'Audio'
audio_track += 1
if @info[:audio].has_key? audio_track
track_info = @info[:audio][audio_track]
track_info[:stream] = stream
if @scan =~ /[ ]+Stream #0[.:]#{stream}[^ ]*: Audio: [^\n]+(?:\n[^\n]+)?\(default\)/m
track_info[:default] = true
else
track_info[:default] = false
end
if @scan =~ /[ ]+Stream #0[.:]#{stream}[^ ]*: Audio: [^\n]+\n(?:[^\n]+\n)?[ ]+Metadata:\r?\n^[ ]+title[ ]+: ([^\r\n]+)/m
track_info[:name] = $1
else
track_info[:name] = nil
end
end
when 'Subtitle'
subtitle_track += 1
if @info[:subtitle].has_key? subtitle_track
track_info = @info[:subtitle][subtitle_track]
track_info[:stream] = stream
if attributes =~ /\(default\)/
track_info[:default] = true
else
track_info[:default] = false
end
if attributes =~ /\(forced\)/
track_info[:forced] = true
else
track_info[:forced] = false
end
if @scan =~ /[ ]+Stream #0[.:]#{stream}[^ ]*: Subtitle: [^\n]+\n(?:[^\n]+\n)?[ ]+Metadata:\r?\n^[ ]+title[ ]+: ([^\r\n]+)/m
track_info[:name] = $1
else
track_info[:name] = nil
end
end
end
end
end
@info[:bd] = false
@info[:dvd] = false
if @scan =~ / scan: (BD|DVD) has [0-9]+ title/
@info[$1.downcase.to_sym] = true
@info[:mpeg2] = true if @info[:dvd]
end
if @scan =~ /Input #0, matroska/
@info[:mkv] = true
else
@info[:mkv] = false
end
if @scan =~ /Input #0, mov,mp4/
@info[:mp4] = true
else
@info[:mp4] = false
end
if @info[:mp4]
@mp4_scan = Media.scan_mp4(@path)
types = []
flags = []
names = []
@mp4_scan.each_line do |line|
if line =~ /^[ ]+type[ ]+=[ ]+(.*)$/
types << $1
elsif line =~ /^[ ]+enabled[ ]+=[ ]+(.*)$/
if $1 == 'true'
flags << true
else
flags << false
end
elsif line =~ /^[ ]+userDataName[ ]+=[ ]+(.*)$/
if $1 == '<absent>'
names << nil
else
names << $1
end
end
end
index = 0
audio_track = 0
subtitle_track = 0
types.each do |type|
case type
when 'audio'
audio_track += 1
if @info[:audio].has_key? audio_track
track_info = @info[:audio][audio_track]
track_info[:default] = flags[index]
track_info[:name] = names[index]
end
when '(sbtl)', '(subp)', 'text'
subtitle_track += 1
if @info[:subtitle].has_key? subtitle_track
track_info = @info[:subtitle][subtitle_track]
track_info[:default] = flags[index]
track_info[:forced] = flags[index]
track_info[:name] = names[index]
end
end
index += 1
end
if subtitle_track > 0 and @scan =~ /[ ]+Chapter #0[.:]0: start /
@info[:subtitle].delete subtitle_track
end
end
@info
end
|