webvtt 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in webvtt.gemspec
4
+ gemspec
5
+
6
+ gem 'rake'
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :test do
5
+ watch(%r{^lib/webvtt/(.+)\.rb$}) { |m| "test/#{m[1]}_test.rb" }
6
+ watch(%r{^test/.+_test\.rb$})
7
+ watch('test/test_helper.rb') { "test" }
8
+ callback(:run_on_changes_begin) { puts "\n", '='*70, "\n" }
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 North Carolina State University
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # WEBVTT FILE parser for Ruby
2
+
3
+ Parse WebVTT files with Ruby.
4
+ The main goal is just to extract the text out with start and end times.
5
+ The parsing does not conform the [WebVTT specification](http://dev.w3.org/html5/webvtt/) at this point.
6
+
7
+
8
+ # Story
9
+
10
+ We were creating WebVTT files to enable closed captions for HTML5 video.
11
+ We wanted to reuse this work to embed the transcript in the video player
12
+ page to increase SEO and allow for jumping to sections of the video.
13
+
14
+ # Install
15
+
16
+ ```
17
+ git clone
18
+ rake build
19
+ gem install pkg/webvtt.x.x.x.gem
20
+ ```
21
+
22
+ # Usage
23
+
24
+ Based on this WebVTT:
25
+
26
+ ```
27
+ WEBVTT
28
+
29
+ 1
30
+ 00:00:00 --> 00:00:03.000 D:vertical A:start
31
+ I grew up in Eastern North Carolina, <b>Edgecombe</b> County
32
+
33
+ 2
34
+ 00:00:03.300 --> 00:00:07.800 A:start
35
+ on a tobacco and dairy farm outside of Tarboro.
36
+ ```
37
+
38
+ You can do the following:
39
+
40
+
41
+ ```
42
+ require 'webvtt'
43
+ vtt = Webvtt::File.new('path/to/file.vtt')
44
+ vtt.cues.class #=> Webvtt::Cue
45
+ vtt.cues[0].start #=> '00:00:00'
46
+ vtt.cues[0].end #=> '00:00:03.000'
47
+ vtt.cues[0].identifier #=> '1'
48
+ vtt.cues[0].settings #=> 'D:vertical A:start'
49
+ vtt.cues[0].text #=> 'I grew up in Eastern North Carolina, <b>Edgecombe</b> County'
50
+ ```
51
+
52
+ # Author
53
+
54
+ Jason Ronallo
55
+
56
+ # License
57
+
58
+ See LICENSE
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/*_test.rb']
9
+ end
10
+
11
+ desc "Run tests"
12
+ task :default => :test
data/lib/webvtt/cue.rb ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Webvtt
3
+ class Cue
4
+ attr_accessor :identifier, :start, :end, :settings, :text
5
+ def initialize(opts={})
6
+ if !opts.empty?
7
+ @identifier = opts[:identifier]
8
+ @text = opts[:text]
9
+ cue_parts = opts[:cue_line].split('-->')
10
+ @start = cue_parts.first.strip
11
+ remaining_cue_parts = cue_parts.last.split(" ")
12
+ @end = remaining_cue_parts.shift.strip
13
+ @settings = remaining_cue_parts.join(' ')
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,72 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Webvtt
3
+ class File
4
+
5
+ attr_accessor :file, :cues
6
+
7
+ def initialize(input_file)
8
+ if input_file.is_a?(String)
9
+ input_file = input_file.encode('UTF-8')
10
+ if ::File.exist?(input_file)
11
+ @file = ::File.read(input_file)
12
+ else
13
+ @file = input_file
14
+ end
15
+ elsif input_file.is_a?(::File)
16
+ @file = input_file.read
17
+ else
18
+ raise
19
+ end
20
+ @cues = []
21
+ parse
22
+ end
23
+
24
+ def parse
25
+ remove_bom
26
+ if !webvtt_line?(file.lines.first)
27
+ raise Webvtt::MalformedError
28
+ end
29
+ collected_lines = []
30
+ file_lines = file.dup.lines.to_a
31
+
32
+ file_lines.each_with_index do |line,index|
33
+ line.chomp!
34
+
35
+ next if webvtt_line?(line)
36
+ if line.empty?
37
+ add_a_cue(collected_lines) if !collected_lines.empty?
38
+ collected_lines = []
39
+ elsif !line.empty? and file_lines.length == (index + 1)
40
+ collected_lines << line
41
+ add_a_cue(collected_lines)
42
+ else
43
+ collected_lines << line
44
+ end
45
+ end
46
+ end
47
+
48
+ def webvtt_line?(line)
49
+ line[0,6] == 'WEBVTT'
50
+ end
51
+
52
+ def remove_bom
53
+ file.gsub!("\uFEFF", '')
54
+ end
55
+
56
+ private
57
+
58
+ def add_a_cue(collected_lines)
59
+ cue_opts = {}
60
+ if collected_lines.first.include?('-->')
61
+ cue_opts[:identifier] = nil
62
+ cue_opts[:cue_line] = collected_lines.first
63
+ elsif collected_lines[1].include?('-->')
64
+ cue_opts[:identifier] = collected_lines.first
65
+ cue_opts[:cue_line] = collected_lines[1]
66
+ end
67
+ cue_opts[:text] = collected_lines[2..-1].join('')
68
+ cues << Cue.new(cue_opts)
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Webvtt
3
+ VERSION = "0.0.1"
4
+ end
data/lib/webvtt.rb ADDED
@@ -0,0 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "webvtt/version"
3
+
4
+ module Webvtt
5
+ # Your code goes here...
6
+ end
7
+
8
+ require 'webvtt/file'
9
+ require 'webvtt/cue'
data/test/cue_test.rb ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'test_helper'
3
+
4
+ class WebvttCueTest < Test::Unit::TestCase
5
+ test "should have a Cue class" do
6
+ assert Webvtt::Cue
7
+ end
8
+
9
+ test "should respond to identifier, start, end, settings, and text" do
10
+ cue = Webvtt::Cue.new
11
+ assert cue.respond_to?(:identifier)
12
+ assert cue.respond_to?(:start)
13
+ assert cue.respond_to?(:end)
14
+ assert cue.respond_to?(:settings)
15
+ assert cue.respond_to?(:text)
16
+ end
17
+
18
+ test "parses an individual cue" do
19
+ cue = Webvtt::Cue.new(:identifier => "1", :cue_line => "00:00:00 --> 00:00:03.000 D:vertical A:start", :text => "I grew up in Eastern North Carolina, <b>Edgecombe</b> County")
20
+ assert_equal '1', cue.identifier
21
+ assert_equal '00:00:00', cue.start
22
+ assert_equal '00:00:03.000', cue.end
23
+ assert_equal 'D:vertical A:start', cue.settings
24
+ assert_equal 'I grew up in Eastern North Carolina, <b>Edgecombe</b> County', cue.text
25
+ end
26
+
27
+ end
@@ -0,0 +1,10 @@
1
+ WEBVTT
2
+
3
+ 1
4
+ 00:00:00 --> 00:00:03.000 D:vertical A:start
5
+ I grew up in Eastern North Carolina, <b>Edgecombe</b> County
6
+
7
+ 2
8
+ 00:00:03.300 --> 00:00:07.800 A:start
9
+ on a tobacco and dairy farm outside of
10
+ Tarboro.
data/test/file_test.rb ADDED
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ class WebvttFileTest < Test::Unit::TestCase
4
+
5
+ setup do
6
+ path = File.expand_path(File.dirname(__FILE__))
7
+ @file_path = File.join(path, 'examples', 'tolson.vtt')
8
+ @vtt = Webvtt::File.new(@file_path)
9
+ end
10
+
11
+ test "should have a File class" do
12
+ assert Webvtt::File
13
+ end
14
+
15
+ test "should create a new File with file path" do
16
+ assert Webvtt::File.new(@file_path)
17
+ end
18
+
19
+ test "should create a new File with a File object" do
20
+ file = File.open(@file_path)
21
+ assert Webvtt::File.new(file)
22
+ end
23
+
24
+ test "should create a new File with a String as the contents of the WEBVTT file" do
25
+ file = "WEBVTT FILE\n\n1\n00:00:00 --> 00:00:05\nIt was a frightening time but it also was a time of great student intrigue"
26
+ assert Webvtt::File.new(file)
27
+ end
28
+
29
+ test "should handle files with a BOM and different line endings" do
30
+ file = "\uFEFFWEBVTT FILE\r\n\r\n1\r\n00:00:00 --> 00:00:05\r\nIt was a frightening time but it also was a time of great student intrigue"
31
+ assert Webvtt::File.new(file)
32
+ end
33
+
34
+ test "should have a file method" do
35
+ assert @vtt.respond_to?(:file)
36
+ end
37
+
38
+ test "should have a file method with the contents of the original file" do
39
+ assert @vtt.file.include?('WEBVTT')
40
+ end
41
+
42
+ test "should have a list of cues" do
43
+ assert @vtt.respond_to?(:cues)
44
+ assert @vtt.cues.is_a?(Array)
45
+ end
46
+
47
+ test "should have 2 cues" do
48
+ assert_equal 2, @vtt.cues.length
49
+ end
50
+
51
+ test "first cue should be correct" do
52
+ first_cue = @vtt.cues.first
53
+ assert_equal "1", first_cue.identifier
54
+ assert_equal "00:00:00", first_cue.start
55
+ assert_equal "00:00:03.000", first_cue.end
56
+ assert_equal "D:vertical A:start", first_cue.settings
57
+ assert_equal "I grew up in Eastern North Carolina, <b>Edgecombe</b> County", first_cue.text
58
+ end
59
+
60
+ test "multiline cue text should be correct" do
61
+ assert_equal "on a tobacco and dairy farm outside of Tarboro.", @vtt.cues.last.text
62
+ end
63
+
64
+ end
@@ -0,0 +1,5 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'rubygems'
3
+ require 'test-unit'
4
+ require 'webvtt'
5
+
data/webvtt.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "webvtt/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "webvtt"
7
+ s.version = Webvtt::VERSION
8
+ s.authors = ["Jason Ronallo"]
9
+ s.email = ["jronallo@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{WEBVTT file parser in Ruby}
12
+ s.description = %q{WEBVTT file parser in Ruby}
13
+
14
+ s.rubyforge_project = "webvtt"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ s.add_development_dependency "test-unit", [">= 2.5.0"]
21
+ s.add_development_dependency "guard-test", [">= 0.5.0"]
22
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webvtt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Ronallo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: test-unit
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.5.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.5.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: guard-test
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.5.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.5.0
46
+ description: WEBVTT file parser in Ruby
47
+ email:
48
+ - jronallo@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - Guardfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - lib/webvtt.rb
60
+ - lib/webvtt/cue.rb
61
+ - lib/webvtt/file.rb
62
+ - lib/webvtt/version.rb
63
+ - test/cue_test.rb
64
+ - test/examples/tolson.vtt
65
+ - test/file_test.rb
66
+ - test/test_helper.rb
67
+ - webvtt.gemspec
68
+ homepage: ''
69
+ licenses: []
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project: webvtt
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: WEBVTT file parser in Ruby
92
+ test_files: []
93
+ has_rdoc: