vtt2ass 0.2.14 → 0.3.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59af90e625fca8d57a1427d6b520854830536816a2d8c9d4e3bad15a8f5c229f
4
- data.tar.gz: 32211cb9f63bb2de48114d071cf04e625e62892750a17143a505f8a2e62a8346
3
+ metadata.gz: 5e94988be1922e26d861f105404e3eff863a2df470aa3aa7fc5f35cdf26c3c71
4
+ data.tar.gz: 055641cfd88eef119e3c623adbae16b9eb70a7091c26b13410d8b94c2c8e55ec
5
5
  SHA512:
6
- metadata.gz: c3a3c9897a10163f7a9decfd84eef040a12700d031c2ecccfcebecabea794e5e80fb885e6e747e78b8bce69cf35f4c34248d3e90fc79d6b3d4d961b905d27447
7
- data.tar.gz: bb112d38d09029baf95cc48baace9a58bfff70f00c4a391bf5bd5dbf4cfae12f0a3b232f308ed70fa3bf15b4e965486616643ca620f205013cfa5a874d813abc
6
+ metadata.gz: 785df255fc71e0479a8b124222e25033e2aca337765cdd17bf46c6ef0e11adae7fb854b01fa23959946f8d9ebd8bbfefcaf6cafc10b9a7089a52b11806f5e82a
7
+ data.tar.gz: 9cd4940b1d5667136aa51d9bff670a2d98e4a966ee898110230c15d710c87898aaf596afd6a6c7173f0ef06cf26eb57e7f2b3acd1c670f95209d97be9c5d5480
data/.gitignore CHANGED
@@ -9,4 +9,8 @@ Gemfile.lock
9
9
 
10
10
  # Input and output folders
11
11
  input/
12
- output/
12
+ output/
13
+
14
+ # Subtitle files
15
+ /*.vtt
16
+ /*.ass
data/README.md CHANGED
@@ -42,6 +42,9 @@ Options:
42
42
  # Default: 52
43
43
  -f, [--font-family=FONT_FAMILY] # Specify a font family for the subtitles
44
44
  # Default: Open Sans Semibold
45
+ -c, [--css=CSS] # Specify a CSS file path for Hidive subs
46
+ -l, [--line-offset=N] # Specify a line offset for the main dialog (e.g. 50 lowers the text line by 50px of the total height)
47
+ # Default: 0
45
48
  -q, [--quiet], [--no-quiet] # Don't output to the console
46
49
 
47
50
  Run the VTT to ASS conversion for the specified file(s)
@@ -1,6 +1,8 @@
1
1
  # Relative imports
2
2
  require_relative 'ASSLine'
3
3
  require_relative 'ASSStyle'
4
+ require_relative 'CSSFile'
5
+ require_relative 'CSSRule'
4
6
 
5
7
  ##
6
8
  # This class defines an ASS subtitle file.
@@ -10,9 +12,12 @@ class ASSFile
10
12
 
11
13
  ##
12
14
  # Creates a new ASSFile instance and assigns the default values of instance variables.
13
- def initialize(title, width, height)
15
+ def initialize(title, width, height, css_file_path = nil)
14
16
  @width = width
15
17
  @height = height
18
+ if not css_file_path.nil? then
19
+ @css_file = CSSFile.new(css_file_path)
20
+ end
16
21
  @header = [
17
22
  '[Script Info]',
18
23
  "Title: #{title}",
@@ -40,8 +45,12 @@ class ASSFile
40
45
  # This method receives a VTTFile object and font arguments creates new ASSLine with the params of
41
46
  # each VTTLine. All those ASSLine are stored in an array. It also creates an array of ASSStyle that
42
47
  # will be used in the ASS style list.
43
- def convertVTTtoASS(vtt_file, font_family, font_size)
48
+ def convertVTTtoASS(vtt_file, font_family, font_size, line_offset = 0)
49
+ fs = font_size
44
50
  vtt_file.lines.each do |line|
51
+ font_color = '&H00FFFFFF'
52
+ is_italic = false
53
+ is_bold = false
45
54
  @ass_lines.push(ASSLine.new(line.style, line.time_start, line.time_end, line.text))
46
55
  style_exists = false
47
56
  @ass_styles.each do |style|
@@ -51,7 +60,35 @@ class ASSFile
51
60
  end
52
61
  end
53
62
  if not style_exists then
54
- @ass_styles.push(ASSStyle.new(line.style, line.params, font_family, font_size, @width, @height))
63
+ if defined?(@css_file) then
64
+ css_rule = @css_file.find_rule(line.style)
65
+ if not css_rule.nil? then
66
+ css_rule.properties.each do |property|
67
+ case property[:key]
68
+ when 'font-family'
69
+ font_family = property[:value].gsub('"', '').split(' ,').last
70
+ when 'font-size'
71
+ em_size = 1
72
+ #em_size = property[:value][0].eql? '.' ? "0#{property[:value]}" : property[:value]
73
+ if property[:value][0].eql? '.' then
74
+ em_size = "0#{property[:value]}".gsub('em', '').to_f
75
+ end
76
+ font_size = (fs * em_size).to_i
77
+ when 'color'
78
+ font_color = ASSStyle.convert_color(property[:value])
79
+ when 'font-weight'
80
+ if property[:value].eql? 'bold' then
81
+ is_bold = true
82
+ end
83
+ when 'font-style'
84
+ if property[:value].eql? 'italic' then
85
+ is_italic = true
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ @ass_styles.push(ASSStyle.new(line.style, line.params, font_family, font_size, font_color, is_bold, is_italic, line_offset, @width, @height))
55
92
  end
56
93
  end
57
94
  end
@@ -1,5 +1,7 @@
1
1
  # Relative imports
2
2
  require_relative 'ASSStyleParams'
3
+ require_relative 'Validator'
4
+ require 'redgreenblue'
3
5
 
4
6
  ##
5
7
  # This class defines an ASS style that can be applied on a subtitle line.
@@ -13,21 +15,34 @@ class ASSStyle
13
15
  # * Requires +params+, a string of VTT styling as input.
14
16
  # * Requires a video +width+ as input.
15
17
  # * Requires a video +height+ as input.
16
- def initialize(style_name, params, font_family, font_size, width, height)
18
+ def initialize(style_name, params, font_family, font_size, font_color, is_bold, is_italic, line_offset, width, height)
17
19
  @width = width
18
20
  @height = height
19
21
  @font_family = font_family
20
22
  @font_size = font_size
23
+ @font_color = font_color
21
24
  @style_name = style_name
22
25
  @s_params = ASSStyleParams.new(params, width, height)
23
26
  if style_name.eql? 'MainTop' then
24
27
  @s_params.vertical_margin = 50
25
28
  end
29
+ if style_name.include? 'Subtitle' then
30
+ @s_params.vertical_margin -= line_offset
31
+ end
32
+ @is_italic = is_italic
33
+ @is_bold = is_bold
26
34
  end
27
35
 
28
36
  ##
29
37
  # This method assigns the object values to an ASS style line and outputs it.
30
38
  def to_s
31
- return "Style: #{@style_name},#{@font_family},#{@font_size},&H00FFFFFF,&H000000FF,&H00020713,&H00000000,-1,0,0,0,100,100,0,0,1,2.0,2.0,#{@s_params.alignment},#{@s_params.horizontal_margin},0,#{@s_params.vertical_margin},1"
39
+ # Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
40
+ return "Style: #{@style_name},#{@font_family},#{@font_size},#{@font_color},&H000000FF,&H00020713,&H00000000,#{@is_bold ? '-1' : '0'},#{@is_italic ? '-1' : '0'},0,0,100,100,0,0,1,2.0,2.0,#{@s_params.alignment},#{@s_params.horizontal_margin},0,#{@s_params.vertical_margin},1"
41
+ end
42
+
43
+ def self.convert_color(color_value)
44
+ color_value.gsub!('#', '')
45
+ color = Validator.hex?(color_value) ? RGB.hex(color_value) : RGB.css(color_value)
46
+ return ("&H00%02x%02x%02x" % [color.b, color.g, color.r]).upcase
32
47
  end
33
48
  end
@@ -20,7 +20,10 @@ class Application
20
20
  @title = options[:title]
21
21
  end
22
22
  @quiet = options[:quiet]
23
- @noout = options[:noout]
23
+ if options[:css] then
24
+ @css = options[:css].gsub('\\', '/')
25
+ end
26
+ @line_offset = options[:line_offset]
24
27
  end
25
28
 
26
29
  ##
@@ -55,9 +58,10 @@ class Application
55
58
  ass_file = ASSFile.new(
56
59
  (defined?(@title) ? @title : File.basename(file_path).gsub('.vtt', '')),
57
60
  @width,
58
- @height
61
+ @height,
62
+ defined?(@css) ? @css : nil
59
63
  )
60
- ass_file.convertVTTtoASS(vtt_file, @font_family, @font_size)
64
+ ass_file.convertVTTtoASS(vtt_file, @font_family, @font_size, @line_offset)
61
65
  return ass_file
62
66
  end
63
67
 
@@ -0,0 +1,36 @@
1
+ require 'css_parser'
2
+ require_relative 'CSSRule'
3
+
4
+ class CSSFile
5
+ attr_reader :rules
6
+ include CssParser
7
+
8
+ def initialize(file_path)
9
+ @file_path = file_path
10
+ parser = CssParser::Parser.new
11
+ parser.load_file!(file_path)
12
+ @rules = []
13
+ parser.each_selector do |selector, declarations, specificity|
14
+ css_obj = CSSRule.new(selector, declarations)
15
+ if not css_obj.name.empty? then
16
+ @rules.push(css_obj)
17
+ end
18
+ end
19
+ end
20
+
21
+ def find_rule(value)
22
+ return_rule = nil
23
+ @rules.each do |rule|
24
+ if rule.name == value then
25
+ return_rule = rule
26
+ break
27
+ end
28
+ end
29
+ return return_rule
30
+ end
31
+
32
+ def to_s
33
+ return @file_path
34
+ end
35
+
36
+ end
@@ -0,0 +1,22 @@
1
+ class CSSRule
2
+ attr_reader :name, :properties
3
+
4
+ def initialize(selector, declarations)
5
+ @name = reduce_selector(selector)
6
+ @properties = []
7
+ declarations.split(/;\s?/).each do |dec|
8
+ temp = dec.split(/:\s?/)
9
+ @properties.push(
10
+ { key: temp.first, value: temp.last}
11
+ )
12
+ end
13
+ end
14
+
15
+ def to_s
16
+ return "#{@name} #{@properties}"
17
+ end
18
+
19
+ def reduce_selector(selector)
20
+ return selector.to_s.gsub(/\.rmp-container>\.rmp-content>\.rmp-cc-area>\.rmp-cc-container>\.rmp-cc-display>\.rmp-cc-cue\s?\.?/, '')
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ class Validator
2
+ def self.hex?(value)
3
+ hex = true
4
+ value.gsub!('#', '')
5
+ value.chars.each do |digit|
6
+ hex = false unless digit.match(/\h/)
7
+ end
8
+ return hex
9
+ end
10
+ end
@@ -4,5 +4,5 @@ module Vtt2ass
4
4
  ##
5
5
  # This is the version of the application.
6
6
  # This needs to be changed for each gem release.
7
- VERSION = "0.2.14"
7
+ VERSION = "0.3.2"
8
8
  end
data/lib/vtt2ass.rb CHANGED
@@ -15,6 +15,8 @@ class MainCommand < Thor
15
15
  method_option :title, :aliases => '-t', :desc => 'Specify a title for you file. If the input is a directory, all files will share the same title.', :type => :string
16
16
  method_option :font_size, :aliases => '-s', :desc => 'Specify a font size for the subtitles', :default => 52, :type => :numeric
17
17
  method_option :font_family, :aliases => '-f', :desc => 'Specify a font family for the subtitles', :default => 'Open Sans Semibold', :type => :string
18
+ method_option :css, :aliases => '-c', :desc => 'Specify a CSS file path for Hidive subs', :type => :string
19
+ method_option :line_offset, :aliases => '-l', :desc => 'Specify a line offset for the main dialog (e.g. 50 lowers the text line by 50px of the total height)', :default => 0, :type => :numeric
18
20
  method_option :quiet, :aliases => '-q', :desc => 'Don\'t output to the console', :type => :boolean
19
21
  def convert(input)
20
22
  app = Application.new(input, options)
data/vtt2ass.gemspec CHANGED
@@ -27,6 +27,8 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_dependency 'htmlentities', '~> 4.3'
29
29
  spec.add_dependency 'thor', '~> 1.1'
30
+ spec.add_dependency 'css_parser', '~> 1.10'
31
+ spec.add_dependency 'redgreenblue', '~> 0.15'
30
32
 
31
33
  spec.add_development_dependency 'rake', '~> 12.0'
32
34
  spec.add_development_dependency 'minitest', '~> 5.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vtt2ass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.14
4
+ version: 0.3.2
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-11-23 00:00:00.000000000 Z
11
+ date: 2022-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: htmlentities
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: css_parser
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redgreenblue
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.15'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.15'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rake
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -130,8 +158,11 @@ files:
130
158
  - lib/vtt2ass/ASSStyle.rb
131
159
  - lib/vtt2ass/ASSStyleParams.rb
132
160
  - lib/vtt2ass/Application.rb
161
+ - lib/vtt2ass/CSSFile.rb
162
+ - lib/vtt2ass/CSSRule.rb
133
163
  - lib/vtt2ass/VTTFile.rb
134
164
  - lib/vtt2ass/VTTLine.rb
165
+ - lib/vtt2ass/Validator.rb
135
166
  - lib/vtt2ass/version.rb
136
167
  - vtt2ass.gemspec
137
168
  homepage: https://gitlab.com/dkb-weeblets/vtt2ass