-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube-snarf-audio
executable file
·78 lines (64 loc) · 1.44 KB
/
youtube-snarf-audio
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby
require 'cgi'
require 'faraday'
require 'rack'
require 'uri'
def main
while youtube_url = ARGV.shift
puts ">>> Snarfing audio from #{youtube_url}..."
if YouTubeSnarfer.snarf(youtube_url)
sleep 60
end
end
end
class YouTubeSnarfer
attr_reader :url, :video_filename
def self.snarf(url)
new(url).snarf
end
def initialize(url)
@url = url
unless @url =~ /^http/
@url = "http://youtube.com/watch?v=#{@url}"
end
end
def snarf
encode_audio
end
def title
video_filename.sub(/\s*-\w+.mp4\s*$/, '')
end
def audio_filename
"#{title}.m4a"
end
def encode_audio
save_video
if File.exists?(audio_filename)
puts ">>> Skipping, #{audio_filename} already exists. Delete it if you want to re-extract the audio."
return false
end
cmd = "ffmpeg -i '#{video_filename}' -vn -acodec copy '#{audio_filename}'"
IO.popen(cmd) do |io|
io.each_line do |line|
puts line
end
end
true
end
def save_video
IO.popen("youtube-dl #{url}") do |io|
io.each_line do |line|
puts line
if line =~ /^\[download\] /
@video_filename =
if line =~ /Destination:/
line.split(': ').last
else
line.sub('[download] ', '').sub(' has already been downloaded', '')
end.sub(/[\s\r\n]*$/, '')
end
end
end
end
end
main if $0 == __FILE__