Class: VttService

Inherits:
Object
  • Object
show all
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

Class Method Details

.format_timestamp(milliseconds) ⇒ String

Format milliseconds to VTT timestamp format (HH:MM:SS.mmm)

Parameters:

  • milliseconds (Integer, Float, nil)

    Time in milliseconds

Returns:

  • (String)

    Formatted timestamp string



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/vtt_service.rb', line 31

def self.format_timestamp(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

Parameters:

  • sentences_data (Hash)

    Hash containing sentences array with start, end, and text

Returns:

  • (String)

    Formatted sentences content



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 = format_timestamp(sentence['start'])
    end_time = format_timestamp(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

Parameters:

  • title (String)

    Video title

Returns:

  • (String)

    Formatted filename



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

Parameters:

  • paragraphs (Array<Hash>)

    Array of paragraph hashes with text

Returns:

  • (String)

    Formatted transcript content



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

Parameters:

  • vtt_data (Array<Hash>)

    Array of caption hashes with start_time, end_time, and text

Returns:

  • (String)

    Formatted VTT content



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 << "#{format_timestamp(caption['start_time'])} --> #{format_timestamp(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

Parameters:

  • title (String)

    Video title

  • type (String) (defaults to: 'polished')

    VTT type (e.g., 'original', 'polished')

Returns:

  • (String)

    Formatted filename



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