vtt2ass 0.2.5 → 0.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -86,7 +86,7 @@
86
86
 
87
87
 
88
88
 
89
- <strong class="classes">Classes:</strong> <span class='object_link'><a href="ASSFile.html" title="ASSFile (class)">ASSFile</a></span>, <span class='object_link'><a href="ASSStyle.html" title="ASSStyle (class)">ASSStyle</a></span>, <span class='object_link'><a href="ASSSubtitle.html" title="ASSSubtitle (class)">ASSSubtitle</a></span>, <span class='object_link'><a href="Application.html" title="Application (class)">Application</a></span>, <span class='object_link'><a href="VTTSubtitle.html" title="VTTSubtitle (class)">VTTSubtitle</a></span>
89
+ <strong class="classes">Classes:</strong> <span class='object_link'><a href="ASSFile.html" title="ASSFile (class)">ASSFile</a></span>, <span class='object_link'><a href="ASSLine.html" title="ASSLine (class)">ASSLine</a></span>, <span class='object_link'><a href="ASSStyle.html" title="ASSStyle (class)">ASSStyle</a></span>, <span class='object_link'><a href="ASSStyleParams.html" title="ASSStyleParams (class)">ASSStyleParams</a></span>, <span class='object_link'><a href="Application.html" title="Application (class)">Application</a></span>, <span class='object_link'><a href="VTTFile.html" title="VTTFile (class)">VTTFile</a></span>, <span class='object_link'><a href="VTTLine.html" title="VTTLine (class)">VTTLine</a></span>
90
90
 
91
91
 
92
92
  </p>
@@ -102,9 +102,9 @@
102
102
  </div>
103
103
 
104
104
  <div id="footer">
105
- Generated on Thu Jan 14 00:31:16 2021 by
105
+ Generated on Tue Mar 23 21:07:02 2021 by
106
106
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
107
- 0.9.26 (ruby-2.7.2).
107
+ 0.9.26 (ruby-3.0.0).
108
108
  </div>
109
109
 
110
110
  </div>
data/lib/vtt2ass.rb CHANGED
@@ -29,6 +29,9 @@ module Vtt2ass
29
29
  opts.on("-s", "--font-size SIZE", Integer, "Specify a font size for the subtitles (default: 52)") do |font_size|
30
30
  options[:font_size] = font_size
31
31
  end
32
+ opts.on("-t", "--title TITLE", String, "Specify a title for you file. If the input is a directory, all files will share the same title.") do |title|
33
+ options[:title] = title
34
+ end
32
35
  opts.on("-v", "--version", "Show version") do
33
36
  puts Vtt2ass::VERSION
34
37
  exit
@@ -8,6 +8,8 @@ class ASSFile
8
8
  attr_reader :title, :width, :height
9
9
  attr_accessor :ass_styles, :ass_lines
10
10
 
11
+ ##
12
+ # Creates a new ASSFile instance and assigns the default values of instance variables.
11
13
  def initialize(title, width, height)
12
14
  @width = width
13
15
  @height = height
@@ -34,6 +36,10 @@ class ASSFile
34
36
  @ass_lines = []
35
37
  end
36
38
 
39
+ ##
40
+ # This method receives a VTTFile object and font arguments creates new ASSLine with the params of
41
+ # each VTTLine. All those ASSLine are stored in an array. It also creates an array of ASSStyle that
42
+ # will be used in the ASS style list.
37
43
  def convertVTTtoASS(vtt_file, font_family, font_size)
38
44
  vtt_file.lines.each do |line|
39
45
  @ass_lines.push(ASSLine.new(line.style, line.time_start, line.time_end, line.text))
@@ -50,6 +56,8 @@ class ASSFile
50
56
  end
51
57
  end
52
58
 
59
+ ##
60
+ # This method writes the content of the ASSFile object into a file path that is provided.
53
61
  def writeToFile(file_path)
54
62
  File.open(file_path, 'w') do |line|
55
63
  line.print "\ufeff"
@@ -1,3 +1,5 @@
1
+ require 'htmlentities'
2
+
1
3
  ##
2
4
  # This class defines an ASS subtile line.
3
5
  class ASSLine
@@ -28,6 +30,7 @@ class ASSLine
28
30
  #
29
31
  # * Requires +text+, a string of VTT formated text as input.
30
32
  def convertToAssText(text)
33
+ decoder = HTMLEntities.new()
31
34
  text = text
32
35
  .gsub(/\r/, '')
33
36
  .gsub(/\n/, '\\N')
@@ -42,7 +45,7 @@ class ASSLine
42
45
  .gsub(/<[^>]>/, '')
43
46
  .gsub(/\\N$/, '')
44
47
  .gsub(/ +$/, '')
45
- return text
48
+ return decoder.decode(text)
46
49
  end
47
50
 
48
51
  ##
@@ -80,7 +83,7 @@ class ASSLine
80
83
  end
81
84
 
82
85
  ##
83
- # The method pads text so that time numbers are a fixed number of digit.
86
+ # This method pads text so that time numbers are a fixed number of digit.
84
87
  #
85
88
  # * Requires +sep+, a string separator.
86
89
  # * Requires +input+, an integer.
@@ -20,6 +20,9 @@ class ASSStyle
20
20
  @font_size = font_size
21
21
  @style_name = style_name
22
22
  @s_params = ASSStyleParams.new(params, width, height)
23
+ if style_name.eql? 'MainTop'
24
+ @s_params.vertical_margin = 50
25
+ end
23
26
  end
24
27
 
25
28
  ##
@@ -1,8 +1,12 @@
1
1
  ##
2
2
  # This class defines the ASS style parameters from VTT cue settings.
3
3
  class ASSStyleParams
4
- attr_reader :horizontal_margin, :vertical_margin, :alignment, :align
4
+ attr_accessor :horizontal_margin, :vertical_margin, :alignment, :align
5
5
 
6
+ ##
7
+ # Creates an instance of ASSStyleParams
8
+ # It takes VTT style arguments and assign them to their respectful instance variable.
9
+ # It calls methods to create ASS values from the VTT cue settings.
6
10
  def initialize(params, width, height)
7
11
  (params.split(' ').map { |p| p.split(':') }).each do |p|
8
12
  case p[0]
@@ -20,6 +24,9 @@ class ASSStyleParams
20
24
  createVerticalMargin(height)
21
25
  end
22
26
 
27
+ ##
28
+ # This method decides the alignement value in a 9 position grid based of the
29
+ # values in cue settings "align" and "line".
23
30
  def createAlignment()
24
31
  if (defined?(@line) and not defined?(@position)) then
25
32
  if (defined?(@align)) then
@@ -50,6 +57,9 @@ class ASSStyleParams
50
57
  end
51
58
  end
52
59
 
60
+ ##
61
+ # This method calculates the horizontal margin in px between the alignement position and
62
+ # and the content displayed by using the "position" cue setting.
53
63
  def createHorizontalMargin(width)
54
64
  steps = (width / 100).to_i
55
65
  if defined?(@position) then
@@ -59,6 +69,9 @@ class ASSStyleParams
59
69
  end
60
70
  end
61
71
 
72
+ ##
73
+ # This method calculates the vertical margin in px between the alignement position and
74
+ # and the content displayed by using the "line" cue setting.
62
75
  def createVerticalMargin(height)
63
76
  steps = (height / 100).to_i
64
77
  if defined?(@line) then
@@ -1,6 +1,3 @@
1
- # Imports
2
- require 'os'
3
-
4
1
  # Relative imports
5
2
  require_relative 'VTTFile'
6
3
  require_relative 'ASSFile'
@@ -13,12 +10,15 @@ class Application
13
10
  # Creates a new Application instance.
14
11
  # It receives +options+ that can define the input and output directories.
15
12
  def initialize(options)
16
- @input = options[:input] ? options[:input].gsub('\\', '/') : "./"
17
- @output = options[:output] ? options[:output].gsub('\\', '/') : "./"
13
+ @input = options[:input] ? options[:input].gsub('\\', '/').delete_suffix('/') : "."
14
+ @output = options[:output] ? options[:output].gsub('\\', '/').delete_suffix('/') : "."
18
15
  @width = 1920
19
16
  @height = 1080
20
17
  @font_family = options[:font_family] ? options[:font_family] : 'Open Sans Semibold'
21
18
  @font_size = options[:font_size] ? options[:font_size] : 52
19
+ if options[:title] then
20
+ @title = options[:title]
21
+ end
22
22
  end
23
23
 
24
24
  ##
@@ -28,18 +28,25 @@ class Application
28
28
  def start
29
29
  if File.directory?(@input) then
30
30
  Dir["#{@input}/*.vtt"].each do |file_path|
31
- vtt_to_ass(file_path).writeToFile(File.basename(file_path).gsub('.vtt', '.ass'))
31
+ vtt_to_ass(file_path).writeToFile(@output + '/' + File.basename(file_path).gsub('.vtt', '.ass'))
32
32
  end
33
33
  elsif File.file?(@input) then
34
- vtt_to_ass(@input).writeToFile(File.basename(@input).gsub('.vtt', '.ass'))
34
+ vtt_to_ass(@input).writeToFile(@output + '/' + File.basename(@input).gsub('.vtt', '.ass'))
35
35
  else
36
36
  puts 'Error: input file or directory does not exist.'
37
37
  end
38
38
  end
39
39
 
40
+ ##
41
+ # This method creates a new VTTFile object from the file path provided and convert its content
42
+ # inside a new ASSFile object.
40
43
  def vtt_to_ass(file_path)
41
44
  vtt_file = VTTFile.new(file_path)
42
- ass_file = ASSFile.new(File.basename(file_path).gsub('.vtt', ''), @width, @height)
45
+ ass_file = ASSFile.new(
46
+ (defined?(@title) ? @title : File.basename(file_path).gsub('.vtt', '')),
47
+ @width,
48
+ @height
49
+ )
43
50
  ass_file.convertVTTtoASS(vtt_file, @font_family, @font_size)
44
51
  return ass_file
45
52
  end
@@ -1,6 +1,3 @@
1
- # Imports
2
- require 'os'
3
-
4
1
  # Relative imports
5
2
  require_relative 'VTTLine'
6
3
 
@@ -9,19 +6,32 @@ require_relative 'VTTLine'
9
6
  class VTTFile
10
7
  attr_accessor :lines
11
8
 
9
+ ##
10
+ # Creates a new VTTFile instance and assigns the default values of instance variables.
12
11
  def initialize(file_path)
13
12
  @title = File.basename(file_path).gsub('.vtt', '')
14
13
  @lines = []
15
- separator = OS.posix? ? "\r\n\r\n": "\n\n"
14
+ separator = determine_line_ending(file_path) ? "\n\n" : "\r\n\r\n"
15
+ count = 0
16
16
  File.foreach(file_path, separator) do |paragraph|
17
- paragraph = paragraph.rstrip.gsub(/\r\n/, "\n")
17
+ paragraph = paragraph.rstrip.gsub(/[\r\n]/, "\n")
18
18
  if not paragraph.eql? "" then
19
- @lines.push(VTTLine.new(paragraph))
19
+ vtt_line = VTTLine.new(paragraph)
20
+ @lines.push(vtt_line)
21
+ count += 1
20
22
  end
21
23
  end
22
24
  @lines.shift
23
25
  end
24
26
 
27
+ def determine_line_ending(file_path)
28
+ File.open(file_path, 'r') do |file|
29
+ return file.readline[/\r?\n$/] == "\n"
30
+ end
31
+ end
32
+
33
+ ##
34
+ # This method writes the content of the VTTFile object into a file path that is provided.
25
35
  def writeToFile(file_path)
26
36
  File.open(file_path, 'w') do |line|
27
37
  line.print "\ufeff"
@@ -29,6 +39,8 @@ class VTTFile
29
39
  end
30
40
  end
31
41
 
42
+ ##
43
+ # This method concatenates the object data in the right order for a string output.
32
44
  def to_s
33
45
  return "WEBVTT\n\n\n" + @lines
34
46
  end
@@ -22,7 +22,7 @@ class VTTLine
22
22
  @time_start = m[1]
23
23
  @time_end = m[2]
24
24
  @params = m[3]
25
- if @params.include? "align:middle line:7%" then
25
+ if @params.include? "align:middle line:7%" or @params.include? "align:middle line:10%" then
26
26
  @style = "MainTop"
27
27
  end
28
28
  else
@@ -1,3 +1,8 @@
1
+ ##
2
+ # Vtt2ass module to provide the version number
1
3
  module Vtt2ass
2
- VERSION = "0.2.5"
4
+ ##
5
+ # This is the version of the application.
6
+ # This needs to be changed for each gem release.
7
+ VERSION = "0.2.11"
3
8
  end
data/vtt2ass.gemspec CHANGED
@@ -24,4 +24,10 @@ Gem::Specification.new do |spec|
24
24
  spec.bindir = "exe"
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency 'htmlentities'
29
+
30
+ spec.add_development_dependency 'rake'
31
+ spec.add_development_dependency 'minitest'
32
+ spec.add_development_dependency 'yard'
27
33
  end
metadata CHANGED
@@ -1,15 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vtt2ass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Louis-Philippe Fortin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2021-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: htmlentities
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
13
69
  description:
14
70
  email:
15
71
  - timemaster.lpf@gmail.com
@@ -27,9 +83,13 @@ files:
27
83
  - bin/run
28
84
  - bin/setup
29
85
  - doc/ASSFile.html
86
+ - doc/ASSLine.html
30
87
  - doc/ASSStyle.html
88
+ - doc/ASSStyleParams.html
31
89
  - doc/ASSSubtitle.html
32
90
  - doc/Application.html
91
+ - doc/VTTFile.html
92
+ - doc/VTTLine.html
33
93
  - doc/VTTSubtitle.html
34
94
  - doc/Vtt2ass.html
35
95
  - doc/Vtt2ass/Error.html