subtt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +13 -0
- data/LICENSE +21 -0
- data/README.md +14 -0
- data/bin/smi2srt +11 -0
- data/lib/subtt.rb +4 -0
- data/lib/subtt/converter.rb +22 -0
- data/lib/subtt/duration.rb +37 -0
- data/lib/subtt/format/sami.rb +57 -0
- data/lib/subtt/version.rb +3 -0
- metadata +55 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 918127ea062b1e0a5ea6f0c25108c54a4dc0cf7d
|
4
|
+
data.tar.gz: ec87b52a0da305c63286b36b5072614f4520239d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3a3908692e0694a0abc97ba8964189baa5de83bbd27d6939dcd286353e26602c5106a9525766a746d52eeed968f34964e55bf42c0c5c306d2da7148053f78da0
|
7
|
+
data.tar.gz: c450b8c7ed37b6b40de5af281c5ad681671333329ce69b9a9bc565035e62b60cd2b47bf6edc5a83124302c59c5849c0dd66176f86e9280dac88b09426c384ee5
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 buo
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# subtt
|
2
|
+
|
3
|
+
Subtt is a command line tool to help you convert a subtitle file into another format.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```sh
|
8
|
+
smi2srt example.smi
|
9
|
+
```
|
10
|
+
|
11
|
+
## Supported File Formats
|
12
|
+
|
13
|
+
- `.smi`, `.sami` - [SAMI](https://en.wikipedia.org/wiki/SAMI)
|
14
|
+
- `.srt` - [SubRip](https://en.wikipedia.org/wiki/SubRip)
|
data/bin/smi2srt
ADDED
data/lib/subtt.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Subtt
|
2
|
+
def self.smi2srt(smi_path)
|
3
|
+
srt_path = smi_path.gsub(/\.smi/, '.srt')
|
4
|
+
puts "converted into #{srt_path}"
|
5
|
+
File.open(srt_path, 'w') do |srt|
|
6
|
+
smi = SAMI.load smi_path
|
7
|
+
|
8
|
+
duration = 5000
|
9
|
+
smi.each_with_index do |e, i|
|
10
|
+
from = Duration.new(e[0])
|
11
|
+
to = Duration.new(e[0] + duration)
|
12
|
+
|
13
|
+
nextSync = smi.fetch(i+1)
|
14
|
+
if !nextSync.nil? and nextSync[0] <= to
|
15
|
+
to.set nextSync[0] - 10
|
16
|
+
end
|
17
|
+
|
18
|
+
srt.write "#{i+1}\n#{from} --> #{to}\n#{e[1]}\n\n"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Subtt
|
2
|
+
class Duration
|
3
|
+
include Comparable
|
4
|
+
attr :ms, :hours, :minutes, :seconds, :miliseconds
|
5
|
+
|
6
|
+
def initialize(other)
|
7
|
+
set other
|
8
|
+
end
|
9
|
+
|
10
|
+
def coerce(other)
|
11
|
+
return Duration.new(other), self
|
12
|
+
end
|
13
|
+
|
14
|
+
def set(other)
|
15
|
+
@ms = other
|
16
|
+
|
17
|
+
@hours = (other / 3600000).to_i
|
18
|
+
other -= @hours * 3600000
|
19
|
+
|
20
|
+
@minutes = (other / 60000).to_i
|
21
|
+
other -= @minutes * 60000
|
22
|
+
|
23
|
+
@seconds = (other / 1000).to_i
|
24
|
+
other -= @seconds * 1000
|
25
|
+
|
26
|
+
@miliseconds = other
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_s
|
30
|
+
"%02d:%02d:%02d,%03d" % [@hours, @minutes, @seconds, @miliseconds]
|
31
|
+
end
|
32
|
+
|
33
|
+
def <=>(other)
|
34
|
+
@ms <=> other.ms
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'iconv'
|
2
|
+
|
3
|
+
module Subtt
|
4
|
+
class SAMI
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
def self.load(file)
|
8
|
+
smi = SAMI.new
|
9
|
+
smi.parse(File.binread(file))
|
10
|
+
smi
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse(txt)
|
14
|
+
@txt = txt
|
15
|
+
@sync = []
|
16
|
+
|
17
|
+
# TODO detect encoding
|
18
|
+
txt = Iconv.iconv('UTF-8', 'CP949', txt).join
|
19
|
+
txt.scan(/<SYNC Start=(\d+)><P Class=.+>((?:.|[\r\n])*?)(?:<SYNC|<\/BODY)/) do |m|
|
20
|
+
t, s = m[0].to_i, m[1]
|
21
|
+
|
22
|
+
# <br> tag
|
23
|
+
s = s.gsub(/<br( \/)?>/, "\n")
|
24
|
+
|
25
|
+
# squash multiple newline
|
26
|
+
s = s.gsub(/\r\n/, "\n")
|
27
|
+
s = s.gsub(/[\n]{2,}/, "\n")
|
28
|
+
|
29
|
+
# strip HTML tags
|
30
|
+
s = s.gsub(/<.*?>/, '')
|
31
|
+
|
32
|
+
# strip HTML entities
|
33
|
+
s = s.gsub(/&[a-z]+;/, ' ')
|
34
|
+
|
35
|
+
s = s.strip
|
36
|
+
|
37
|
+
@sync << [t, s]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def each
|
42
|
+
@sync.each do |t, s|
|
43
|
+
yield t, s
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def fetch(n)
|
48
|
+
@sync[n] if n < @sync.length
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
@sync.each do |t, s|
|
53
|
+
puts "[#{t}]", s, "\r\n"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: subtt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- buo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A command line tool for converting a subtitle file into another format.
|
14
|
+
email:
|
15
|
+
- buo@users.noreply.github.com
|
16
|
+
executables:
|
17
|
+
- smi2srt
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- Gemfile
|
22
|
+
- Gemfile.lock
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- bin/smi2srt
|
26
|
+
- lib/subtt.rb
|
27
|
+
- lib/subtt/converter.rb
|
28
|
+
- lib/subtt/duration.rb
|
29
|
+
- lib/subtt/format/sami.rb
|
30
|
+
- lib/subtt/version.rb
|
31
|
+
homepage: https://github.com/buo/subtt
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata: {}
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 2.0.14
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: A command line tool for converting subtitle files.
|
55
|
+
test_files: []
|