---
title: "Embed video"
author: "Ian Lyttle"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Embed video}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library("htmltools")
library("fs")
```

```{r}
library("vembedr")
```

Using this package, you can embed videos into RMarkdown documents and Shiny apps for videos from:

- [YouTube](https://developers.google.com/youtube/player_parameters)
- [Vimeo](https://developer.vimeo.com/player/embedding)
- [Box](https://developer.box.com/guides/embed/box-embed/#programmatically)
- [Microsoft Stream](https://docs.microsoft.com/en-us/stream/portal-embed-video)

The links above are associated with the embedding API for each of the services; these are the APIs wrapped by the
`embed()` functions in this package. 
You can also embed the video using only the URL of the page your video is on, with `embed_url()`.

To set the start time, or to change the formatting, check out the `use_*()` functions in `vignette("modify")`.

## Embded using URL

Although a URL can be long and awkward, it will get the job done. 
If the URL is from one of the supported services, it should *just work*:

```{r}
embed_url("https://www.youtube.com/watch?v=BD_n6ju9iRA")
```

If you would like to generate more-concise code using the URL, you can use the `suggest_embed()` function:

```{r}
suggest_embed("https://www.youtube.com/watch?v=BD_n6ju9iRA")
```

## Embed by service

### YouTube

You can get your video's identifier by inspecting its URL at YouTube - you can use the short version, as well:

```{r}
suggest_embed("https://youtu.be/JeaBNAXfHfQ")
```

The identifier is the last part of the URL. 
To embed this video:

```{r}
embed_youtube("JeaBNAXfHfQ")
```

### Vimeo

For Vimeo, the identifier is also included in the standard URL:

```{r}
suggest_embed("https://vimeo.com/238200347")
```

The Vimeo identifier is the path of the URL:

```{r}
embed_vimeo("238200347")
```

### Box

To share a video on Box, the video file itself will have to be shared. From the Box web-interface, you can do this by clicking the **Share** button at the top-right corner of your file's web-page. 
Then create a **Share Link**: 

```{r out.width="100%", echo=FALSE}
knitr::include_graphics("figures/box-binford.png")
```

Then, you need to capture some information about the embed-link itself, either the URL or the `id`. 
Please note that you need to work with the "ugly" URL, not a custom URL: 

```{r}
suggest_embed("https://app.box.com/s/d75g9cr27s2jnx62b86idpgffzzxfdt2")
```

You can do the same thing using the `embed_box()` function and the `id`. 
The Box identifier is the last part of the path, in this case **`d75g9cr27s2jnx62b86idpgffzzxfdt2`**.


```{r}
embed_box("d75g9cr27s2jnx62b86idpgffzzxfdt2")
```

If you are using a corporate instance of Box, and use the `embed_box()` function, you will also have to specify the `custom_domain`. 
For example, if your URL starts with `"https://acme.app.box.com"`, then your `custom_domain` is `"acme"`.

### Microsoft Stream

Microsoft Stream is a service offered for use by (large) organizations so that they may share videos internally to their organization. 
As such, this functionality may have limited appeal.

```{r}
suggest_embed(
  "https://web.microsoftstream.com/video/ae21b0ac-4a2b-41f4-b3fc-f1720dd20f48"
)
```

The identifier is the last part of the path of the URL, in this case **`ae21b0ac-4a2b-41f4-b3fc-f1720dd20f48`**.

```{r}
embed_msstream("ae21b0ac-4a2b-41f4-b3fc-f1720dd20f48") %>%
  use_rounded() %>%
  use_start_time(10)
```

Because this video is internal to an organization, it will likely not play for you.

## Sample videos

In case you need sample videos with which to experiment, this package has you covered:

```{r}
rickroll_vimeo()
```

Also (mercifully, not displayed as it seems no-longer embeddable):

```{r eval=FALSE}
rickroll_youtube()
```

