wch / webshot

Take screenshots of web pages from R
http://wch.github.io/webshot/
227 stars 40 forks source link

Recording a timeframe not a single shot #80

Open muschellij2 opened 5 years ago

muschellij2 commented 5 years ago

I've been playing around with https://github.com/romainfrancois/rsciinema @romainfrancois and we've been able to get an asciicast to an GIF with the help of asciicast2gif, see (https://github.com/romainfrancois/rsciinema/issues/11 for an example).

As asciicast2gif relies mainly on PhatomJS (https://github.com/asciinema/asciicast2gif/blob/master/src/asciinema/gif/main.cljs#L156) (but to be fair a lot of other JS/node), I was wondering if it'd be possible to use rmdshot but have it take multiple shots over time or a range of time.

Example below

Here we create an asciicast which can has an RMarkdown/Shiny output object that will render when put in an Rmd. The output will

library(rsciinema)
input = paste0("library(magrittr); # a comment is a gra", backspace(), "eat thing to do", 
               "\niris %>% \n  dplyr::group_by(Species) %>%\n  ", 
               "dplyr::summarise_all(mean) \n # a new line")
data = asciicast( input, speed = 0.2)
  out = asciinema(data = data, autoplay = TRUE, loop = FALSE)
  tfile = tempfile()
  dput(out, file = tfile)
  dout = readLines(tfile)

  x = c("---", "output: rsciinema::asciinema_document", "---", "",
        "```{r, echo = FALSE}", 
        "library(rsciinema)",
        dout,
        "```"
        )
  tfile = tempfile(fileext = ".Rmd")
  writeLines(x, con = tfile, sep ="\n")

Setting multiple files for rmdshot/webshot did not achieve the results we're looking for. Just wanted to know if any insight possible or if a better approach would be to hack the asciicast2gif JS to it's core, then use the PhantomJS install from webshot.

wch commented 5 years ago

I have some rough code for recording frames of a web page using chromote:

library(chromote)
b <- ChromoteSession$new(width = 480, height = 360)
frames <- list()
cancel_save_screencast_frames <- b$Page$screencastFrame(callback_ = function(value) {
  frames[[length(frames) + 1]] <<- value
  cat(".")
})
b$Page$navigate("http://giphygifs.s3.amazonaws.com/media/73FzKOYDpp7VK/giphy.mp4")
b$Page$startScreencast(format = "png", everyNthFrame = 2)

# Stop after 6 seconds
later(
  function() {
    b$Page$stopScreencast()
    cancel_save_screencast_frames()

    lapply(seq_along(frames), function(i) {
      writeBin(jsonlite::base64_dec(frames[[i]]$data), sprintf("frame%02d.png", i))  
    })
    b$close()
    message("done")
  },
  6
)