Created
December 23, 2011 00:31
-
-
Save seedifferently/1512496 to your computer and use it in GitHub Desktop.
Read ID3 info over SSH via Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/sftp' | |
require 'mp3info' | |
require 'stringio' | |
io = StringIO.new | |
Net::SFTP.start('host.com', 'username', :password => 'password', :port => 22) do |sftp| | |
f = sftp.file.open("/path/to/file.mp3", "r") | |
begin | |
# Read in 1024byte chunks | |
io << f.read(1024) | |
# Mp3Info.open will throw an error if it hasn't read enough data | |
id3 = Mp3Info.open(io) | |
break | |
rescue | |
# Rescue from the error and repeat the loop | |
next | |
end while io.size < 2097152 # Don't ever read more than 2MB of data | |
f.close() | |
puts id3.tag | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment