How do I get local videos to play in RShiny app?

Viewed 178

I'm trying to get videos saved locally to play in RShiny but I only get an empty video player with no video loaded or way to load one. I've seen several posts on this when searched but none seem to reach an answer. My code is:

library(shiny)

ui <- fluidPage(
  tags$video(src = "C:/Downloads/big_buck_bunny_720p_30mb.mp4", type 
  = "video/mp4", autoplay = TRUE, controls = TRUE)
)

server <- function(input, output, session) {
}

shinyApp(ui, server)

This results in a mini player loading in RStudio browser or Edge but no video:

Screenshot

However, if I view source on the Edge page, save that as an .html file and load that file in Edge it works fine. It just doesn't work when run from RStudio as in Edge won't display videos when the address is of the form http://127.0.0.1:6190/. Any ideas? Security settings or something? RStudio plays the same video fine if I instead use a web URL rather than local video file as well.

I've tried loads of iterations of ui.r / server.r, files in www folder, etc. none of which work. Rstudio native browser has same behaviour as Edge (although it can't display the .html file either).

Thanks @ismirsehregal - this gets me a lot further. The video now plays but weirdly looks washed (greyed) out. If I select fullscreen the colour comes back fully. The actual video is https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_30mb.mp4 if that helps.

In RStudio I also get the message:

Listening on http://127.0.0.1:7109 Warning: Error in value[[3L]]: Couldn't normalize path in addResourcePath, with arguments: prefix = 'videos'; directoryPath = 'C:/Downloads' 55: stop 54: value[[3L]] 53: tryCatchOne 52: tryCatchList 51: tryCatch 50: addResourcePath 49: server [#2] Error in value[3L] : Couldn't normalize path in addResourcePath, with arguments: prefix = 'videos'; directoryPath = 'C:/Downloads'

but apart from the colour it works.

1 Answers

You can add static resources to Shiny's web server via addResourcePath.

Please try the following:

library(shiny)

ui <- fluidPage(
  tags$video(src = "videos/big_buck_bunny_720p_30mb.mp4", type = "video/mp4", autoplay = TRUE, controls = TRUE)
)

server <- function(input, output, session) {
  addResourcePath(prefix = "videos", directoryPath = "C:/Downloads")
}

shinyApp(ui, server)

However, using the www subdirectory should also be fine. The prefix for the www folder is "/". Please see this.

Related