subber 0.1.1
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +41 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/subber/errors.rb +7 -0
- data/lib/subber/file/base.rb +65 -0
- data/lib/subber/file/srt.rb +12 -0
- data/lib/subber/file/vtt.rb +12 -0
- data/lib/subber/formatter/base.rb +12 -0
- data/lib/subber/formatter/srt.rb +76 -0
- data/lib/subber/formatter/vtt.rb +84 -0
- data/lib/subber/parser/base.rb +14 -0
- data/lib/subber/parser/srt.rb +100 -0
- data/lib/subber/parser/vtt.rb +109 -0
- data/lib/subber/subtitle.rb +13 -0
- data/lib/subber/version.rb +3 -0
- data/lib/subber.rb +24 -0
- data/subber.gemspec +37 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f88b65e6f3d058fda1c65f7183e2242b2bb4788
|
4
|
+
data.tar.gz: 57872f42bd1c0e8e08279dffb2a8cbea057e85e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 74ac9c8c6e06471a2b28a9f88dda66f224fc3a4d15d950941ab27bc9a514936f1f75f5de19c083f30ecbd43ff06fc52b7cfcd86a95d4460dca18ebc047a8c812
|
7
|
+
data.tar.gz: ac5cb2c919c2ae4652f3a2ce46e2db1088c794d4d138981abfbe769eb1d8a33332e342f916a9602b9bb42ee929079fb3e3f6ded298480bce4b13bf3fde8db9fe
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at hello@julianponce.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
subber (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
coderay (1.1.2)
|
10
|
+
diff-lcs (1.3)
|
11
|
+
method_source (0.9.0)
|
12
|
+
pry (0.11.3)
|
13
|
+
coderay (~> 1.1.0)
|
14
|
+
method_source (~> 0.9.0)
|
15
|
+
rake (10.5.0)
|
16
|
+
rspec (3.7.0)
|
17
|
+
rspec-core (~> 3.7.0)
|
18
|
+
rspec-expectations (~> 3.7.0)
|
19
|
+
rspec-mocks (~> 3.7.0)
|
20
|
+
rspec-core (3.7.1)
|
21
|
+
rspec-support (~> 3.7.0)
|
22
|
+
rspec-expectations (3.7.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.7.0)
|
25
|
+
rspec-mocks (3.7.0)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.7.0)
|
28
|
+
rspec-support (3.7.1)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
bundler (~> 1.16)
|
35
|
+
pry (~> 0.11)
|
36
|
+
rake (~> 10.0)
|
37
|
+
rspec (~> 3.0)
|
38
|
+
subber!
|
39
|
+
|
40
|
+
BUNDLED WITH
|
41
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Julian Ponce
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Subber
|
2
|
+
|
3
|
+
This is the Subber gem from Viki. This gem helps you convert subtitles from SRT to VTT and vice versa.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'subber'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install subber
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```rb
|
24
|
+
# Ways to instantiate a `Subber::File::Srt` or `Subber::File::Vtt`
|
25
|
+
#
|
26
|
+
srt_file = Subber::File::Srt.from_content("pass file content here")
|
27
|
+
srt_file = Subber::File::Srt.from_path("pass local or remote file path here")
|
28
|
+
srt_file = Subber::File::Srt.new(Array<Subber::Subtitle>)
|
29
|
+
|
30
|
+
# Read the content of the file
|
31
|
+
#
|
32
|
+
srt_file.content
|
33
|
+
|
34
|
+
# Get Subber::Subtitle objects
|
35
|
+
#
|
36
|
+
srt_file.subtitles
|
37
|
+
|
38
|
+
# Export to a local file
|
39
|
+
#
|
40
|
+
srt_file.export("pass local path here")
|
41
|
+
|
42
|
+
# Convert between Srt and Vtt
|
43
|
+
#
|
44
|
+
vtt_file = srt_file.to_vtt
|
45
|
+
srt_file = vtt_file.to_srt
|
46
|
+
```
|
47
|
+
|
48
|
+
## Development
|
49
|
+
|
50
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
51
|
+
|
52
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/viki-org/subber-gem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
57
|
+
|
58
|
+
## License
|
59
|
+
|
60
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
61
|
+
|
62
|
+
## Code of Conduct
|
63
|
+
|
64
|
+
Everyone interacting in the Subber project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/viki-org/subber-gem/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "subber"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
|
3
|
+
module Subber::File
|
4
|
+
class Base
|
5
|
+
class << self
|
6
|
+
attr_reader :parser_klass
|
7
|
+
attr_reader :formatter_klass
|
8
|
+
|
9
|
+
# @param value [Class] class to be used as parser
|
10
|
+
#
|
11
|
+
def parser(value = nil)
|
12
|
+
@parser_klass = value unless value.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
# @param value [Class] class to be used as formatter
|
16
|
+
#
|
17
|
+
def formatter(value = nil)
|
18
|
+
@formatter_klass = value unless value.nil?
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param content [String]
|
22
|
+
# @return [Subber::File::Base]
|
23
|
+
#
|
24
|
+
def from_content(content)
|
25
|
+
subtitles = parser_klass.parse(content)
|
26
|
+
new(subtitles: subtitles)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param path [String]
|
30
|
+
# @return [Subber::File::Base]
|
31
|
+
#
|
32
|
+
def from_path(path)
|
33
|
+
content = open(path).read
|
34
|
+
from_content(content)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_reader :subtitles
|
39
|
+
|
40
|
+
# @param [Hash]
|
41
|
+
# @param subtitles [Array<Subber::Subtitle>]
|
42
|
+
#
|
43
|
+
def initialize(subtitles: nil)
|
44
|
+
@subtitles = subtitles
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [String]
|
48
|
+
#
|
49
|
+
def content
|
50
|
+
@content ||= formatter.format(subtitles)
|
51
|
+
end
|
52
|
+
|
53
|
+
# @param path [String] Remote or local file path
|
54
|
+
#
|
55
|
+
def export(path)
|
56
|
+
File.write(path, content)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def formatter
|
62
|
+
self.class.formatter_klass
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Subber::Formatter
|
2
|
+
class Srt < Base
|
3
|
+
LF_REGEX = /\n/
|
4
|
+
CRLF = "\r\n"
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# @param subtitles [Array<Subber::Subtitle>]
|
8
|
+
# @return [String]
|
9
|
+
#
|
10
|
+
def format(subtitles)
|
11
|
+
subtitle_texts =
|
12
|
+
subtitles.map do |subtitle|
|
13
|
+
convert_subtitle_to_text(subtitle)
|
14
|
+
end
|
15
|
+
|
16
|
+
file_content = subtitle_texts.join
|
17
|
+
file_content = add_window_line_break(file_content)
|
18
|
+
file_content
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# @param file_content [String]
|
24
|
+
# @return [String]
|
25
|
+
#
|
26
|
+
def add_window_line_break(file_content)
|
27
|
+
file_content.gsub(LF_REGEX, CRLF)
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param subtitle [Subber::Subtitle]
|
31
|
+
# @return [String]
|
32
|
+
#
|
33
|
+
def convert_subtitle_to_text(subtitle)
|
34
|
+
subtitle_text = [
|
35
|
+
build_counter(subtitle),
|
36
|
+
build_time_range(subtitle),
|
37
|
+
build_content(subtitle),
|
38
|
+
].join("\n")
|
39
|
+
|
40
|
+
"#{subtitle_text}\n\n"
|
41
|
+
end
|
42
|
+
|
43
|
+
# @param subtitle [Subber::Subtitle]
|
44
|
+
# @return [String]
|
45
|
+
#
|
46
|
+
def build_counter(subtitle)
|
47
|
+
subtitle.counter.to_s
|
48
|
+
end
|
49
|
+
|
50
|
+
# @param subtitle [Subber::Subtitle]
|
51
|
+
# @return [String]
|
52
|
+
#
|
53
|
+
def build_time_range(subtitle)
|
54
|
+
start_time = convert_ms_to_time(subtitle.start_time)
|
55
|
+
end_time = convert_ms_to_time(subtitle.end_time)
|
56
|
+
|
57
|
+
"#{start_time} --> #{end_time}"
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param subtitle [Subber::Subtitle]
|
61
|
+
# @return [String]
|
62
|
+
#
|
63
|
+
def build_content(subtitle)
|
64
|
+
subtitle.content
|
65
|
+
end
|
66
|
+
|
67
|
+
# @param ms_time [Integer] Time in milliseconds
|
68
|
+
# @return [String] Formatted time
|
69
|
+
#
|
70
|
+
def convert_ms_to_time(ms_time)
|
71
|
+
seconds = ms_time / 1000.0
|
72
|
+
Time.at(seconds).utc.strftime("%H:%M:%S,%L")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Subber::Formatter
|
2
|
+
class Vtt < Base
|
3
|
+
LF_REGEX = /\n/
|
4
|
+
CRLF = "\r\n"
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# @param subtitles [Array<Subber::Subtitle>]
|
8
|
+
# @return [String]
|
9
|
+
#
|
10
|
+
def format(subtitles)
|
11
|
+
subtitle_texts =
|
12
|
+
subtitles.map do |subtitle|
|
13
|
+
convert_subtitle_to_text(subtitle)
|
14
|
+
end
|
15
|
+
|
16
|
+
file_content = subtitle_texts.join
|
17
|
+
file_content = add_webvtt_header(file_content)
|
18
|
+
file_content = add_window_line_break(file_content)
|
19
|
+
file_content
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
# @param file_content [String]
|
25
|
+
# @return [String]
|
26
|
+
#
|
27
|
+
def add_webvtt_header(file_content)
|
28
|
+
"WEBVTT\n\n#{file_content}"
|
29
|
+
end
|
30
|
+
|
31
|
+
# @param file_content [String]
|
32
|
+
# @return [String]
|
33
|
+
#
|
34
|
+
def add_window_line_break(file_content)
|
35
|
+
file_content.gsub(LF_REGEX, CRLF)
|
36
|
+
end
|
37
|
+
|
38
|
+
# @param subtitle [Subber::Subtitle]
|
39
|
+
# @return [String]
|
40
|
+
#
|
41
|
+
def convert_subtitle_to_text(subtitle)
|
42
|
+
subtitle_text = [
|
43
|
+
build_counter(subtitle),
|
44
|
+
build_time_range(subtitle),
|
45
|
+
build_content(subtitle),
|
46
|
+
].join("\n")
|
47
|
+
|
48
|
+
"#{subtitle_text}\n\n"
|
49
|
+
end
|
50
|
+
|
51
|
+
# @param subtitle [Subber::Subtitle]
|
52
|
+
# @return [String]
|
53
|
+
#
|
54
|
+
def build_counter(subtitle)
|
55
|
+
subtitle.counter.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
# @param subtitle [Subber::Subtitle]
|
59
|
+
# @return [String]
|
60
|
+
#
|
61
|
+
def build_time_range(subtitle)
|
62
|
+
start_time = convert_ms_to_time(subtitle.start_time)
|
63
|
+
end_time = convert_ms_to_time(subtitle.end_time)
|
64
|
+
|
65
|
+
"#{start_time} --> #{end_time}"
|
66
|
+
end
|
67
|
+
|
68
|
+
# @param subtitle [Subber::Subtitle]
|
69
|
+
# @return [String]
|
70
|
+
#
|
71
|
+
def build_content(subtitle)
|
72
|
+
subtitle.content
|
73
|
+
end
|
74
|
+
|
75
|
+
# @param ms_time [Integer] Time in milliseconds
|
76
|
+
# @return [String] Formatted time
|
77
|
+
#
|
78
|
+
def convert_ms_to_time(ms_time)
|
79
|
+
seconds = ms_time / 1000.0
|
80
|
+
Time.at(seconds).utc.strftime("%H:%M:%S.%L")
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module Subber::Parser
|
2
|
+
class Srt < Base
|
3
|
+
SUBTITLE_REGEX = /([^\n]*)\n([^\n]*)(\n(.*))?/m
|
4
|
+
COUNTER_REGEX = /\d+/
|
5
|
+
TIME_RANGE_REGEX = /(\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2},\d{3})/
|
6
|
+
TIMECODE_REGEX = /(\d{2}):(\d{2}):(\d{2}),(\d{3})/
|
7
|
+
|
8
|
+
DELIMITER_REGEX = /\n?\n\n/
|
9
|
+
WINDOW_LINE_BREAK_REGEX = /\r/
|
10
|
+
BYTE_ORDER_MARK_STRING = "\xEF\xBB\xBF"
|
11
|
+
|
12
|
+
class << self
|
13
|
+
# @param file_content [String]
|
14
|
+
# @return [Array<Subber::Subtitle>]
|
15
|
+
#
|
16
|
+
def parse(file_content)
|
17
|
+
file_content = remove_window_line_break(file_content)
|
18
|
+
|
19
|
+
subtitle_texts = file_content.split(DELIMITER_REGEX)
|
20
|
+
subtitle_texts.map do |subtitle_text|
|
21
|
+
convert_text_to_subtitle(subtitle_text)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# @param file_content [String]
|
28
|
+
# @return [String]
|
29
|
+
#
|
30
|
+
def remove_window_line_break(file_content)
|
31
|
+
file_content.gsub(WINDOW_LINE_BREAK_REGEX, '')
|
32
|
+
end
|
33
|
+
|
34
|
+
# @param subtitle_text [String]
|
35
|
+
# @return [Subber::Subtitle]
|
36
|
+
#
|
37
|
+
def convert_text_to_subtitle(subtitle_text)
|
38
|
+
matches = subtitle_text.match(SUBTITLE_REGEX).to_a
|
39
|
+
raise(Subber::Errors::InvalidSrtFormat, subtitle_text) if matches.empty?
|
40
|
+
|
41
|
+
_subtitle_text, counter, time_range_string, _new_line, content = matches
|
42
|
+
|
43
|
+
counter = extract_counter(counter)
|
44
|
+
from, to = extract_time_range(time_range_string)
|
45
|
+
|
46
|
+
Subber::Subtitle.new(
|
47
|
+
counter: counter,
|
48
|
+
start_time: convert_time_to_ms(from),
|
49
|
+
end_time: convert_time_to_ms(to),
|
50
|
+
content: content
|
51
|
+
)
|
52
|
+
rescue Subber::Errors::InvalidCounter
|
53
|
+
raise(Subber::Errors::InvalidCounter, subtitle_text)
|
54
|
+
rescue Subber::Errors::InvalidTimeRange
|
55
|
+
raise(Subber::Errors::InvalidTimeRange, subtitle_text)
|
56
|
+
rescue Subber::Errors::InvalidTimestamp
|
57
|
+
raise(Subber::Errors::InvalidTimestamp, subtitle_text)
|
58
|
+
end
|
59
|
+
|
60
|
+
# @param counter_string [String]
|
61
|
+
# @return [Integer]
|
62
|
+
# @raise [Subber::Errors::InvalidCounter]
|
63
|
+
#
|
64
|
+
def extract_counter(text)
|
65
|
+
raise(Subber::Errors::InvalidCounter) if text.match(COUNTER_REGEX).nil?
|
66
|
+
text.sub!(BYTE_ORDER_MARK_STRING, '')
|
67
|
+
text.to_i
|
68
|
+
end
|
69
|
+
|
70
|
+
# @param text [String]
|
71
|
+
# @return [
|
72
|
+
# [String] from
|
73
|
+
# [String] to
|
74
|
+
# ]
|
75
|
+
# @raise [Subber::Errors::InvalidCounter]
|
76
|
+
#
|
77
|
+
def extract_time_range(text)
|
78
|
+
matches = text.match(TIME_RANGE_REGEX).to_a
|
79
|
+
raise(Subber::Errors::InvalidTimeRange) if matches.empty?
|
80
|
+
_text, from, to = matches
|
81
|
+
|
82
|
+
[from, to]
|
83
|
+
end
|
84
|
+
|
85
|
+
# @param time_string [String]
|
86
|
+
# @return [Integer]
|
87
|
+
# @raise [Subber::Errors::InvalidTimestamp]
|
88
|
+
#
|
89
|
+
def convert_time_to_ms(time_string)
|
90
|
+
matches = time_string.match(TIMECODE_REGEX).to_a
|
91
|
+
raise(Subber::Errors::InvalidTimestamp, time_string) if matches.empty?
|
92
|
+
|
93
|
+
int_matches = matches.map(&:to_i)
|
94
|
+
_time_string, hours, minutes, seconds, milliseconds = int_matches
|
95
|
+
|
96
|
+
((hours * 60 + minutes) * 60 + seconds) * 1000 + milliseconds
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module Subber::Parser
|
2
|
+
class Vtt < Base
|
3
|
+
SUBTITLE_REGEX = /([^\n]*)\n([^\n]*)(\n(.*))?/m
|
4
|
+
COUNTER_REGEX = /\d+/
|
5
|
+
TIME_RANGE_REGEX = /(\d{2}:\d{2}:\d{2}\.\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2}\.\d{3})/
|
6
|
+
TIMECODE_REGEX = /(\d{2}):(\d{2}):(\d{2})\.(\d{3})/
|
7
|
+
|
8
|
+
DELIMITER_REGEX = /\n?\n\n/
|
9
|
+
WINDOW_LINE_BREAK_REGEX = /\r/
|
10
|
+
WEBVTT_HEADER_REGEX = /WEBVTT\n\n/
|
11
|
+
BYTE_ORDER_MARK_STRING = "\xEF\xBB\xBF"
|
12
|
+
|
13
|
+
class << self
|
14
|
+
# @param file_content [String]
|
15
|
+
# @return [Array<Subber::Subtitle>]
|
16
|
+
#
|
17
|
+
def parse(file_content)
|
18
|
+
file_content = remove_window_line_break(file_content)
|
19
|
+
file_content = remove_webvtt_header(file_content)
|
20
|
+
|
21
|
+
subtitle_texts = file_content.split(DELIMITER_REGEX)
|
22
|
+
subtitle_texts.map do |subtitle_text|
|
23
|
+
convert_text_to_subtitle(subtitle_text)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# @param file_content [String]
|
30
|
+
# @return [String]
|
31
|
+
#
|
32
|
+
def remove_webvtt_header(file_content)
|
33
|
+
file_content.sub(WEBVTT_HEADER_REGEX, '')
|
34
|
+
end
|
35
|
+
|
36
|
+
# @param file_content [String]
|
37
|
+
# @return [String]
|
38
|
+
#
|
39
|
+
def remove_window_line_break(file_content)
|
40
|
+
file_content.gsub(WINDOW_LINE_BREAK_REGEX, '')
|
41
|
+
end
|
42
|
+
|
43
|
+
# @param subtitle_text [String]
|
44
|
+
# @return [Subber::Subtitle]
|
45
|
+
#
|
46
|
+
def convert_text_to_subtitle(subtitle_text)
|
47
|
+
matches = subtitle_text.match(SUBTITLE_REGEX).to_a
|
48
|
+
raise(Subber::Errors::InvalidSrtFormat, subtitle_text) if matches.empty?
|
49
|
+
|
50
|
+
_subtitle_text, counter, time_range_string, _new_line, content = matches
|
51
|
+
|
52
|
+
counter = extract_counter(counter)
|
53
|
+
from, to = extract_time_range(time_range_string)
|
54
|
+
|
55
|
+
Subber::Subtitle.new(
|
56
|
+
counter: counter,
|
57
|
+
start_time: convert_time_to_ms(from),
|
58
|
+
end_time: convert_time_to_ms(to),
|
59
|
+
content: content
|
60
|
+
)
|
61
|
+
rescue Subber::Errors::InvalidCounter
|
62
|
+
raise(Subber::Errors::InvalidCounter, subtitle_text)
|
63
|
+
rescue Subber::Errors::InvalidTimeRange
|
64
|
+
raise(Subber::Errors::InvalidTimeRange, subtitle_text)
|
65
|
+
rescue Subber::Errors::InvalidTimestamp
|
66
|
+
raise(Subber::Errors::InvalidTimestamp, subtitle_text)
|
67
|
+
end
|
68
|
+
|
69
|
+
# @param counter_string [String]
|
70
|
+
# @return [Integer]
|
71
|
+
# @raise [Subber::Errors::InvalidCounter]
|
72
|
+
#
|
73
|
+
def extract_counter(text)
|
74
|
+
raise(Subber::Errors::InvalidCounter) if text.match(COUNTER_REGEX).nil?
|
75
|
+
text.sub!(BYTE_ORDER_MARK_STRING, '')
|
76
|
+
text.to_i
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param text [String]
|
80
|
+
# @return [
|
81
|
+
# [String] from
|
82
|
+
# [String] to
|
83
|
+
# ]
|
84
|
+
# @raise [Subber::Errors::InvalidCounter]
|
85
|
+
#
|
86
|
+
def extract_time_range(text)
|
87
|
+
matches = text.match(TIME_RANGE_REGEX).to_a
|
88
|
+
raise(Subber::Errors::InvalidTimeRange) if matches.empty?
|
89
|
+
_text, from, to = matches
|
90
|
+
|
91
|
+
[from, to]
|
92
|
+
end
|
93
|
+
|
94
|
+
# @param time_string [String]
|
95
|
+
# @return [Integer]
|
96
|
+
# @raise [Subber::Errors::InvalidTimestamp]
|
97
|
+
#
|
98
|
+
def convert_time_to_ms(time_string)
|
99
|
+
matches = time_string.match(TIMECODE_REGEX).to_a
|
100
|
+
raise(Subber::Errors::InvalidTimestamp, time_string) if matches.empty?
|
101
|
+
|
102
|
+
int_matches = matches.map(&:to_i)
|
103
|
+
_time_string, hours, minutes, seconds, milliseconds = int_matches
|
104
|
+
|
105
|
+
((hours * 60 + minutes) * 60 + seconds) * 1000 + milliseconds
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class Subber::Subtitle
|
2
|
+
attr_reader :counter
|
3
|
+
attr_reader :start_time
|
4
|
+
attr_reader :end_time
|
5
|
+
attr_reader :content
|
6
|
+
|
7
|
+
def initialize(attributes)
|
8
|
+
@counter = attributes[:counter]
|
9
|
+
@start_time = attributes[:start_time]
|
10
|
+
@end_time = attributes[:end_time]
|
11
|
+
@content = attributes[:content]
|
12
|
+
end
|
13
|
+
end
|
data/lib/subber.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'subber/version'
|
2
|
+
require 'subber/errors'
|
3
|
+
require 'subber/subtitle'
|
4
|
+
|
5
|
+
# Formatters
|
6
|
+
#
|
7
|
+
require 'subber/formatter/base'
|
8
|
+
require 'subber/formatter/srt'
|
9
|
+
require 'subber/formatter/vtt'
|
10
|
+
|
11
|
+
# Parsers
|
12
|
+
#
|
13
|
+
require 'subber/parser/base'
|
14
|
+
require 'subber/parser/srt'
|
15
|
+
require 'subber/parser/vtt'
|
16
|
+
|
17
|
+
# Files
|
18
|
+
#
|
19
|
+
require 'subber/file/base'
|
20
|
+
require 'subber/file/srt'
|
21
|
+
require 'subber/file/vtt'
|
22
|
+
|
23
|
+
module Subber
|
24
|
+
end
|
data/subber.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "subber/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "subber"
|
8
|
+
spec.version = Subber::VERSION
|
9
|
+
spec.authors = ["Julian Ponce"]
|
10
|
+
spec.email = ["julian.ponce@viki.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Subtitle converter}
|
13
|
+
spec.description = %q{Subtitle converter}
|
14
|
+
spec.homepage = "https://github.com/viki-org/subber-gem"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
spec.add_development_dependency "pry", "~> 0.11"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: subber
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julian Ponce
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.11'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.11'
|
69
|
+
description: Subtitle converter
|
70
|
+
email:
|
71
|
+
- julian.ponce@viki.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/subber.rb
|
88
|
+
- lib/subber/errors.rb
|
89
|
+
- lib/subber/file/base.rb
|
90
|
+
- lib/subber/file/srt.rb
|
91
|
+
- lib/subber/file/vtt.rb
|
92
|
+
- lib/subber/formatter/base.rb
|
93
|
+
- lib/subber/formatter/srt.rb
|
94
|
+
- lib/subber/formatter/vtt.rb
|
95
|
+
- lib/subber/parser/base.rb
|
96
|
+
- lib/subber/parser/srt.rb
|
97
|
+
- lib/subber/parser/vtt.rb
|
98
|
+
- lib/subber/subtitle.rb
|
99
|
+
- lib/subber/version.rb
|
100
|
+
- subber.gemspec
|
101
|
+
homepage: https://github.com/viki-org/subber-gem
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata:
|
105
|
+
allowed_push_host: https://rubygems.org
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.4.5.2
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Subtitle converter
|
126
|
+
test_files: []
|