shift_subtitle 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-09-26
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,18 @@
1
+ bin/shift_subtitle
2
+ History.txt
3
+ lib/rsrt.rb
4
+ lib/rsrt/srt_parser.rb
5
+ lib/rsrt/subtitle.rb
6
+ lib/rsrt/time_range.rb
7
+ lib/shift_subtitle/cli.rb
8
+ Manifest.txt
9
+ PostInstall.txt
10
+ README.rdoc
11
+ Rakefile
12
+ rsrt.gemspec
13
+ script/console
14
+ script/destroy
15
+ script/generate
16
+ test/test_helper.rb
17
+ test/test_subtitle.rb
18
+ test/test_time_range.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,3 @@
1
+ Run shift_subtitle -h to get help on using this application.
2
+
3
+
data/README.rdoc ADDED
@@ -0,0 +1,52 @@
1
+ = ShiftSubtitle
2
+
3
+ * http://github.com/olivernn/ShiftSubtitle
4
+
5
+ == DESCRIPTION:
6
+
7
+ This gem includes the command line application shift_subtitle, a quick application that allows people to shift the times of subtitles in a .srt file.
8
+
9
+ This application was created as part of the RubyLearning Blog competition here http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/
10
+
11
+ == FEATURES/PROBLEMS:
12
+
13
+ Subtitles can be shifted forward or backwards.
14
+ You can start shifting from any subtitle.
15
+
16
+ == SYNOPSIS:
17
+
18
+ run ruby shift_subtitle -h for a full list of options
19
+ -p --operation : either add or sub to add time or subtract time from the subtitles
20
+ -t --time : the amount of time to add or subtract from each subtitle range, this should be in milliseconds using the , as a thousand separator, e.g. 1,500 is 1 and a half seconds
21
+ -s --start_from : optional, the id of the subtitle to start shifting from, defaults to 1
22
+ -i --input_file : the full path to the input .srt file, this must be in the correct format
23
+ -o --output_file : the full path to the output .srt file
24
+
25
+ == INSTALL:
26
+
27
+ sudo gem install shift_subtitle
28
+
29
+ == LICENSE:
30
+
31
+ (The MIT License)
32
+
33
+ Copyright (c) 2009 FIXME full name
34
+
35
+ Permission is hereby granted, free of charge, to any person obtaining
36
+ a copy of this software and associated documentation files (the
37
+ 'Software'), to deal in the Software without restriction, including
38
+ without limitation the rights to use, copy, modify, merge, publish,
39
+ distribute, sublicense, and/or sell copies of the Software, and to
40
+ permit persons to whom the Software is furnished to do so, subject to
41
+ the following conditions:
42
+
43
+ The above copyright notice and this permission notice shall be
44
+ included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
47
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
49
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
50
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
51
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
52
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/rsrt'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'shift_subtitle' do
14
+ self.developer 'Oliver Nightingale', 'oliver.nightingale2@ntlworld.com'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by Oliver Nightingale on 2009-9-28.
4
+ # Copyright (c) 2009. All rights reserved.
5
+
6
+ require 'rubygems'
7
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/rsrt")
8
+ require "rsrt/srt_parser"
9
+ require "rsrt/subtitle"
10
+ require "rsrt/time_range"
11
+ require "shift_subtitle/cli"
12
+
13
+ ShiftSubtitle::CLI.execute(STDOUT, ARGV)
data/lib/rsrt.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Rsrt
5
+ VERSION = '0.1.0'
6
+ end
@@ -0,0 +1,35 @@
1
+ require 'open-uri'
2
+
3
+ class SRTParser
4
+
5
+ attr_reader :subtitles
6
+
7
+ def initialize(srt_file_path)
8
+ @srt_file_path = srt_file_path
9
+ @regex_subtitle_id = Regexp.new('^\d+$')
10
+ @regex_time_range = Regexp.new('^\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}$')
11
+ @regex_text = Regexp.new('^.+$')
12
+ @subtitles = Array.new
13
+ parse
14
+ end
15
+
16
+ private
17
+
18
+ def parse
19
+ subtitle_details = {}
20
+ subtitle_details[:text] = ""
21
+
22
+ File.open(@srt_file_path).each do |line|
23
+ if @regex_subtitle_id.match(line)
24
+ subtitle_details[:id] = line
25
+ elsif @regex_time_range.match(line)
26
+ subtitle_details[:time_range] = line
27
+ elsif @regex_text.match(line)
28
+ subtitle_details[:text] += line
29
+ else # we think this is the end of the subtitle
30
+ @subtitles << Subtitle.new(subtitle_details)
31
+ subtitle_details[:text] = ""
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ class Subtitle
2
+
3
+ attr_reader :start_time, :end_time, :id, :text
4
+
5
+ def initialize(details={})
6
+ @id = details[:id].to_i
7
+ @text = details[:text]
8
+ parse_time_range(details[:time_range])
9
+ end
10
+
11
+ def shift_time_range_forward(shift)
12
+ @start_time.from_ms(@start_time.to_ms + shift)
13
+ @end_time.from_ms(@end_time.to_ms + shift)
14
+ end
15
+
16
+ def shift_time_range_backward(shift)
17
+ @start_time.from_ms(@start_time.to_ms - shift)
18
+ @end_time.from_ms(@end_time.to_ms - shift)
19
+ end
20
+
21
+ def to_s
22
+ @id.to_s + "\n" + @start_time.to_s + " --> " + @end_time.to_s + "\n" + @text.to_s
23
+ end
24
+
25
+ private
26
+
27
+ def parse_time_range(time_range)
28
+ @start_time = TimeRange.new(time_range.split(" --> ")[0])
29
+ @end_time = TimeRange.new(time_range.split(" --> ")[1])
30
+ end
31
+ end
@@ -0,0 +1,67 @@
1
+ class TimeRange
2
+
3
+ attr_accessor :hours, :minutes, :seconds, :millieseconds
4
+
5
+ def initialize(time)
6
+ @time = time
7
+ parse
8
+ end
9
+
10
+ def to_ms
11
+ @millieseconds + (@seconds * 1000) + (@minutes * 60000) + (@hours * 3600000)
12
+ end
13
+
14
+ def from_ms(ms)
15
+ t = Time.at(ms/1000)
16
+ @hours = t.hour - 1 #epoch was at 01:00:00!
17
+ @minutes = t.min
18
+ @seconds = t.sec
19
+ @millieseconds = ms.to_s.reverse.slice(0,3).reverse.to_i
20
+ end
21
+
22
+ def to_s
23
+ hours_to_s + ":" + minutes_to_s + ":" + seconds_to_s + "," + millieseconds_to_s
24
+ end
25
+
26
+ private
27
+
28
+ def parse
29
+ time_split = @time.split(':')
30
+ @hours = time_split[0].to_i
31
+ @minutes = time_split[1].to_i
32
+ @seconds = time_split[2].split(',')[0].to_i
33
+ @millieseconds = time_split[2].split(',')[1].to_i
34
+ end
35
+
36
+ def hours_to_s
37
+ if @hours.to_s.length == 2
38
+ @hours.to_s
39
+ else
40
+ @hours.to_s.rjust(2, "0")
41
+ end
42
+ end
43
+
44
+ def minutes_to_s
45
+ if @minutes.to_s.length == 2
46
+ @minutes.to_s
47
+ else
48
+ @minutes.to_s.rjust(2, "0")
49
+ end
50
+ end
51
+
52
+ def seconds_to_s
53
+ if @seconds.to_s.length == 2
54
+ @seconds.to_s
55
+ else
56
+ @seconds.to_s.rjust(2, "0")
57
+ end
58
+ end
59
+
60
+ def millieseconds_to_s
61
+ if @millieseconds.to_s.length == 3
62
+ @millieseconds.to_s
63
+ else
64
+ @millieseconds.to_s.rjust(3, "0")
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,64 @@
1
+ require 'optparse'
2
+ require 'open-uri'
3
+
4
+ module ShiftSubtitle
5
+ class CLI
6
+ def self.execute(stdout, arguments=[])
7
+
8
+ options = {}
9
+
10
+ optparse = OptionParser.new do |opts|
11
+ opts.banner = "Usage: shift_subtitle.rb [options] input_file output_file"
12
+
13
+ options[:operation] = ""
14
+ opts.on('-p', '--operation OPERATION', 'add or sub') do |operation|
15
+ options[:operation] = operation
16
+ end
17
+
18
+ options[:time] = ""
19
+ opts.on('-t', '--time TIME', 'how much time to shift subtitles by') do |time|
20
+ options[:time] = time
21
+ end
22
+
23
+ options[:start] = 1
24
+ opts.on('-s', '--start_from [START]', 'where to start shifting subtitles from') do |start|
25
+ options[:start] = start
26
+ end
27
+
28
+ options[:input_file] = ""
29
+ opts.on('-i', '--input_file INPUT_FILE', 'the original subtitles') do |input_file|
30
+ options[:input_file] = input_file
31
+ end
32
+
33
+ options[:output_file] =
34
+ opts.on('-o', '--output_file OUTPUT_FILE', 'where to save results') do |output_file|
35
+ options[:output_file] = output_file
36
+ end
37
+ end
38
+
39
+ optparse.parse!
40
+
41
+ @time_shift_ms = options[:time].gsub(",", "").to_i
42
+ @input_file_path = options[:input_file]
43
+ @output_file_path = options[:output_file]
44
+ @operation = options[:operation]
45
+ @start = options[:start].to_i
46
+
47
+ @subtitles = SRTParser.new(@input_file_path).subtitles
48
+
49
+ output_file = File.new(@output_file_path, "w+")
50
+
51
+ @subtitles.each do |subtitle|
52
+ if @operation == "add"
53
+ subtitle.shift_time_range_forward(@time_shift_ms) if subtitle.id >= @start
54
+ elsif @operation == "sub"
55
+ subtitle.shift_time_range_backward(@time_shift_ms) if subtitle.id >= @start
56
+ end
57
+ output_file.puts subtitle.to_s
58
+ output_file.puts "\n"
59
+ end
60
+
61
+ output_file.close
62
+ end
63
+ end
64
+ end
data/rsrt.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{shift_subtitle}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Oliver Nightingale"]
9
+ s.date = %q{2009-09-29}
10
+ s.default_executable = %q{shift_subtitle}
11
+ s.description = %q{This gem includes the command line application shift_subtitle, a quick application that allows people to shift the times of subtitles in a .srt file.
12
+
13
+ This application was created as part of the RubyLearning Blog competition here http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/}
14
+ s.email = ["oliver.nightingale2@ntlworld.com"]
15
+ s.executables = ["shift_subtitle"]
16
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt"]
17
+ s.files = ["bin/shift_subtitle", "History.txt", "lib/rsrt.rb", "lib/rsrt/srt_parser.rb", "lib/rsrt/subtitle.rb", "lib/rsrt/time_range.rb", "lib/shift_subtitle/cli.rb", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "rsrt.gemspec", "script/console", "script/destroy", "script/generate", "test/test_helper.rb", "test/test_subtitle.rb", "test/test_time_range.rb"]
18
+ s.homepage = %q{http://github.com/olivernn/shift_subtitle}
19
+ s.post_install_message = %q{PostInstall.txt}
20
+ s.rdoc_options = ["--main", "README.rdoc"]
21
+ s.require_paths = ["lib"]
22
+ s.rubyforge_project = %q{rsrt}
23
+ s.rubygems_version = %q{1.3.5}
24
+ s.summary = %q{This gem includes the command line application shift_subtitle, a quick application that allows people to shift the times of subtitles in a .srt file}
25
+ s.test_files = ["test/test_helper.rb", "test/test_subtitle.rb", "test/test_time_range.rb"]
26
+
27
+ if s.respond_to? :specification_version then
28
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
29
+ s.specification_version = 3
30
+
31
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
32
+ s.add_development_dependency(%q<hoe>, [">= 2.3.3"])
33
+ else
34
+ s.add_dependency(%q<hoe>, [">= 2.3.3"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<hoe>, [">= 2.3.3"])
38
+ end
39
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/rsrt.rb'}"
9
+ puts "Loading rsrt gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,5 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/rsrt'
4
+ require "rsrt/subtitle"
5
+ require "rsrt/time_range"
@@ -0,0 +1,48 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestSubtitle < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @subtitle = Subtitle.new(:id =>"100", :time_range => "01:31:51,210 --> 01:31:54,893", :text => "MNU Head Office")
7
+ end
8
+
9
+ def test_time_range_parsing
10
+ assert_equal 1, @subtitle.start_time.hours
11
+ assert_equal 1, @subtitle.end_time.hours
12
+
13
+ assert_equal 31, @subtitle.start_time.minutes
14
+ assert_equal 54, @subtitle.end_time.seconds
15
+ end
16
+
17
+ def test_id
18
+ assert_equal 100, @subtitle.id
19
+ end
20
+
21
+ def test_shift_time_range_forward
22
+ @subtitle.shift_time_range_forward(1000)
23
+ assert_equal 52, @subtitle.start_time.seconds
24
+ assert_equal 55, @subtitle.end_time.seconds
25
+
26
+ assert_equal 1, @subtitle.start_time.hours
27
+ assert_equal 893, @subtitle.end_time.millieseconds
28
+ end
29
+
30
+ def test_shift_time_range_backward
31
+ @subtitle.shift_time_range_backward(1500)
32
+ assert_equal 49, @subtitle.start_time.seconds
33
+ assert_equal 53, @subtitle.end_time.seconds
34
+
35
+ assert_equal 710, @subtitle.start_time.millieseconds
36
+ assert_equal 393, @subtitle.end_time.millieseconds
37
+ end
38
+
39
+ def test_to_s
40
+ assert_equal expected_subtitle_to_s, @subtitle.to_s
41
+ end
42
+
43
+ private
44
+
45
+ def expected_subtitle_to_s
46
+ "100\n01:31:51,210 --> 01:31:54,893\nMNU Head Office"
47
+ end
48
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestTimeRange < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @time_range = TimeRange.new("01:31:51,210")
7
+ end
8
+
9
+ def test_hours_parsing
10
+ assert_equal 1, @time_range.hours
11
+ end
12
+
13
+ def test_minutes_parsing
14
+ assert_equal 31, @time_range.minutes
15
+ end
16
+
17
+ def test_seconds_parsing
18
+ assert_equal 51, @time_range.seconds
19
+ end
20
+
21
+ def test_millieseconds_parsing
22
+ assert_equal 210, @time_range.millieseconds
23
+ end
24
+
25
+ def test_to_ms
26
+ assert_equal 5511210, @time_range.to_ms
27
+ end
28
+
29
+ def test_from_ms
30
+ @time_range.from_ms(5511210)
31
+ assert_equal 1, @time_range.hours
32
+ assert_equal 31, @time_range.minutes
33
+ assert_equal 51, @time_range.seconds
34
+ assert_equal 210, @time_range.millieseconds
35
+ end
36
+
37
+ def test_to_s
38
+ assert_equal "01:31:51,210", @time_range.to_s
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shift_subtitle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Nightingale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-05 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: |-
26
+ This gem includes the command line application shift_subtitle, a quick application that allows people to shift the times of subtitles in a .srt file.
27
+
28
+ This application was created as part of the RubyLearning Blog competition here http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/
29
+ email:
30
+ - oliver.nightingale2@ntlworld.com
31
+ executables:
32
+ - shift_subtitle
33
+ extensions: []
34
+
35
+ extra_rdoc_files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - PostInstall.txt
39
+ files:
40
+ - bin/shift_subtitle
41
+ - History.txt
42
+ - lib/rsrt.rb
43
+ - lib/rsrt/srt_parser.rb
44
+ - lib/rsrt/subtitle.rb
45
+ - lib/rsrt/time_range.rb
46
+ - lib/shift_subtitle/cli.rb
47
+ - Manifest.txt
48
+ - PostInstall.txt
49
+ - README.rdoc
50
+ - Rakefile
51
+ - rsrt.gemspec
52
+ - script/console
53
+ - script/destroy
54
+ - script/generate
55
+ - test/test_helper.rb
56
+ - test/test_subtitle.rb
57
+ - test/test_time_range.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/olivernn/ShiftSubtitle
60
+ licenses: []
61
+
62
+ post_install_message: PostInstall.txt
63
+ rdoc_options:
64
+ - --main
65
+ - README.rdoc
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project: shift_subtitle
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: This gem includes the command line application shift_subtitle, a quick application that allows people to shift the times of subtitles in a .srt file
87
+ test_files:
88
+ - test/test_helper.rb
89
+ - test/test_subtitle.rb
90
+ - test/test_time_range.rb