moving_words 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5ae83eed33f0e46c66cc67e607733aacc0fd6a21
4
+ data.tar.gz: 5996bafe18776a506dbbcf5e82c1e520a2a57f35
5
+ SHA512:
6
+ metadata.gz: e96fefcb675625551efc777ef195ee6634a83b78829ca9fe2f554fa768ba6f677bfed00b026d146b367bea974a08accc1577d6b8dc89e4d31eafa00ac5e99323
7
+ data.tar.gz: 8f349a29b02215627fad49c6e3036c29592a432a8fc8667830c3138b86d7416ff959e9495a2d88ff5cd979a6d134f56d8ab2d315396594e8fd8cb1bd0ac149db
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in caption.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alan Johnson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Moving Words
2
+
3
+ Moving Words is a parser for video caption file formats. It currently supports SubRip, or SRT.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'moving_words'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install moving_words
18
+
19
+ ## Usage
20
+
21
+
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ module MovingWords
2
+ class CaptionBlock
3
+ attr_accessor :start_time, :end_time, :content
4
+
5
+ # Public: Sets up a new CaptionBlock with the given
6
+ # start_time, end_time, and content.
7
+ #
8
+ # start_time - Offset in milliseconds from the
9
+ # beginning of the video to this `CaptionBlock`
10
+ # end_time - Offset in milliseconds from the
11
+ # beginning of the video to the end of this
12
+ # `CaptionBlock`
13
+ # content - The contents of this `CaptionBlock`
14
+ def initialize(start_time, end_time, content)
15
+ self.start_time = start_time
16
+ self.end_time = end_time
17
+ self.content = content
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,64 @@
1
+ module MovingWords
2
+ class SrtParser
3
+ attr_accessor :data
4
+
5
+ # Public: Sets up a new parser based on the given
6
+ # data.
7
+ def initialize(data)
8
+ self.data = data
9
+ end
10
+
11
+ # Public: Converts the caption data to a set of
12
+ # `CaptionBlock` objects.
13
+ #
14
+ # Returns an array of `CaptionBlock`s
15
+ def parse
16
+ blocks = []
17
+ block_content = ""
18
+ data.each_line do |line|
19
+ if line.strip.empty?
20
+ blocks << parse_caption_block(block_content) unless block_content.empty?
21
+ block_content = ""
22
+ else
23
+ block_content += line
24
+ end
25
+ end
26
+
27
+ # Handle any final content
28
+ blocks << parse_caption_block(block_content) unless block_content.empty?
29
+
30
+ blocks
31
+ end
32
+
33
+ # Internal: Converts a caption block in the SRT
34
+ # data to a `CaptionBlock` object.
35
+ #
36
+ # block_content - The text for the content block
37
+ # to parse.
38
+ #
39
+ # Returns a `CaptionBlock`
40
+ def parse_caption_block(block_content)
41
+ lines = block_content.split("\n")
42
+
43
+ time_line = lines[1]
44
+ times = time_line.split("-->")
45
+ times = times.map { |t| t.strip }
46
+
47
+ start_time = subrip_time_to_ms(times[0])
48
+ end_time = subrip_time_to_ms(times[1])
49
+ content = lines[2..-1].join("\n")
50
+
51
+ CaptionBlock.new(start_time, end_time, content)
52
+ end
53
+
54
+ # Internal: Converts a subrip time code to milliseconds
55
+ #
56
+ # subrip_time - The subrip time string
57
+ def subrip_time_to_ms(subrip_time)
58
+ seconds, millis = subrip_time.split(",")
59
+ hours, minutes, seconds = seconds.split(":")
60
+
61
+ millis.to_i + (seconds.to_i * 1000) + (minutes.to_i * 60000) + (hours.to_i * 3600000)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module MovingWords
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,6 @@
1
+ require_relative 'moving_words/version'
2
+ require_relative 'moving_words/caption_block'
3
+ require_relative 'moving_words/srt_parser'
4
+
5
+ module MovingWords
6
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'moving_words/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "moving_words"
8
+ spec.version = MovingWords::VERSION
9
+ spec.authors = ["Alan Johnson"]
10
+ spec.email = ["alan@commondream.net"]
11
+ spec.description = %q{Parse and work with captions.}
12
+ spec.summary = %q{Parse and work with captions. Supports SRT for now.}
13
+ spec.homepage = "https://github.com/commondream/moving_words"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.14.1"
24
+ end
@@ -0,0 +1 @@
1
+ require_relative '../lib/moving_words'
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ module Caption
4
+ describe SrtParser do
5
+ describe "#parse" do
6
+ let(:parser) { SrtParser.new <<EOF
7
+ 1
8
+ 00:00:00,000 --> 00:00:13,000
9
+ [?Jazzy music playing?]
10
+
11
+ 2
12
+ 00:00:14,000 --> 00:00:16,000
13
+ In the previous lesson, we learned how to encode
14
+
15
+ 3
16
+ 00:00:16,000 --> 00:00:18,000
17
+ and embed a video into our page.
18
+
19
+ 4
20
+ 00:00:18,000 --> 00:00:23,000
21
+ In this lesson, we're going to learn about the JavaScript API for interacting with our videos
22
+ EOF
23
+ }
24
+
25
+ it "returns the correct number of blocks" do
26
+ expect(parser.parse.length).to eq(4)
27
+ end
28
+ end
29
+
30
+ describe "#parse_caption_block" do
31
+ let(:parser) { SrtParser.new("") }
32
+
33
+ let(:single_line) {
34
+ parser.parse_caption_block <<EOF
35
+ 3
36
+ 00:00:16,012 --> 00:00:18,293
37
+ and embed a video into our page.
38
+ EOF
39
+ }
40
+
41
+ let(:multi_line) {
42
+ parser.parse_caption_block <<EOF
43
+ 3
44
+ 00:00:16,012 --> 00:00:18,293
45
+ and embed a video into our page
46
+ and then embed another video for kicks.
47
+ EOF
48
+ }
49
+
50
+ it "parses the start_time correctly" do
51
+ expect(single_line.start_time).to eq(16012)
52
+ end
53
+
54
+ it "parses the end_time correctly" do
55
+ expect(single_line.end_time).to eq(18293)
56
+ end
57
+
58
+ it "handles single line content correctly" do
59
+ expect(single_line.content).to eq("and embed a video into our page.")
60
+ end
61
+
62
+ it "handles multi-line content correctly" do
63
+ expect(multi_line.content).to eq("and embed a video into our page\nand then embed another video for kicks.")
64
+ end
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moving_words
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alan Johnson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-10 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.1
55
+ description: Parse and work with captions.
56
+ email:
57
+ - alan@commondream.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/moving_words.rb
68
+ - lib/moving_words/caption_block.rb
69
+ - lib/moving_words/srt_parser.rb
70
+ - lib/moving_words/version.rb
71
+ - moving_words.gemspec
72
+ - spec/spec_helper.rb
73
+ - spec/srt_parser_spec.rb
74
+ homepage: https://github.com/commondream/moving_words
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Parse and work with captions. Supports SRT for now.
98
+ test_files:
99
+ - spec/spec_helper.rb
100
+ - spec/srt_parser_spec.rb
101
+ has_rdoc: