subrip 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ tags
19
+ data/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in subrip.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Prasanna N
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Subrip
2
+
3
+ Command-line utility to shift subtitles.
4
+
5
+ My solution to RubyLearning.com's challenge # 1 https://github.com/npras/subrip
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'subrip'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install subrip
20
+
21
+ ## Usage
22
+
23
+ In the command-line,
24
+
25
+ `shift_subtitle -o add -t 2,500 input/batman_begins.srt output/batman_begins.srt`
26
+
27
+ where:
28
+
29
+ -o => Operation switch. Accepts either 'add' or 'sub' (without the quotes).
30
+
31
+ -t => Time to shift. It's of the form SS,MMM. Example: `-t 02,500` meaning
32
+ 2 seconds and 500 milliseconds.
33
+
34
+ The last two parameters are the input subtitle file and the output subtitle
35
+ file respectively.
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require "bundler/gem_tasks"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'spec/*_spec.rb'
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
4
+
5
+ require 'subrip'
6
+
7
+ options = Subrip::Command.parse ARGV
8
+ if Subrip::Command.validate
9
+ subrip = Subrip::Shift.new(options)
10
+ subrip.shift_subtitle
11
+ else # good habit to not leave the else part
12
+ exit
13
+ end
14
+
15
+ # Example Usage
16
+ #
17
+ # bin/shift_subtitle -o add -t 11,222 data/batman_begins.txt data/batman_begins_shift.txt
data/lib/command.rb ADDED
@@ -0,0 +1,63 @@
1
+ require 'optparse'
2
+ require 'optparse/time'
3
+ require 'ostruct'
4
+
5
+ module Subrip
6
+ class Command
7
+ #
8
+ # Return a structure describing the options.
9
+ #
10
+ def self.parse(args)
11
+ # The options specified on the command line will be collected in *options*.
12
+ # We set default values here.
13
+ @options = OpenStruct.new
14
+
15
+ opts = OptionParser.new do |opts|
16
+ opts.banner = "Usage: example.rb [options] input_file output_file"
17
+
18
+ opts.separator ""
19
+
20
+ opts.on("-o", "--operation OPERATION",
21
+ [:add, :sub],
22
+ "Operation can either be 'add' or 'sub'") do |oper|
23
+ @options.operation = oper
24
+ end
25
+
26
+ opts.on("-t", "--time TIME_TO_SHIFT",
27
+ "Time should be of format 11,222 where '11' is the amount of seconds and '222' the amount of milliseconds") do |time|
28
+ @options.time_to_shift = time
29
+ end
30
+
31
+ opts.separator ""
32
+ opts.separator "Common options:"
33
+
34
+ # No argument, shows at tail. This will print an options summary.
35
+ # Try it and see!
36
+ opts.on_tail("-h", "--help", "Show this message") do
37
+ puts opts
38
+ exit
39
+ end
40
+ end # OptionParser.new do
41
+
42
+ begin
43
+ @options.ip_file = args.fetch(4)
44
+ @options.op_file = args.fetch(5)
45
+ rescue IndexError
46
+ raise OptionParser::MissingArgument, "See --help."
47
+ exit
48
+ end
49
+
50
+ opts.parse!(args)
51
+ @options
52
+ end # parse()
53
+
54
+ def self.validate
55
+ if @options.ip_file.nil? or @options.op_file.nil? or @options.time_to_shift.nil? or @options.operation.nil?
56
+ raise OptionParser::MissingArgument, "See --help."
57
+ else
58
+ true
59
+ end
60
+ end
61
+
62
+ end # class Command
63
+ end # module Subrip
data/lib/shift.rb ADDED
@@ -0,0 +1,40 @@
1
+ module Subrip
2
+ class Shift
3
+ #attr_reader :ip_file, :op_file, :time_to_shift, :operation
4
+
5
+ def initialize(opts)
6
+ @ip_file, @op_file = opts.ip_file, opts.op_file
7
+ sec, milli_sec = *opts.time_to_shift.split(',').map(&:to_i)
8
+ @time_to_shift = get_time_in_sec(sec, milli_sec)
9
+ @operation = (opts.operation == :add) ? :+ : :-
10
+ end
11
+
12
+ def shift_subtitle
13
+ op_lines = File.readlines(@ip_file).map do |line|
14
+ if line.match /(.*) --> (.*)/
15
+ "#{transform_time($1)} --> #{transform_time($2)}\n"
16
+ else
17
+ line
18
+ end
19
+ end
20
+ File.open(@op_file, 'w') do |f|
21
+ f << op_lines.join
22
+ end
23
+ end
24
+
25
+ def transform_time(time, operation=@operation, time_to_shift=@time_to_shift)
26
+ # eg: "01:32:04,283"
27
+ time.match /(\d+):(\d+):(\d+),(\d+)/
28
+ hour, min = $1.to_i, $2.to_i
29
+ sec = get_time_in_sec($3.to_i, $4.to_i)
30
+ from_time = Time.new(Time.now.year, nil, nil, hour, min, sec)
31
+ to_time = from_time.send(operation, time_to_shift)
32
+ to_time.strftime "%H:%M:%S,%3N"
33
+ end
34
+
35
+ def get_time_in_sec(sec, milli_sec)
36
+ Time.at(sec, milli_sec * 1_000).to_f
37
+ end
38
+
39
+ end # class Shift
40
+ end # module Subrip
data/lib/subrip.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "subrip/version"
2
+ require "shift"
3
+ require "command"
@@ -0,0 +1,3 @@
1
+ module Subrip
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ require_relative './spec_helper'
2
+ require_relative '../lib/shift'
3
+
4
+ describe Subrip::Shift do
5
+ before do
6
+ options = OpenStruct.new
7
+ options.ip_file = 'data/batman_begins.txt'
8
+ options.op_file = 'data/batman_begins_shift.txt'
9
+ options.time_to_shift = '2,500'
10
+ options.operation = :add
11
+ @it = Subrip::Shift.new(options)
12
+ end
13
+
14
+ describe "#transform_time" do
15
+ it "adds time_to_shift to the given time" do
16
+ operation = :+
17
+ time_to_shift = Time.at(2, 500000).to_f
18
+
19
+ time_str = "01:32:04,283"
20
+ shifted_time_str = "01:32:06,783"
21
+ @it.transform_time(time_str, operation, time_to_shift).must_equal shifted_time_str
22
+
23
+ time_str = "01:32:07,769"
24
+ shifted_time_str = "01:32:10,269"
25
+ @it.transform_time(time_str, operation, time_to_shift).must_equal shifted_time_str
26
+ end
27
+
28
+ it "subtracts time_to_shift from the given time" do
29
+ operation = :-
30
+ time_to_shift = Time.at(2, 500000).to_f
31
+
32
+ time_str = "01:32:04,283"
33
+ shifted_time_str = "01:32:01,783"
34
+ @it.transform_time(time_str, operation, time_to_shift).must_equal shifted_time_str
35
+
36
+ time_str = "01:32:07,769"
37
+ shifted_time_str = "01:32:05,269"
38
+ @it.transform_time(time_str, operation, time_to_shift).must_equal shifted_time_str
39
+ end
40
+ end
41
+
42
+ end
@@ -0,0 +1,9 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'rr'
4
+ require 'pry'
5
+
6
+ class MiniTest::Unit::TestCase
7
+ include RR::Adapters::TestUnit
8
+ end
9
+
data/subrip.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/subrip/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Prasanna N"]
6
+ gem.email = ["prasann87@gmail.com"]
7
+ gem.description = %q{Command-line utility to shift subtitles}
8
+ gem.summary = %q{My solution to RubyLearning.com's challenge#1}
9
+ gem.homepage = "https://github.com/npras/subrip"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "subrip"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Subrip::VERSION
17
+ gem.executables << 'shift_subtitle'
18
+ gem.post_install_message = %q|
19
+ *****************************************************************************
20
+
21
+ A Command-line utility to shift subrip subtitles.
22
+
23
+ My solution to RubyLearning.com's challenge#1
24
+ (http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/)
25
+
26
+ Usage:
27
+ shift_subtitle -o add -t 2,500 input/batman_begins.srt output/batman_begins.srt
28
+
29
+ *****************************************************************************
30
+ |
31
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subrip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Prasanna N
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Command-line utility to shift subtitles
15
+ email:
16
+ - prasann87@gmail.com
17
+ executables:
18
+ - shift_subtitle
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - bin/shift_subtitle
28
+ - lib/command.rb
29
+ - lib/shift.rb
30
+ - lib/subrip.rb
31
+ - lib/subrip/version.rb
32
+ - spec/shift_spec.rb
33
+ - spec/spec_helper.rb
34
+ - subrip.gemspec
35
+ homepage: https://github.com/npras/subrip
36
+ licenses: []
37
+ post_install_message: ! "\n *****************************************************************************\n\n
38
+ \ A Command-line utility to shift subrip subtitles.\n\n My solution to RubyLearning.com's
39
+ challenge#1 \n (http://rubylearning.com/blog/2009/09/24/rpcfn-shift-subtitle-1/)\n\n
40
+ \ Usage:\n shift_subtitle -o add -t 2,500 input/batman_begins.srt output/batman_begins.srt\n\n
41
+ \ *****************************************************************************\n
42
+ \ "
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.24
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: My solution to RubyLearning.com's challenge#1
64
+ test_files:
65
+ - spec/shift_spec.rb
66
+ - spec/spec_helper.rb
67
+ has_rdoc: