Class: YouTubeRipper

Inherits:
Object
  • Object
show all
Defined in:
lib/ytripper.rb

Class Method Summary collapse

Class Method Details

.ripObject

Raises:

  • (OptionParser::MissingArgument)


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
# File 'lib/ytripper.rb', line 6

def self.rip
options = {}
OptionParser.new do |parser|
	parser.on("-i", "--input-file FILE", "Specify the input FILE") do |input_file|
		options[:i] = input_file
	end

	parser.on("-o", "--output-directory DIRECTORY", "Specify the output DIRECTORY") do |output_directory|
		options[:o] = output_directory
	end

	parser.on("-f", "--output-format FORMAT", "Specify the output FORMAT") do |format|
		options[:f] = format
	end
end.parse!

  raise OptionParser::MissingArgument if options[:i].nil?
  raise OptionParser::MissingArgument if options[:o].nil?
  raise OptionParser::MissingArgument if options[:f].nil?

  list = File.open(options[:i]).read.split("\n")

  list.each do |item|
    qoptions = {
      "q" => URI.encode_www_form_component(item),
      "part" => "snippet"
    }
    
    videoId = YoutubeSearch.search(qoptions)['items'][0]['id']['videoId']
    url = "https://www.youtube.com/watch?v=#{videoId}"

    format = options[:f]

    case format
    when 'mp3'
      command = "youtube-dl --extract-audio --audio-format mp3 -o '#{options[:o]}/%(title)s.%(ext)s' #{url}"
    when 'mp4'
      command = "youtube-dl -f 137+140 -o '#{options[:o]}/%(title)s.%(ext)s' #{url}"
    end

    if defined?(command)
      system(command)
    end
  end
end