AUDIO FILE DOWNLOAD IN RUBY

A

andrew mcelroy

[Note: parts of this message were removed to make it a legal post.]

hi all,

Please could some on help me with a code to download an audio file in
ruby.
the code below does not work;;

Are you trying to stream a MP3 or are you trying to download it no
differently than any other binary format?

Andrew McElroy
 
K

kevid

hi, tanks for your response.

I am trying to download it and save it locally. the file is in mp3
format.

Regards
Kevid
 
J

Jonathan Nielsen

[Note: parts of this message were removed to make it a legal post.]

I am trying to download it and save it locally. the file is in mp3
format.

What your current code would do is print the contents of the mp3 file to
the console. Since it's all binary data, this is not very useful.

What you need to do is open a file and write the download contents to it.
One way to do this is:

require 'net/http'
url = "http://stream.esvmedia.org/mp3-play/hw/19023001-19023006.mp3"
res = Net::HTTP.get_response(URI.parse(url))
File.open('output.mp3','w') do |f|
f.write(res.body)
end


For more information on writing to files, see
http://ruby-doc.org/core/classes/File.html

-Jonathan Nielsen
 
K

kevid

hi,

Thanks.

I just tried the code you gave me but I can't play the output file
(output.mp3)

i can't figure out why
 
J

Joel VanderWerf

kevid said:
hi,

Thanks.

I just tried the code you gave me but I can't play the output file
(output.mp3)

i can't figure out why

Make sure you open the file with "wb" instead of "w" to use binary mode?
 
J

Josh Cheek

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top