ttml 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *~
data/README.md CHANGED
@@ -2,9 +2,19 @@
2
2
 
3
3
  Very simple Timed Text Markup Language parsing - I needed to parse a ttml file
4
4
  and couldn't find a ruby implementation (probably because you don't really
5
- need one!), so I wrote one. In the future I may support writing a ttml file
5
+ need one!), so I wrote it. In the future I may support writing a ttml file
6
6
  and/or a better, richer API.
7
7
 
8
+ Also, I needed to convert ttml to SubRip, so a little binary was born, ttml2srt;
9
+ It's run like
10
+
11
+ $ ttml2srt filename
12
+
13
+ and outputs some .srt formatted subtitles to standard output.
14
+ **Please note that it does a very (my)application dependent stripping of
15
+ html tags from the input, so you may have to check the source to get
16
+ out valid SubRip output.**
17
+
8
18
  ## Installation
9
19
 
10
20
  Add this line to your application's Gemfile:
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'ttml'
5
+
6
+ raise "Please pass filename[s] to parse" if ARGV.empty?
7
+
8
+ def self.conv2srt fname
9
+ doc = Ttml::Document.new(fname) || raise("No doc at #{ fname }")
10
+ counter = 1
11
+ doc.subtitle_stream.each do |sub|
12
+ # Avoid outputing empty lines
13
+ cleaned = sub.content.sub(/.+00">([^<]*)<.+/, '\1')
14
+ next if cleaned.empty?
15
+ puts <<EOS
16
+ #{ counter }
17
+ #{ Ttml::Util.smpte_time(sub['begin']) } --> #{ Ttml::Util.smpte_time(sub['end']) }
18
+
19
+ #{ cleaned }
20
+
21
+ EOS
22
+ counter += 1
23
+ end
24
+ end
25
+
26
+ while fname = ARGV.shift
27
+ conv2srt(fname)
28
+ end
@@ -2,6 +2,23 @@ require "ttml/version"
2
2
  require "nokogiri"
3
3
 
4
4
  module Ttml
5
+
6
+ # Some stuff that helps but is not directly about getting nodes out of it
7
+ class Util
8
+ # takes a string offset (as found in ttml nodes) and returns smpte string;
9
+ # Used in converting to SubRip.
10
+ #
11
+ # Ex: 0.0s => 00:00:00.0
12
+ # Ex: 63.5s => 00:01:03.500
13
+ def self.smpte_time offset
14
+ hours = minutes = seconds = millisecs = 0
15
+ f_off = offset.to_s.sub(/s$/, '').to_f
16
+ millisecs = ((f_off - f_off.to_i) * 1000).to_i
17
+ minutes, seconds = f_off.divmod(60)
18
+ hours, minutes = minutes.divmod(60)
19
+ "#{ sprintf('%02d', hours) }:#{ sprintf('%02d', minutes) }:#{ sprintf('%02d', seconds) },#{ millisecs }"
20
+ end
21
+ end
5
22
  # Minimal Timed Text Markup Language parsing and extraction.
6
23
  #
7
24
  # Example:
@@ -9,9 +26,9 @@ module Ttml
9
26
  # => [Ttml::Document]
10
27
  # >> doc.copyright
11
28
  # => '(c) 2012 loop23'
12
- # >> doc.subtitles
13
- # => [All subtitles]
14
- # >> doc.subtitles(0.0, 100.0)
29
+ # >> doc.subtitle_stream
30
+ # => [All subtitles as Nokogiri::Node instances]
31
+ # >> doc.subtitle_stream(0.0, 100.0)
15
32
  # => [Subtitles from beginning to 100 seconds]
16
33
  class Document
17
34
 
@@ -1,3 +1,3 @@
1
1
  module Ttml
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,17 +1,17 @@
1
- <?xml version='1.0'?><ns2:tt xmlns:ns4="http://www.w3.org/2006/10/ttaf1#parameter" xmlns:ns3="http://www.w3.org/2006/10/ttaf1#style" xmlns:ns2="http://www.w3.org/2006/10/ttaf1" xmlns:ns1="http://www.w3.org/2006/10/ttaf1#metadata" xml:lang="it"><ns2:head><ns1:title>Timed Text DFPX</ns1:title>
2
- <ns1:copyright>2010 (c) PerVoice S.p.A.</ns1:copyright>
3
- <ns2:styling><ns2:style xml:id="s1" ns3:textAlign="left" ns3:fontSize="22px" ns3:fontFamily="proportionalSansSerif" ns3:color="yellow"/>
4
- <ns2:style xml:id="s2" ns3:color="yellow" style="s1"/>
5
- <ns2:style xml:id="s1Right" ns3:textAlign="end" style="s1"/>
6
- <ns2:style xml:id="s2Left" ns3:textAlign="start" style="s2"/>
7
- </ns2:styling>
8
- <ns2:layout><ns2:region ns3:padding="5px 3px" ns3:extent="560px 62px" ns3:displayAlign="after" ns3:backgroundColor="black" style="s1" xml:id="subtitleArea"/>
9
- </ns2:layout>
10
- </ns2:head>
11
- <ns2:body region="subtitleArea"><ns2:div><ns2:p end="104.34s" begin="104.34s"><![CDATA[<p align="left" ><font color="#ffa500">Sono</font></p>]]></ns2:p>
12
- <ns2:p end="107.76s" begin="104.34s"><![CDATA[<p align="left" ><font color="#ffa500"></font></p>]]></ns2:p>
13
- <ns2:p end="117.8s" begin="107.76s"><![CDATA[<p align="left" ><font color="#ffa500">presenti 25</font></p>]]></ns2:p>
14
- <ns2:p end="117.8s" begin="117.8s"><![CDATA[<p align="left" ><font color="#ffa500">Consiglieri,</font></p>]]></ns2:p>
15
- </ns2:div>
16
- </ns2:body>
17
- </ns2:tt>
1
+ <?xml version='1.0'?><ns2:tt xmlns:ns4="http://www.w3.org/2006/10/ttaf1#parameter" xmlns:ns3="http://www.w3.org/2006/10/ttaf1#style" xmlns:ns2="http://www.w3.org/2006/10/ttaf1" xmlns:ns1="http://www.w3.org/2006/10/ttaf1#metadata" xml:lang="it"><ns2:head><ns1:title>Timed Text DFPX</ns1:title>
2
+ <ns1:copyright>2010 (c) PerVoice S.p.A.</ns1:copyright>
3
+ <ns2:styling><ns2:style xml:id="s1" ns3:textAlign="left" ns3:fontSize="22px" ns3:fontFamily="proportionalSansSerif" ns3:color="yellow"/>
4
+ <ns2:style xml:id="s2" ns3:color="yellow" style="s1"/>
5
+ <ns2:style xml:id="s1Right" ns3:textAlign="end" style="s1"/>
6
+ <ns2:style xml:id="s2Left" ns3:textAlign="start" style="s2"/>
7
+ </ns2:styling>
8
+ <ns2:layout><ns2:region ns3:padding="5px 3px" ns3:extent="560px 62px" ns3:displayAlign="after" ns3:backgroundColor="black" style="s1" xml:id="subtitleArea"/>
9
+ </ns2:layout>
10
+ </ns2:head>
11
+ <ns2:body region="subtitleArea"><ns2:div><ns2:p end="104.34s" begin="104.34s"><![CDATA[<p align="left" ><font color="#ffa500">Sono</font></p>]]></ns2:p>
12
+ <ns2:p end="107.76s" begin="104.34s"><![CDATA[<p align="left" ><font color="#ffa500"></font></p>]]></ns2:p>
13
+ <ns2:p end="117.8s" begin="107.76s"><![CDATA[<p align="left" ><font color="#ffa500">presenti 25</font></p>]]></ns2:p>
14
+ <ns2:p end="117.8s" begin="117.8s"><![CDATA[<p align="left" ><font color="#ffa500">Consiglieri,</font></p>]]></ns2:p>
15
+ </ns2:div>
16
+ </ns2:body>
17
+ </ns2:tt>
File without changes
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'test/unit'
3
+ require 'ttml'
4
+
5
+ class UtilTest < Test::Unit::TestCase
6
+
7
+ def test_smpte
8
+ assert_equal "00:00:00,0", Ttml::Util.smpte_time('0s')
9
+ assert_equal "00:00:00,0", Ttml::Util.smpte_time('0.0s')
10
+ assert_equal "00:01:00,0", Ttml::Util.smpte_time('60.0s')
11
+ assert_equal "00:01:01,0", Ttml::Util.smpte_time('61.0s')
12
+ assert_equal "00:00:00,500", Ttml::Util.smpte_time('0.5s')
13
+ end
14
+
15
+ end
metadata CHANGED
@@ -1,75 +1,94 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ttml
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.5
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Luca S.G. de Marinis
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-09-14 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
17
+
18
+ date: 2013-12-26 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: nokogiri
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
22
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
23
+ requirement: &id001 !ruby/object:Gem::Requirement
25
24
  none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
30
34
  description: Parse a ttml file
31
- email:
35
+ email:
32
36
  - loop23@gmail.com
33
- executables: []
37
+ executables:
38
+ - ttml2srt
34
39
  extensions: []
40
+
35
41
  extra_rdoc_files: []
36
- files:
42
+
43
+ files:
37
44
  - .gitignore
38
45
  - Gemfile
39
46
  - LICENSE
40
47
  - README.md
41
48
  - Rakefile
49
+ - bin/ttml2srt
42
50
  - lib/ttml.rb
43
51
  - lib/ttml/version.rb
44
52
  - test/sample.xml
45
53
  - test/sample_2.xml
46
- - test/test_ttml.rb
54
+ - test/ttml_test.rb
55
+ - test/util_test.rb
47
56
  - ttml.gemspec
48
57
  homepage: http://github.com/loop23/ttml
49
58
  licenses: []
59
+
50
60
  post_install_message:
51
61
  rdoc_options: []
52
- require_paths:
62
+
63
+ require_paths:
53
64
  - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
65
+ required_ruby_version: !ruby/object:Gem::Requirement
55
66
  none: false
56
- requirements:
57
- - - ! '>='
58
- - !ruby/object:Gem::Version
59
- version: '0'
60
- required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
75
  none: false
62
- requirements:
63
- - - ! '>='
64
- - !ruby/object:Gem::Version
65
- version: '0'
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
66
83
  requirements: []
84
+
67
85
  rubyforge_project:
68
86
  rubygems_version: 1.8.24
69
87
  signing_key:
70
88
  specification_version: 3
71
89
  summary: Minimal parsing for timed text markup language (http://www.w3.org/TR/ttaf1-dfxp/)
72
- test_files:
90
+ test_files:
73
91
  - test/sample.xml
74
92
  - test/sample_2.xml
75
- - test/test_ttml.rb
93
+ - test/ttml_test.rb
94
+ - test/util_test.rb