Skip to content

Instantly share code, notes, and snippets.

@neaf
Last active November 8, 2015 17:40
Show Gist options
  • Save neaf/946c7f1ee6d6ff8e5bed to your computer and use it in GitHub Desktop.
Save neaf/946c7f1ee6d6ff8e5bed to your computer and use it in GitHub Desktop.
Gets youtube video URL and exports mp3 file while asking for artist and title
~/Dev/ytdl ❯ ruby ytdl.rb https://www.youtube.com/watch\?v\=ftIBZNklEFU R: 2.2.2
[youtube] ftIBZNklEFU: Downloading webpage
[youtube] ftIBZNklEFU: Downloading video info webpage
[youtube] ftIBZNklEFU: Extracting video information
[youtube] ftIBZNklEFU: Downloading DASH manifest
[youtube] ftIBZNklEFU: Downloading DASH manifest
[download] Destination: Acid Interstate V1 - 2015 Remake-ftIBZNklEFU.m4a
[download] 100% of 3.33MiB in 00:01
[ffmpeg] Correcting container in "Acid Interstate V1 - 2015 Remake-ftIBZNklEFU.m4a"
[ffmpeg] Destination: Acid Interstate V1 - 2015 Remake-ftIBZNklEFU.mp3
Deleting original file Acid Interstate V1 - 2015 Remake-ftIBZNklEFU.m4a (pass -k to keep)
File name: Acid Interstate V1 - 2015 Remake-ftIBZNklEFU.mp3
Artist: No idea
Title: Acid Interstate
Enjoy!
~/Dev/ytdl ❯
#!/usr/bin/env ruby
# Requires youtube-dl (https://rg3.github.io/youtube-dl/),
# ffmpeg (https://www.ffmpeg.org/download.html)
# and https://github.com/moumar/ruby-mp3info (gem install ruby-mp3info)
require "mp3info" # imports required library for use
url = ARGV.shift # ARGV is an array of arguments passed to the script
command = "youtube-dl -x --audio-format mp3 --audio-quality 0 #{ url }"
file_name = nil
IO.popen(command) do |io| # runs a command and exposes stream of it's output
while (line = io.gets) do
puts line
if match = line.match(/\[ffmpeg\] Destination: (.+)/) # Checks for line that starts with '[ffmpeg] Destination: ' and extracts the rest of it
file_name ||= match[1] # here we actually get the first something in parentheses from line above
end
end
end
puts
puts "File name: %s" % file_name
Mp3Info.open(file_name) do |mp3| # that's just mp3info usage (https://github.com/moumar/ruby-mp3info)
print "Artist: "
mp3.tag.artist = gets.chomp
print "Title: "
mp3.tag.title = gets.chomp
end
puts
puts "Enjoy!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment