Class: VttService
- Inherits:
-
Object
- Object
- VttService
- Defined in:
- app/services/vtt_service.rb
Overview
Service class for handling VTT (WebVTT) generation and transcript processing
Constant Summary collapse
- SECONDS_PER_HOUR =
Constants for time calculations
3600- SECONDS_PER_MINUTE =
60- MILLISECONDS_PER_SECOND =
1000
Class Method Summary collapse
-
.format_timestamp(milliseconds) ⇒ String
Format milliseconds to VTT timestamp format (HH:MM:SS.mmm).
-
.generate_sentences_content(sentences_data) ⇒ String
Generate sentences content from structured sentences data.
-
.generate_sentences_filename(title) ⇒ String
Generate sentences filename based on video title.
-
.generate_transcript_content(paragraphs) ⇒ String
Generate transcript content from paragraphs data.
-
.generate_vtt_content(vtt_data) ⇒ String
Generate VTT content from structured transcript data.
-
.generate_vtt_filename(title, type = 'polished') ⇒ String
Generate VTT filename based on video title and type.
Class Method Details
.format_timestamp(milliseconds) ⇒ String
Format milliseconds to VTT timestamp format (HH:MM:SS.mmm)
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/services/vtt_service.rb', line 31 def self.(milliseconds) return '00:00:00.000' if milliseconds.nil? total_seconds = milliseconds / 1000.0 hours = (total_seconds / SECONDS_PER_HOUR).to_i minutes = ((total_seconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE).to_i seconds = (total_seconds % SECONDS_PER_MINUTE).to_i milliseconds_part = ((total_seconds % 1) * MILLISECONDS_PER_SECOND).to_i format('%<hours>02d:%<minutes>02d:%<seconds>02d.%<milliseconds>03d', hours: hours, minutes: minutes, seconds: seconds, milliseconds: milliseconds_part) end |
.generate_sentences_content(sentences_data) ⇒ String
Generate sentences content from structured sentences data
47 48 49 50 51 52 53 54 55 |
# File 'app/services/vtt_service.rb', line 47 def self.generate_sentences_content(sentences_data) return '' if sentences_data.blank? || sentences_data['sentences'].blank? sentences_data['sentences'].map.with_index do |sentence, index| start_time = (sentence['start']) end_time = (sentence['end']) "Sentence #{index + 1} (#{start_time} --> #{end_time}):\n#{sentence['text']}" end.join("\n\n") end |
.generate_sentences_filename(title) ⇒ String
Generate sentences filename based on video title
79 80 81 |
# File 'app/services/vtt_service.rb', line 79 def self.generate_sentences_filename(title) "#{title.parameterize}-sentences.txt" end |
.generate_transcript_content(paragraphs) ⇒ String
Generate transcript content from paragraphs data
60 61 62 63 64 65 66 |
# File 'app/services/vtt_service.rb', line 60 def self.generate_transcript_content(paragraphs) return '' if paragraphs.blank? paragraphs.map.with_index do |paragraph, index| "Paragraph #{index + 1}:\n#{paragraph['text']}" end.join("\n\n") end |
.generate_vtt_content(vtt_data) ⇒ String
Generate VTT content from structured transcript data
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/services/vtt_service.rb', line 13 def self.generate_vtt_content(vtt_data) return '' if vtt_data.blank? vtt_lines = ['WEBVTT', ''] vtt_data.each_with_index do |caption, index| vtt_lines << (index + 1).to_s vtt_lines << "#{(caption['start_time'])} --> #{(caption['end_time'])}" vtt_lines << caption['text'] vtt_lines << '' end vtt_lines.join("\n") end |
.generate_vtt_filename(title, type = 'polished') ⇒ String
Generate VTT filename based on video title and type
72 73 74 |
# File 'app/services/vtt_service.rb', line 72 def self.generate_vtt_filename(title, type = 'polished') "#{title.parameterize}-#{type}-captions.vtt" end |