Question

Streaming mp3 and ogg files on internal server and authentication. Trying to stream files to HTML5 player but running into problems with Chrome Seek To function.

Have all my headers setup, but how can I open a Binary File, Seek to position and only send data from that point to the end?

i.e. Given an mp3 file that is 119132474 long,
     And A request comes in asking for the new start point of the file be at 21012274
     How can I send a new binary file with only information from 21012274 to 119132474

Here is something similar to what I want to do but in Node.js http://www.extrawurst.org/blog11/2012/06/streaming-media-in-nodejs/

------- UPDATE 02/15/2014 --------

I installed Redis and used Redis as a temp cache server of Binary data. Then used Redis's GETRANGE. See http://redis.io/commands/getrange

Was it helpful?

Solution

You can open file in binary mode and use the methods from IO module to read bytes. For example:

file_size = File.size('filename')
File.open('filename', 'rb') do |file| # read in binary mode
  file.seek(position)
  file.read(file_size - position) # return all bytes until the end
end

There is another that should work, but I didn't test on streaming. The method is 'binread' which is simpler than the first one:

File.binread('filename', start_pos, offset)

It should work!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top