coderrr-video-accel 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/speed_stream.rb +19 -10
- data/lib/vdl.rb +2 -2
- data/lib/video_player.rb +4 -0
- data/lib/youtube_speed_stream.rb +45 -10
- data/video-accel.gemspec +1 -1
- metadata +2 -2
data/lib/speed_stream.rb
CHANGED
@@ -18,8 +18,8 @@ class SpeedStream
|
|
18
18
|
@output_file = output_file
|
19
19
|
@write_mutex = Mutex.new
|
20
20
|
@finished = []
|
21
|
-
@bytes_per_conn =
|
22
|
-
@concurrent_connections =
|
21
|
+
@bytes_per_conn = 200_000
|
22
|
+
@concurrent_connections = 15
|
23
23
|
@cookies = {}
|
24
24
|
@extra_file_length_headers = {'Range' => 'bytes=1-'}
|
25
25
|
end
|
@@ -34,8 +34,8 @@ class SpeedStream
|
|
34
34
|
|
35
35
|
begin
|
36
36
|
ranges.each do |range|
|
37
|
-
pool.add_work(range) do |
|
38
|
-
download_range
|
37
|
+
pool.add_work(range) do |drange|
|
38
|
+
download_range drange
|
39
39
|
end
|
40
40
|
end
|
41
41
|
ensure
|
@@ -48,11 +48,18 @@ class SpeedStream
|
|
48
48
|
private
|
49
49
|
|
50
50
|
def download_range(range)
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
data = nil
|
52
|
+
Timeout.timeout(3*60) do
|
53
|
+
data = get_with_redirects(range)
|
54
|
+
end
|
55
|
+
rescue Timeout::Error
|
56
|
+
puts "Timed out, retrying"
|
57
|
+
retry
|
58
|
+
rescue Exception
|
59
|
+
puts $!, $@
|
60
|
+
retry
|
61
|
+
else
|
62
|
+
write_file(range.begin, data)
|
56
63
|
process_progress(range)
|
57
64
|
end
|
58
65
|
|
@@ -85,8 +92,8 @@ class SpeedStream
|
|
85
92
|
end
|
86
93
|
|
87
94
|
def write_file(pos, buf)
|
88
|
-
@bytes_written += buf.size
|
89
95
|
@write_mutex.synchronize do
|
96
|
+
@bytes_written += buf.size
|
90
97
|
file_handle.seek(pos, IO::SEEK_SET)
|
91
98
|
file_handle.write buf
|
92
99
|
file_handle.flush
|
@@ -100,6 +107,8 @@ class SpeedStream
|
|
100
107
|
rs << (n*bytes_per_conn..(n+1)*bytes_per_conn)
|
101
108
|
end
|
102
109
|
rs[-1] = (rs[-1].begin..file_length)
|
110
|
+
|
111
|
+
puts file_length
|
103
112
|
rs
|
104
113
|
end
|
105
114
|
end
|
data/lib/vdl.rb
CHANGED
@@ -46,10 +46,10 @@ stream.on_progress = lambda do |bytes, bps, percent|
|
|
46
46
|
puts "downloading #{url} to #{file}..."
|
47
47
|
puts "#{'%.2f'%bps} b/s #{'%.2f%'%percent}"
|
48
48
|
|
49
|
-
if !started and bytes >
|
49
|
+
if !started and bytes > 200_000
|
50
50
|
started = true
|
51
51
|
system(%{#{terminal} "#{player} #{file}" &})
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
stream.download!
|
55
|
+
stream.download!
|
data/lib/video_player.rb
CHANGED
@@ -21,6 +21,9 @@ class VideoPlayer
|
|
21
21
|
puts "process #{cmd}"
|
22
22
|
case cmd
|
23
23
|
when 'q' then exit!
|
24
|
+
when 'r'
|
25
|
+
@pid.kill! if @pid
|
26
|
+
@pid = spawn(@last_spawn)
|
24
27
|
when 'm'
|
25
28
|
@pid.kill! if @pid
|
26
29
|
@pid = spawn("mplayer -msglevel all=0 #{MP_ARGS[@mplayer_arg]} #{@file}")
|
@@ -32,6 +35,7 @@ class VideoPlayer
|
|
32
35
|
end
|
33
36
|
|
34
37
|
def spawn(cmd)
|
38
|
+
@last_spawn = cmd
|
35
39
|
pid = fork do
|
36
40
|
exec(cmd)
|
37
41
|
end
|
data/lib/youtube_speed_stream.rb
CHANGED
@@ -1,25 +1,48 @@
|
|
1
1
|
require 'speed_stream'
|
2
2
|
|
3
3
|
class YoutubeSpeedStream < SpeedStream
|
4
|
+
class YoutubeRange < Range
|
5
|
+
attr_accessor :file_offset
|
6
|
+
attr_writer :begin
|
7
|
+
alias_method :orig_begin, :begin
|
8
|
+
def begin
|
9
|
+
@begin or orig_begin
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
4
13
|
def initialize(*a)
|
5
14
|
super
|
6
|
-
@concurrent_connections =
|
15
|
+
@concurrent_connections = 4
|
7
16
|
@extra_file_length_headers = {}
|
17
|
+
@chunk_beginnings = {}
|
8
18
|
end
|
9
19
|
|
10
20
|
private
|
11
21
|
|
22
|
+
def ranges
|
23
|
+
@ranges ||= begin
|
24
|
+
len = 2*60 + 13
|
25
|
+
ranges = []
|
26
|
+
i = 0
|
27
|
+
(0..len).step(2) do |start|
|
28
|
+
ranges << YoutubeRange.new(start, i)
|
29
|
+
i += 1
|
30
|
+
end
|
31
|
+
ranges.first.file_offset = 0
|
32
|
+
ranges
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
12
36
|
def get_with_redirects(range)
|
37
|
+
start = range.begin
|
38
|
+
index = range.end
|
13
39
|
current_uri = uri
|
14
40
|
|
15
41
|
loop do
|
16
42
|
TCPSocket.open(current_uri.host, current_uri.port) do |s|
|
17
43
|
uri_str = current_uri.request_uri
|
18
|
-
|
19
|
-
|
20
|
-
else
|
21
|
-
uri_str << "&start=#{range.begin}"
|
22
|
-
end
|
44
|
+
uri_str.sub!(/&begin=\d+|$/, "&begin=#{range.begin*1000}")
|
45
|
+
|
23
46
|
s.write("GET #{uri_str} HTTP/1.1\r\nHost: #{current_uri.host}\r\nCookie: #{cookie_string}\r\n\r\n")
|
24
47
|
|
25
48
|
s.while_reading(data = "") { break if data =~ /\r?\n\r?\n/ }
|
@@ -32,14 +55,26 @@ class YoutubeSpeedStream < SpeedStream
|
|
32
55
|
# strip off header and keep reading
|
33
56
|
data.gsub!(/\A.+?\r?\n\r?\n/m,'')
|
34
57
|
|
35
|
-
while buf = (s.readpartial(
|
58
|
+
while buf = (s.readpartial(1024) rescue nil)
|
36
59
|
data << buf
|
37
|
-
|
60
|
+
if ! @chunk_beginnings[index] and data.size > 1024
|
61
|
+
@chunk_beginnings[index] = data[0, 1024]
|
62
|
+
end
|
63
|
+
break if bgn = @chunk_beginnings[index+1] and end_index = data.index(bgn[327..-1])
|
38
64
|
end
|
65
|
+
sleep 1 until range.file_offset
|
66
|
+
|
67
|
+
@bytes_per_conn ||= data.size
|
68
|
+
data = data[0...end_index] if end_index
|
39
69
|
|
40
70
|
# strip FLV headers on all parts except first
|
41
|
-
|
42
|
-
|
71
|
+
data = data[327..-1] || '' unless index == 0
|
72
|
+
|
73
|
+
ranges[index+1].file_offset = range.file_offset + data.size if ranges[index+1]
|
74
|
+
|
75
|
+
range.begin = range.file_offset
|
76
|
+
|
77
|
+
return data
|
43
78
|
end
|
44
79
|
end
|
45
80
|
end
|
data/video-accel.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coderrr-video-accel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- coderrr
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-02-13 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|