16

I already know how to embed the youtube video in the R Markdown document. But Suppose I have offline video, that I do not want to publish, in mp4 or avi format. Is there a way to embed it in the document? Something like the following (taking inspiration from image embedding):

![Caption](vid/video1.mp4)
Community
  • 1
  • 1
Harmanjit Singh
  • 163
  • 1
  • 1
  • 5
  • I believe you have to use raw HTML syntax: https://www.w3schools.com/html/html5_video.asp – Yihui Xie May 08 '17 at 06:53
  • @Yihui I tried doing that, It shows the contained in the output file, but the video does not play. Do you think htat could be a problem with the chromium browser (Version 58.0.3029.81) on Ubuntu16.04 – Harmanjit Singh May 08 '17 at 07:09
  • I tried myself and it seems a browser issue. In my mac, I can see the video in Safari, but not in Chrome. – amatsuo_net May 08 '17 at 08:08
  • Thanks @amatsuo_net, I tried opening the html in firefox, it showes the container, but gives error `No video with supported format and MIME type found`. Probably, that is because I am using **.avi** file format. Any recomendation on what is the most compatible file format over browsers and operating systems? – Harmanjit Singh May 08 '17 at 08:37
  • html5 supports only mp4, webM and ogg. https://www.w3schools.com/html/html5_video.asp At least it's necessary to convert the file to one of these formats (although I failed to view a file in mp4 in Chrome). – amatsuo_net May 08 '17 at 10:38

4 Answers4

19

This worked for me:

Slide With Code
========================================================
<video width="320" height="240" controls>
  <source src="movie.mp4" type="video/mp4">
</video>

If you remove the width and height, and enable

autosize: true

R takes care of the size of the video.

Mat D.
  • 453
  • 6
  • 15
  • I have the following code `## AI and Deep Learning ![](hadelin.mp4){width=400 height=300} ` But I get nothing, an outline of the video. I have even left out the `![](hadelin.mp4){width=400 height=300}` and I still can't get it to render in a browser. – Bryan Butler Dec 05 '18 at 00:05
  • The above answer should work. The `video` won't show up on the RStudio Viewer Pane so check using another browser, e.g. Google Chrome. See the `answer` here https://stackoverflow.com/questions/53176203/how-to-embed-video-avi-mpg4-in-xaringan-slides – Daniel James Jul 07 '21 at 21:29
2

Using iframe worked for me

```{r}
video_path  <- "vid/"
video_files <- list.files(video_path,
                         pattern = "\\.mp4$",
                         recursive = TRUE,
                         all.files = FALSE,
                         full.names = TRUE)
```

<iframe width="720" height="480" src="`r video_files[1]`" align="middle" frameborder="0" allowfullscreen></iframe>
Tung
  • 26,371
  • 7
  • 91
  • 115
0

It also works to use an image tag

<img src="movie.mp4" type="video/mp4"/>

-1

See the code

![](https://www.yoursite.com/miniature.png)](https://www.youtube.com/watch?v=xyz)

See the code