0

I am trying to continously read a file in ruby (which is growing over time and needs to be processed in a separate process). Currently I am archiving this with the following bit of code:

r,w = IO.pipe
pid = Process.spawn('ffmpeg'+ffmpeg_args, {STDIN => r, STDERR => STDOUT})
Process.detach pid
while true do
  IO.copy_stream(open(@options['filename']), w)
  sleep 1
end

However - while working - I can't imagine that this is the most performant way of doing it. An alternative would be to use the following variation:

step = 1024*4
copied = 0
pid = Process.spawn('ffmpeg'+ffmpeg_args, {STDIN => r, STDERR => STDOUT})
Process.detach pid
while true do
  IO.copy_stream(open(@options['filename']), w, step, copied)
  copied += step
  sleep 1
end

which would only continously copy parts of the file (the issue here being if the step should ever overreach the end of the file). Other approaches such a simple read-file led to ffmpeg failing when there was no new data. With this solution the frames are dropped if no new data is available (which is what I need).

Is there a better way (more performant) to archive something like that?

EDIT:

Using the method proposed by @RaVeN I am now using the following code:

open(@options['filename'], 'rb') do |stream|
  stream.seek(0, IO::SEEK_END)
  queue = INotify::Notifier.new
  queue.watch(@options['filename'], :modify) do
    w.write stream.read
  end
  queue.run
end

However now ffmpeg complaints about invalid data. Is there another method than read?

stiller_leser
  • 1,512
  • 1
  • 16
  • 39
  • 1
    I think what you're looking for is this? http://stackoverflow.com/questions/1293695/watch-read-a-growing-log-file or this: http://rubyforadmins.com/reading-growing-files – siegy22 Jul 19 '16 at 08:59
  • Thanks for the quick reply. I edited my question with the code I am using now, which unfortuantely causes errors in ffmpeg. – stiller_leser Jul 19 '16 at 09:22

0 Answers0