Class: Appydave::Tools::SubtitleProcessor::Transcript

Inherits:
Object
  • Object
show all
Defined in:
lib/appydave/tools/subtitle_processor/transcript.rb

Overview

Convert SRT to plain text transcript Strips timestamps and indices, keeping only the spoken text

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path: nil, srt_content: nil) ⇒ Transcript



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/appydave/tools/subtitle_processor/transcript.rb', line 11

def initialize(file_path: nil, srt_content: nil)
  if file_path && srt_content
    raise ArgumentError, 'You cannot provide both a file path and an SRT content stream.'
  elsif file_path.nil? && srt_content.nil?
    raise ArgumentError, 'You must provide either a file path or an SRT content stream.'
  end

  @content = if file_path
               File.read(file_path, encoding: 'UTF-8')
             else
               srt_content
             end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/appydave/tools/subtitle_processor/transcript.rb', line 9

def content
  @content
end

Instance Method Details

#extract(paragraph_gap: 1) ⇒ String

Convert SRT to plain text transcript



28
29
30
31
32
33
34
# File 'lib/appydave/tools/subtitle_processor/transcript.rb', line 28

def extract(paragraph_gap: 1)
  parser = Join::SRTParser.new
  subtitles = parser.parse(@content)

  separator = "\n" * paragraph_gap
  subtitles.map(&:text).join(separator)
end

#write(output_file, paragraph_gap: 1) ⇒ Object

Write transcript to file



39
40
41
42
43
44
45
46
47
# File 'lib/appydave/tools/subtitle_processor/transcript.rb', line 39

def write(output_file, paragraph_gap: 1)
  transcript = extract(paragraph_gap: paragraph_gap)
  File.write(output_file, transcript, encoding: 'UTF-8')
  puts "Transcript written to #{output_file}"
rescue Errno::EACCES
  puts "Permission denied: Unable to write to #{output_file}"
rescue StandardError => e
  puts "An error occurred while writing to the file: #{e.message}"
end