subtitle_shifter 1.0.0.0 → 1.1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/subtitle_shifter.rb +59 -3
- data/subtitle_shifter.gemspec +64 -0
- metadata +12 -11
data/lib/subtitle_shifter.rb
CHANGED
@@ -1,25 +1,72 @@
|
|
1
1
|
|
2
2
|
# encoding: UTF-8
|
3
3
|
|
4
|
+
#@example Manipulate subtitles from the command line
|
5
|
+
# $ subtitle_shifter --help
|
6
|
+
# $ subtitle_shifter --operation add --index 12 --time 2,345 source.srt dest.srt
|
7
|
+
#@example Manipulate subtitles from within a ruby program
|
8
|
+
# # This will shift all subtitles from index 12 onward by 2.345 seconds
|
9
|
+
# # or 2345 milliseconds
|
10
|
+
# subs = SubtitleShifter.new('mysubs.srt')
|
11
|
+
# subs.parse
|
12
|
+
# subs.shift(:index => 12, :time => 2345)
|
13
|
+
#@example Shift subtitles backward
|
14
|
+
# # This will shift subtitles backward, beware - you cannot shift
|
15
|
+
# # subtitles backward so that they overlap the preceding subtitles.
|
16
|
+
# # A RuntimeError exception will be raised if this occurs.
|
17
|
+
# subs.shift(:index => 12, :time => -2345) # Simply provide a negative time value
|
18
|
+
#@example Output subtitles once they've been parsed and/or manipulated
|
19
|
+
# puts subs
|
20
|
+
# # -- or --
|
21
|
+
# subs.to_s
|
22
|
+
#@see http://en.wikipedia.org/wiki/Subrip Wikipedia's article on the SubRip format
|
4
23
|
class SubtitleShifter
|
5
24
|
module Version
|
6
25
|
MAJOR = 1
|
7
|
-
MINOR =
|
26
|
+
MINOR = 1
|
8
27
|
PATCH = 0
|
9
28
|
BUILD = 0
|
10
29
|
|
11
30
|
STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
|
12
31
|
end
|
13
32
|
|
14
|
-
|
15
|
-
|
33
|
+
# A boolean flag highlighting whether or not subtitles have been parsed yet
|
34
|
+
attr_reader :parsed_ok
|
35
|
+
# A hash of the parsed subtitles. You normally wouldn't need to access this directly
|
36
|
+
# @example The format of the hash is as follows
|
37
|
+
# {1 => {:start => 107,
|
38
|
+
# :end => 5762,
|
39
|
+
# :subtitle => 'This is the first subtitle'
|
40
|
+
# },
|
41
|
+
# {2 => {:start => 5890,
|
42
|
+
# :end => 10553,
|
43
|
+
# :subtitle => 'This is the second subtitle'
|
44
|
+
# }
|
45
|
+
# @note I chose to implement internal representation of subtitle files as a hash
|
46
|
+
# and not an array, which would've been more efficient, as subtitles cannot be
|
47
|
+
# guaranteed to start at index 1
|
48
|
+
# That being said, I can already think of a way to do this using an array and offset attribute
|
49
|
+
attr_reader :subtitles
|
50
|
+
|
51
|
+
# The delimiter used for separating SubRip time stamps
|
16
52
|
TIME_SEPERATOR = '-->'
|
17
53
|
|
54
|
+
# @param [String] file A string of the file name
|
55
|
+
# @param [String] linebreak A string of the linebreak pattern.
|
18
56
|
def initialize(file, linebreak = "\r\n")
|
19
57
|
@sub_file = file
|
20
58
|
@linebreak = linebreak
|
59
|
+
@parsed_ok = false
|
21
60
|
end
|
22
61
|
|
62
|
+
# Parses the subtitles
|
63
|
+
# @note Always call only after initialising.
|
64
|
+
# @note If your subtitle file is UTF-8 encoded, and has a Byte Order Mark as its first few bytes,
|
65
|
+
# the BOM will not be preserved when outputing the parsed and shifted subtitles. You probably don't
|
66
|
+
# need it anyway
|
67
|
+
# @example
|
68
|
+
# sub.parse if sub.parsed_ok
|
69
|
+
# @see http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark Wikipedia's article on UTF-8 and BOM
|
23
70
|
def parse
|
24
71
|
raw_text = File.open(@sub_file, 'r').read.force_encoding('UTF-8')
|
25
72
|
raw_text.gsub!("\xEF\xBB\xBF".force_encoding("UTF-8"), '') #Remove stupid BOM that was causing me so much grief!
|
@@ -37,6 +84,12 @@ class SubtitleShifter
|
|
37
84
|
@parsed_ok = true # Not very useful, but will help when error checking is added
|
38
85
|
end
|
39
86
|
|
87
|
+
# Shifts subtitles forward (or backward) by a number of ms from an index
|
88
|
+
# @param [Integer] :index The index of the subtitle
|
89
|
+
# @param [Integer] :time The time (in ms) by which you wish to shift the subtitles. A negative value will shift backwards.
|
90
|
+
# @example
|
91
|
+
# sub.shift(:index => 42, :time => 10000) # Shift subs from index 42 onwards by 10 seconds.
|
92
|
+
# @raise [RuntimeError] Raises this exception when shifting backwards if index and index-1 time's overlap
|
40
93
|
def shift(args)
|
41
94
|
first = args[:index] # used for checking first go round.
|
42
95
|
index = first
|
@@ -58,6 +111,9 @@ class SubtitleShifter
|
|
58
111
|
end
|
59
112
|
end
|
60
113
|
|
114
|
+
# Outputs parsed subtitles
|
115
|
+
# @raise [RuntimeError] Will raise this exception if an attempt is made to output the subs before parsing has taken place
|
116
|
+
# @see SubtitleShifter#parsed_ok
|
61
117
|
def to_s
|
62
118
|
raise RuntimeError, 'File has not been parsed yet' unless @parsed_ok
|
63
119
|
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{subtitle_shifter}
|
8
|
+
s.version = "1.1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = [%q{Gabriel Fortuna}]
|
12
|
+
s.date = %q{2011-12-13}
|
13
|
+
s.description = %q{A simple little gem for modifying SubRip subtitle files. Done as a mentoring exercise with Citizen428, and taken from the Ruby Programming Challenge For Newbies}
|
14
|
+
s.email = %q{gee.forr@gmail.com}
|
15
|
+
s.executables = [%q{shift_subtitle}]
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
"LICENSE.txt",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"bin/shift_subtitle",
|
27
|
+
"lib/.subtitle_shifter.rb.swp",
|
28
|
+
"lib/subtitle_shifter.rb",
|
29
|
+
"subtitle_shifter.gemspec",
|
30
|
+
"test/The.Big.Bang.Theory.srt",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/subtitle_shifter_spec.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/gee-forr/subtitle_shifter}
|
35
|
+
s.licenses = [%q{MIT}]
|
36
|
+
s.require_paths = [%q{lib}]
|
37
|
+
s.rubygems_version = %q{1.8.6}
|
38
|
+
s.summary = %q{A simple library for adjusting SubRip (.srt) subtitles}
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_development_dependency(%q<minitest>, [">= 0"])
|
45
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
46
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
47
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
48
|
+
s.add_development_dependency(%q<rdoc>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<minitest>, [">= 0"])
|
51
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
53
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
54
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<minitest>, [">= 0"])
|
58
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
59
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
60
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
61
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subtitle_shifter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-12-13 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
16
|
-
requirement: &
|
16
|
+
requirement: &70158991265800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70158991265800
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &70158987679900 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.0.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70158987679900
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &70158987678000 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.6.4
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70158987678000
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rcov
|
49
|
-
requirement: &
|
49
|
+
requirement: &70158987676420 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70158987676420
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rdoc
|
60
|
-
requirement: &
|
60
|
+
requirement: &70158987674940 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70158987674940
|
69
69
|
description: A simple little gem for modifying SubRip subtitle files. Done as a mentoring
|
70
70
|
exercise with Citizen428, and taken from the Ruby Programming Challenge For Newbies
|
71
71
|
email: gee.forr@gmail.com
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- bin/shift_subtitle
|
85
85
|
- lib/.subtitle_shifter.rb.swp
|
86
86
|
- lib/subtitle_shifter.rb
|
87
|
+
- subtitle_shifter.gemspec
|
87
88
|
- test/The.Big.Bang.Theory.srt
|
88
89
|
- test/helper.rb
|
89
90
|
- test/subtitle_shifter_spec.rb
|