gaqzi-youtube-downloader 0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/youtube-downloader +112 -0
- metadata +53 -0
data/youtube-downloader
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
#!/usr/bin/env ruby -KU
|
2
|
+
$KCODE = 'u'
|
3
|
+
require 'open-uri'
|
4
|
+
require 'ftools'
|
5
|
+
|
6
|
+
# Save the downloads in these dirs, if you just want it to get saved in the
|
7
|
+
# directory the script is run from; set to '.'
|
8
|
+
$DOWNLOAD_DIR = "#{ENV['HOME']}/Movies"
|
9
|
+
$MUSIC_DIR = "#{ENV['HOME']}/Music"
|
10
|
+
$TEMP_DIR = '/tmp'
|
11
|
+
$FILE_EXTENSION = 'flv' # Extension of the movies
|
12
|
+
$TOUCH = true # I want my newly downloaded to be timestamped now
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
if ARGV.empty? or ARGV[0].match(/(-h)|((--)?help)/i)
|
17
|
+
puts "Usage: #{$0.split(/\//)[-1]} <youtube url> [command]"
|
18
|
+
puts "Where command is one of the following:"
|
19
|
+
puts "\t audio - download movie, rip audio, remove movie"
|
20
|
+
puts "\t all / rip - download movie and rip audio"
|
21
|
+
puts "\t no command given will download the movie"
|
22
|
+
puts ''
|
23
|
+
exit 0
|
24
|
+
elsif not ARGV[0].match(/youtube/)
|
25
|
+
puts "No valid youtube url supplied"
|
26
|
+
exit 1
|
27
|
+
end
|
28
|
+
|
29
|
+
Video = Struct.new(:title, :video_id, :id) do
|
30
|
+
def to_s
|
31
|
+
"http://youtube.com/get_video.php?t=#{id}&video_id=#{video_id}"
|
32
|
+
end
|
33
|
+
|
34
|
+
def empty?
|
35
|
+
@title.nil? and @video_id.nil? and @id.nil?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
the_vid = Video.new
|
40
|
+
|
41
|
+
begin
|
42
|
+
open(ARGV[0]) do |f|
|
43
|
+
f.each do |l|
|
44
|
+
if l.match(/(var swfArgs)/)
|
45
|
+
the_vid.id = l.match(/"t":\s+"([^"]+)/)[1]
|
46
|
+
the_vid.video_id = l.match(/"video_id":\s+"([^"]+)/)[1]
|
47
|
+
elsif l.match(/<title>/)
|
48
|
+
the_vid.title = l.match(/<title>YouTube - (.*)<\/title>/)[1]
|
49
|
+
end
|
50
|
+
|
51
|
+
break if not the_vid.empty?
|
52
|
+
end
|
53
|
+
end
|
54
|
+
rescue Timeout::Error
|
55
|
+
$stderr.puts "Timeout while connecting to: #{ARGV[0]}"
|
56
|
+
$stderr.puts 'Retrying...'
|
57
|
+
retry
|
58
|
+
rescue Exception => e
|
59
|
+
$stderr.puts e
|
60
|
+
$stderr.puts e.backtrace.join("\n")
|
61
|
+
exit 1
|
62
|
+
end
|
63
|
+
|
64
|
+
print "Filename: [#{the_vid.title}]"
|
65
|
+
name = STDIN.gets.strip
|
66
|
+
name = if name.empty?
|
67
|
+
the_vid.title
|
68
|
+
else
|
69
|
+
name
|
70
|
+
end.gsub(/(['" ])/, '\\\\\1')
|
71
|
+
the_file = "#{$DOWNLOAD_DIR}/#{name}.#{$FILE_EXTENSION}"
|
72
|
+
|
73
|
+
download_command = case ENV['OSTYPE']
|
74
|
+
when 'darwin' # Mac OS X
|
75
|
+
"/usr/bin/env curl -L -C - -o #{the_file} '#{the_vid}'"
|
76
|
+
when /BSD/i
|
77
|
+
"/usr/bin/env fetch -o '#{the_file}' '#{the_vid}'"
|
78
|
+
else # Linux and whatever, most people got wget installed!
|
79
|
+
"/usr/bin/env wget '#{the_vid}' -c -O '#{the_file}'"
|
80
|
+
end
|
81
|
+
|
82
|
+
def rip_audio(file, name)
|
83
|
+
`/usr/bin/env mkfifo #{$TEMP_DIR}/#{name}`
|
84
|
+
IO.popen("/usr/bin/env lame -b 160 #{$TEMP_DIR}/#{name} #{$MUSIC_DIR}/#{name}.mp3")
|
85
|
+
`/usr/bin/env mplayer #{file} -ao pcm:fast:file=#{$TEMP_DIR}/#{name} -vc null -vo null`
|
86
|
+
`/usr/bin/env rm #{$TEMP_DIR}/#{name}`
|
87
|
+
end
|
88
|
+
|
89
|
+
def remove(name)
|
90
|
+
`/usr/bin/env rm #{$DOWNLOAD_DIR}/#{name}.#{$FILE_EXTENSION}`
|
91
|
+
end
|
92
|
+
|
93
|
+
case ARGV[1]
|
94
|
+
when /audio/i # Just audio
|
95
|
+
if File.exist? the_file.to_s
|
96
|
+
`#{download_command}`
|
97
|
+
else
|
98
|
+
`/usr/bin/env mkfifo '#{the_file}'`
|
99
|
+
IO.popen(download_command)
|
100
|
+
end
|
101
|
+
|
102
|
+
rip_audio(the_file, name)
|
103
|
+
remove(name)
|
104
|
+
when /(all)|(rip)/i # Both audio and video
|
105
|
+
`#{download_command}`
|
106
|
+
rip_audio(the_file, name)
|
107
|
+
|
108
|
+
`/usr/bin/env touch #{$DOWNLOAD_DIR}/#{name}.#{$FILE_EXTENSION}` if $TOUCH
|
109
|
+
else
|
110
|
+
`#{download_command}`
|
111
|
+
`/usr/bin/env touch #{$DOWNLOAD_DIR}/#{name}.#{$FILE_EXTENSION}` if $TOUCH
|
112
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gaqzi-youtube-downloader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.5"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- "Bj\xC3\xB6rn Andersson"
|
8
|
+
autorequire:
|
9
|
+
bindir: .
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-09-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Downloads youtube videos to your local harddrive and optionally rips the audio from the video.
|
17
|
+
email: ba@sanitarium.se
|
18
|
+
executables:
|
19
|
+
- youtube-downloader
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- youtube-downloader
|
26
|
+
has_rdoc: false
|
27
|
+
homepage: http://github.com/ba/youtube-downloader
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: "0"
|
38
|
+
version:
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: "0"
|
44
|
+
version:
|
45
|
+
requirements: []
|
46
|
+
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.2.0
|
49
|
+
signing_key:
|
50
|
+
specification_version: 2
|
51
|
+
summary: Downloads youtube videos and optionally rips audio
|
52
|
+
test_files: []
|
53
|
+
|