yt2srt 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/yt2srt +41 -0
  2. data/lib/yt2srt.rb +79 -0
  3. metadata +49 -0
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # yt2srt
4
+ #Copyright (c) 2013, Christiaan Rakowski
5
+ #All rights reserved.
6
+
7
+ #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8
+ # - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9
+ # - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10
+
11
+ #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12
+ #THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
13
+ #IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
14
+ #(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
15
+ #WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
+
17
+
18
+ require 'yt2srt'
19
+
20
+ input = ''
21
+ output = ''
22
+
23
+ if(ARGV.length > 0)
24
+ input = ARGV[0]
25
+ if(ARGV.length > 1)
26
+ output = ARGV[1]
27
+ else
28
+ output = input + '.srt'
29
+ end
30
+ else
31
+ puts 'Specify input file:'
32
+ input = STDIN.gets.chomp
33
+
34
+ puts "Specify output file (leave blank for #{input}.en.srt):"
35
+ output = STDIN.gets.chomp
36
+ if output == ''
37
+ output = input + 'en.srt'
38
+ end
39
+ end
40
+
41
+ Yt2srt.convert(input, output)
@@ -0,0 +1,79 @@
1
+ # yt2srt
2
+ #Copyright (c) 2013, Christiaan Rakowski
3
+ #All rights reserved.
4
+
5
+ #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
+ # - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ # - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10
+ #THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11
+ #IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
12
+ #(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
13
+ #WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
+
15
+ #Main Yt2srt class, contains helper methods to convert Youtube Captions subtitles to SubRip (.srt) format
16
+ class Yt2srt
17
+
18
+ # Helper method, used to convert a single line
19
+ #
20
+ # Example:
21
+ # >> convert_line(index, line)
22
+ #
23
+ # Arguments:
24
+ # index: (Integer)
25
+ # line: (String)
26
+
27
+ def self.convert_line(index, line)
28
+ startTime = line[/(\d+:)?\d+:\d+/]
29
+ if startTime.index(':') == 1
30
+ startTime.insert(0, '0')
31
+ end
32
+ line[/(\d+:)?\d+:\d+/] = ''
33
+ endTime = String.new(startTime)
34
+ sec = endTime[/:\d+/]
35
+ sec.sub!(':', '')
36
+ sec = sec.to_i + 5
37
+ if sec > 59
38
+ sec -= 60
39
+ min = endTime[/\d+:/].chop.to_i + 1
40
+ mins = min.to_s + ':'
41
+ if min < 10
42
+ mins.insert(0, '0')
43
+ end
44
+ endTime[/\d+:/] = mins
45
+ end
46
+ v = ':'
47
+ if sec < 10
48
+ v += '0'
49
+ end
50
+ endTime[/:\d+/] = sec.to_s.insert(0, v)
51
+ text = line
52
+ return "#{index}\n00:#{startTime},000 --> 00:#{endTime},000\n#{text}\n"
53
+ end
54
+
55
+ private_class_method :convert_line
56
+
57
+ # Main method, used to convert Youtube Captions subtitles to SubRip (.srt) format
58
+ #
59
+ # Example:
60
+ # >> Yt2srt.convert(input, output)
61
+ #
62
+ # Arguments:
63
+ # input_file: (String)
64
+ # output_file: (String)
65
+
66
+ def self.convert(input_file, output_file)
67
+ puts "Starting conversion: #{input_file} --> #{output_file}"
68
+ text=File.open(input_file).read
69
+ ofx= File.open(output_file, 'w')
70
+ text.gsub!(/\r\n?/, "\n")
71
+ text.each_line.with_index(1) do |line, index|
72
+ newLine = convert_line(index, line);
73
+ ofx << newLine
74
+ end
75
+ ofx.close
76
+ puts "Conversion completed successfully!"
77
+ end
78
+
79
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yt2srt
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christiaan Rakowski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Quick Ruby script to convert Youtube Captions subtitles to SubRip (.srt)
15
+ format
16
+ email: cs.rakowski@gmail.com
17
+ executables:
18
+ - yt2srt
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/yt2srt.rb
23
+ - bin/yt2srt
24
+ homepage: http://rubygems.org/gems/yt2srt
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.16
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Quick Ruby script to convert Youtube Captions subtitles to SubRip (.srt)
48
+ format
49
+ test_files: []