@@ -92,14 +92,14 @@ def create(input_url, options = {})
9292 new_encode . input . duration = fixed_duration ( working_path ( "duration_input_metadata" , new_encode . id ) )
9393 end
9494
95- options [ : subtitle_count] = new_encode . input . subtitle_count if new_encode . input . subtitle_count &.positive?
95+ subtitle_count = new_encode . input . subtitle_count if new_encode . input . subtitle_count &.positive?
9696
9797 new_encode . state = :running
9898 new_encode . percent_complete = 1
9999 new_encode . errors = [ ]
100100
101101 # Run the ffmpeg command and save its pid
102- command = ffmpeg_command ( input_url , new_encode . id , options )
102+ command = ffmpeg_command ( input_url , new_encode . id , options , subtitle_count )
103103 # Capture the exit status in a file in order to differentiate warning output in stderr between real process failure
104104 exit_status_file = working_path ( "exit_status.code" , new_encode . id )
105105 command = "#{ command } ; echo $? > #{ exit_status_file } "
@@ -264,21 +264,32 @@ def build_supplemental_outputs(encode)
264264 file . id = "#{ encode . input . id } -#{ File . basename ( file_path ) } "
265265 file . created_at = encode . created_at
266266 file . updated_at = File . mtime file_path
267+ # TODO: Add handling for label and language if they are included in stream's metadata
268+ # file.label = tech_metadata
269+ # file.language = tech_metadata
270+
271+ # Subtitle metadata has to be pulled from input because subtitle tracks
272+ # are not encoded into outputs.
273+ metadata_path = working_path ( "input_metadata" , encode . id )
274+ subtitle_track = /(?:caption)(\d )(?:\. vtt)/ . match ( file_path ) [ 1 ] . to_i
275+ file . assign_subtitle_tech_metadata ( get_subtitle_tech_metadata ( metadata_path , subtitle_track ) )
267276
268277 files << file
269278 end
270279
271280 files
272281 end
273282
274- def ffmpeg_command ( input_url , id , opts )
283+ def ffmpeg_command ( input_url , id , opts , subtitle_count = nil )
275284 sanitized_filename = ActiveEncode . sanitize_base input_url
276285 output_opt = opts [ :outputs ] . collect do |output |
277286 file_name = "outputs/#{ sanitized_filename } -#{ output [ :label ] } .#{ output [ :extension ] } "
278287 " #{ output [ :ffmpeg_opt ] } #{ working_path ( file_name , id ) } "
279288 end . join ( " " )
280289
281- supplemental_file_opt = caption_extraction_options ( sanitized_filename , opts [ :subtitle_count ] , id ) if opts [ :subtitle_count ] &.positive?
290+ supplemental_file_opt = if opts [ :extract_subtitles ] && subtitle_count . present?
291+ caption_extraction_options ( sanitized_filename , subtitle_count , id )
292+ end
282293
283294 header_opt = Array ( opts [ :headers ] ) . map do |k , v |
284295 "#{ k } : #{ v } \r \n "
@@ -365,6 +376,20 @@ def get_xpath_text(doc, xpath, cast_method)
365376 doc . xpath ( xpath ) . first &.text &.send ( cast_method )
366377 end
367378
379+ def get_subtitle_tech_metadata ( file_path , track_number )
380+ doc = Nokogiri ::XML File . read ( file_path )
381+ doc . remove_namespaces!
382+
383+ # XPath starts counting from 1, not 0
384+ {
385+ format : get_xpath_text ( doc , "//track[@type='Text'][#{ track_number + 1 } ]/Format/text()" , :to_s ) ,
386+ codec : get_xpath_text ( doc , "//track[@type='Text'][#{ track_number + 1 } ]/CodecID/text()" , :to_s )
387+ # duration: ,
388+ # language: ,
389+ # label:
390+ }
391+ end
392+
368393 def fixed_duration ( path )
369394 get_tech_metadata ( path ) [ :duration ]
370395 end
0 commit comments