Class: WebVTT::Smalt

Inherits:
Object
  • Object
show all
Defined in:
lib/vtt/smalt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f) ⇒ Smalt

Returns a new instance of Smalt.



11
12
13
14
15
16
# File 'lib/vtt/smalt.rb', line 11

def initialize(f)
	@file = File.new(f,'a+')
	@path = f	
     		@filename = File.basename(@path)
  	parse
end

Instance Attribute Details

#cue_parsedObject

Returns the value of attribute cue_parsed.



10
11
12
# File 'lib/vtt/smalt.rb', line 10

def cue_parsed
  @cue_parsed
end

#cuesObject (readonly)

Returns the value of attribute cues.



9
10
11
# File 'lib/vtt/smalt.rb', line 9

def cues
  @cues
end

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/vtt/smalt.rb', line 9

def filename
  @filename
end

#headerObject (readonly)

Returns the value of attribute header.



9
10
11
# File 'lib/vtt/smalt.rb', line 9

def header
  @header
end

#newObject (readonly)

Returns the value of attribute new.



9
10
11
# File 'lib/vtt/smalt.rb', line 9

def new
  @new
end

#oldObject (readonly)

Returns the value of attribute old.



9
10
11
# File 'lib/vtt/smalt.rb', line 9

def old
  @old
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/vtt/smalt.rb', line 9

def path
  @path
end

Instance Method Details

#actual_total_lengthObject

字幕持续时间



92
93
94
# File 'lib/vtt/smalt.rb', line 92

def actual_total_length
 		@cues.last.end_in_sec - @cues.first.start_in_sec
end

#delete(*args) ⇒ Object

删除字幕



45
46
47
48
49
50
51
52
53
# File 'lib/vtt/smalt.rb', line 45

def delete(*args)
	if @cues.reject! do |cue|
			cue.start.to_f == args[0] && cue.end.to_f == args[1]
		end
	else
		raise ArgumentError.new("This time does not exist or The arguments you give wrong")
	end
	save
end

#file_putsObject

文件内容保存



116
117
118
119
# File 'lib/vtt/smalt.rb', line 116

def file_puts
	@file.puts(@output)
	@file.close
end

#insert(*args) ⇒ Object

插入字幕



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/vtt/smalt.rb', line 56

def insert(*args)
	@start = args[0]
	@end = args[1]
	if time_check.empty?
		@new = Cue.parse([time_fix,args[-1]].join("\n").strip)
		i = -1
		begin
			i += 1
		end while !@cues[i].nil?&&@new.start.to_f > @cues[i].start.to_f 
		modify(i)
		save
	else
		raise ArgumentError.new("The arguments you give wrong or This period of time in the file already exists")
	end
end

#modify(a, b = 0) ⇒ Object

把新字幕写入cue



122
123
124
# File 'lib/vtt/smalt.rb', line 122

def modify(a,b = 0)
	@cues[a,b] = @new
end

#parseObject

初始化 读取文件信息 把文件内容写入cue 文件地址写入path 文件名写入filename



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vtt/smalt.rb', line 19

def parse
			content = File.read(@path)
 		if content.nil? ||content.empty? 
 			@output =  "WEBVTT"
 			file_puts
 		end
  @content = File.read(@path).gsub("\r\n", "\n")
  @content.gsub!("\uFEFF", '')
  cues = @content.split("\n\n")
  @header = cues.shift
  header_lines = @header.split("\n").map(&:strip)
  @header.gsub!("\n","")
  if (header_lines[0] =~ /^WEBVTT/).nil?
    raise MalformedFile, "Not a valid WebVTT file"
  end

  @cues = []
  cues.each do |cue|
    cue_parsed = Cue.parse(cue.strip)
    if !cue_parsed.text.nil?
      @cues << cue_parsed
    end
  end
end

#save(output = nil) ⇒ Object

文件输出 写入内容后输出文件



73
74
75
76
77
78
79
# File 'lib/vtt/smalt.rb', line 73

def save(output=nil)
 		output ||= @path.gsub(".srt", ".vtt")
 		File.open(output, "w") do |f|
   		f.write(to_webvtt)
 		end
 		return output
end

#time_checkObject

判断时间格式是否正确



105
106
107
108
109
110
111
112
113
# File 'lib/vtt/smalt.rb', line 105

def time_check
	if @end.to_f > @start.to_f 
		@cues.reject do|cue| 
			(cue.end.to_f >@start.to_f && cue.start.to_f > @end.to_f ) ||(cue.end.to_f < @start.to_f && cue.start.to_f < @end.to_f )
		end
	else
		raise ArgumentError.new("The arguments you give wrong or End time must be greater than the Start")
	end
end

#time_fixObject

把时间经行加工 混合



97
98
99
100
101
102
# File 'lib/vtt/smalt.rb', line 97

def time_fix
	@time = [@start,@end].map do |time|
		Timestamp.new(time).to_hms
	end
	@time.join(" --> ")
end

#to_webvttObject

整合把cue内容转换成文件需要的内容



82
83
84
# File 'lib/vtt/smalt.rb', line 82

def to_webvtt
	 	[@header, @cues.map(&:to_webvtt)].flatten.join("\n\n") << "\n"
end

#total_lengthObject

结束时间



87
88
89
# File 'lib/vtt/smalt.rb', line 87

def total_length
 		@cues.last.end_in_sec
end