Recording audio/ video from R

Exact same (unsolved) question as in Video recording in r from @ber8283

I have tried the following function (which would not record audio anyway), it creates the file, but it is not working, i.e. player won't play it. As additional information, as you add Rvision::readNext(my_stream) the output file does not increase its weight, nor it does when the writer is released.

record_from_camera <- function(
    file_path = lubridate::now() |> 
      stringr::str_remove_all("[^\\d]") |> 
      paste0("_video.mp4"),
    seconds = 5,
    ...,
    fps = 30,
    fourcc = "mpeg"
) {

  file_path <- fs::path_expand(file_path)

  my_stream <- Rvision::stream(index = 0)
  withr::defer(Rvision::release(my_stream))

  my_writer <- Rvision::videoWriter(
    outputFile = file_path,
    fourcc = fourcc,
    fps = fps,
    height = 720, width = 1280
  )
  withr::defer(Rvision::release(my_writer))

  for (i in seq_len(fps * seconds)) {
    my_writer |>
      Rvision::writeFrame(Rvision::readNext(my_stream))
  }

}

I have tried mixing the two packages {Rvision} and {av}, but plots seams not to be captured as a streaming video from {av} (I taught in a similar way as in the example reported in the documentation: capturing: Record Video from Graphics Device in av: Working with Audio and Video in R)

record_from_camera <- function(
    file_path = lubridate::now() |> 
      stringr::str_remove_all("[^\\d]") |> 
      paste0("_video.mp4"),
    seconds = 5,
    fps = 30
) {
  
  streaming <- function(t = fps * seconds) {
    my_stream <- Rvision::stream(index = 0)
    withr::defer(Rvision::release(my_stream))
    
    frames <- vector("list", t)
    for (i in seq_len(t)) {
      frames[[t]] <- Rvision::readNext(my_stream)
    }
    
    for (i in seq_len(t)) plot(frames[[t]])
  }
  
  av::av_capture_graphics(
    streaming(),
    framerate = 30,
    width = 1280,
    height = 720,
    output = file_path,
    verbose = TRUE
  )
}

On the other hand, even if they would be work, non of the above "solutions" would record an audio/video file, but a video only one.

As said by @ber8283, I would "simply" start/stop recording a video file from R whatever way.

Something like:

foo <- 'foo.mp4'
start_recording(foo)
Sys.sleep(5)
stop_recording(foo)

# or
bar <- "bar.mp4"
recording_for(bar, seconds = 5)

Is there anyone who has faced and solved that situation?

Thank you very much,
Corrado.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.