Guides: How to create custom thumbnails and GIFs with the Mux video api

0:00

Hey guys, Dave here with Mux and in this video we're

0:03

going to talk about how you can generate images

0:07

and animated GIFs, GIFs?

0:11

Uh from your videos on demand just by making a request to

0:15

a URL So let's dive into it.

0:19

I'm here on the asset page in the Mux dashboard.

0:23

I'm going to go to the build tab and try to find out some

0:27

more information about this.

0:29

If you scroll down under this build tab, you will

0:32

see ways that you can get previews of your video listed

0:35

right here on the dashboard.

0:38

I'm going to click on create thumbnails and let's take

0:42

a look at what we see here.

0:44

So it's showing us a URL that we can build using

0:49

the playback ID from this particular video.

0:52

I just put using that URL as the source for your

0:56

image tag in your HTML.

0:58

It will allow you to show the preview of the thumbnail.

1:02

Uh, at the given time that you are providing,

1:05

right, the URL, which is a pretty nifty feature.

1:08

So let's take a look here.

1:10

We have the protocol, we have image.

1:13

Mux.

1:14

com, the playback ID is here, and this ID is the same as

1:20

used up here, this public playback ID, which is used to

1:23

actually play this video back, uh, in your video player.

1:28

Uh, but what we're doing here at the end is just

1:30

tagging on thumbnail dot.

1:34

And, uh, Mux does support some other file formats,

1:38

but we're just going to use a png for now.

1:40

And then we have these query parameters here

1:42

at the end as well.

1:43

And these query parameters you can use to specify the

1:47

dimensions of the thumbnail.

1:49

or the specific time that you are requesting

1:53

that a thumbnail be given to you from that video.

1:56

Um, so let's just use this as is and take a look at

1:59

what that might look like.

2:01

I'm over here in code sandbox and I have a url for our

2:05

still frame from this video and that url I specified the

2:11

Thumbnail as a WebP file, which is a little bit more

2:15

performant and maybe it saves us a little bit on bandwidth

2:19

but I have specified the width, I specified the height,

2:23

and I specified the time.

2:25

I set the time to in seconds to 129 which is two

2:31

minutes and nine seconds into this this video so if

2:36

I were to go back to the dashboard and I can take

2:39

a look at two minutes and nine seconds in this video.

2:42

We should be able to see the exact same thumbnail 209.

2:47

So kind of neat.

2:48

We just generated that on demand without having

2:51

to do any pre prep.

2:52

And if I wanted to take a thumbnail from earlier

2:55

in the video, let's say 19 seconds, I can just

2:58

change the time to 19.

2:59

And it would pull the still frame from that

3:01

time of the video.

3:02

So really cool, quick way to be able to generate

3:05

those stills on demand without having to do

3:08

any kind of pre prep.

3:10

There's, uh, several different parameters that you can

3:12

pass during this generation.

3:15

You can specify the file format to be one of.

3:18

PNG, JPEG, or a WebP file.

3:22

Uh, you can also pass the width, height, rotation,

3:26

if you want to change the way that the image is

3:28

displaying on your web page.

3:31

And then flip it as well, or mirror it.

3:35

Uh, there's also fit mode, which will allow you to

3:39

specify basically how the image is showing up, uh, if

3:43

the image is in a different aspect ratio, but you want it

3:45

to fit a different dimension on, in your application.

3:49

You can specify and choose which sort of mode

3:53

you would like to use to fill out the frame.

3:57

So let's say for example, um, for this image that we

4:01

pulled before, we have sort of this long or wide aspect

4:05

ratio, which might be the look you're looking for.

4:08

But if you had to fill a taller box and you wanted

4:12

to have the bars brought in, you might use a fit mode.

4:17

Let's see, fit mode, pad, maybe, and let's

4:22

see what we get here.

4:23

Yeah.

4:23

So look at that.

4:24

The pillar boxes are brought in and now we're filling

4:27

in any of the extra space specified in our height

4:31

and width aspect ratio with black bars so that there's

4:34

some filler and padding that's happening there to

4:36

maintain your aspect ratio.

4:39

So just another neat way that we can leverage the.

4:43

Query parameters to generate the image on demand based on

4:47

what our application needs.

4:50

Alright, so I'm going to go back here and let's take

4:53

a look at how we can then generate the animated GIF.

4:57

So we have the same thing here, and all we have to

5:00

do is insert the playback ID forward slash animated

5:04

dot gif, or you could say webp there at the end.

5:09

So let's take a look at that over here.

5:11

We have this exact same playback ID here

5:14

in a second image.

5:16

And animated, I'm using the WebP with a width of 428,

5:21

and I'm currently styling this so that it appears

5:24

behind the still frame.

5:26

And in my CSS, when you hover over the still frame,

5:31

the opacity will drop to zero using this transition,

5:34

and we'll be able to see The animated, the animated

5:37

frames occurring right underneath the still.

5:40

So watch the mouse, move the mouse over there and we can

5:44

see that animated preview happening right under there.

5:47

So this is kind of a common effect that you might want to

5:50

be able to show thumbnails.

5:52

That show in preview of what's happening under the thumbnail

5:56

when you on the, uh, when the user hovers over that, um,

6:00

let's talk about how we would show a still frame from a, uh,

6:06

asset that is, uh, Requiring a signed or private playback ID.

6:12

So this particular asset here in the Mugs dashboard

6:15

doesn't actually have a public playback ID.

6:19

It only has a signed playback ID.

6:21

So unless we sign the request to retrieve this

6:25

video or its thumbnails, we won't be able to see it.

6:29

We won't have access to it.

6:31

So let's understand here by going through

6:34

the Mugs dashboard.

6:35

this particular request, how we can actually sign the

6:38

request for a thumbnail so we can preview this video.

6:42

So I'm going to look again under this create

6:46

thumbnail section in the dashboard and notice

6:49

that we have a little bit different of a request here.

6:53

We pass still the playback ID right here in the URL.

6:58

We still get the thumbnail dot png, but we have this

7:01

query parameter for token.

7:03

And the token is a signed JWT that will allow access to view

7:10

the video or the thumbnail as long as it's signed

7:15

with the Mux signing key.

7:17

You can generate a signing key in your Mux account

7:19

by visiting the settings and go to signing keys.

7:24

And we'll use the signing key to authorize access.

7:28

To viewing this thumbnail, so I'm going to go back over to

7:33

code sandbox and take a look at this particular page here

7:39

Where, if we were to make the same request for this image

7:44

without using a signed token.

7:46

Let's just do that really quick.

7:48

We'll create a new image.

7:51

Hit the source as here.

7:54

Alt equals my image.

8:02

Uh, you can probably guess what's going to happen here.

8:04

So, trying to load this PNG right here.

8:09

I'm going to hit reload and it's not actually loading.

8:14

It doesn't work because we don't have signed, uh, access

8:18

to this particular image.

8:20

So what I'm going to do in order to sign access is.

8:25

Use the Mux node SDK here in my Next.

8:28

js app.

8:30

And the Mux node SDK will use some environment variables

8:34

that are in my, set in my env file here in Next.

8:38

js.

8:39

We have a Mux sign in key, which was generated

8:41

in the dashboard.

8:43

And the Mux private key here as well.

8:46

Now, Mux node sdk is smart enough to know if you use

8:49

these exact environment variable names, Mux signing

8:54

key, and this Mux private key here, it will pick up

8:58

and load, it will pick up on them and load them right

9:01

into the Mux node sdk.

9:05

So what we can do now is specify our thumb

9:09

parameters, uh, which are the same query parameters

9:12

that we used originally in the unsigned request.

9:16

So here I can pass time.

9:18

I can pass, uh, so I'm passing five seconds is

9:22

where I want the thumbnail from, and the width is 400.

9:26

And here we're making that signing, uh, creating

9:30

that signed token.

9:32

So here I pass my, uh, signed playback ID.

9:37

And I need to tell it that I'm signing a thumbnail.

9:39

So it knows what to expect when making this request, I

9:44

pass the params that I set up here, right into the library.

9:48

And in return, I will receive this thumb token,

9:51

which is just a string for the signed URL.

9:55

Um, so now I can go to create this image down here.

9:59

I have a source.

10:00

We pass the playback ID here, and then the token goes

10:05

right to the And notice we are not including the query

10:09

parameters here at all.

10:11

We just pass the token and it knows already which query

10:16

parameters were set while you were assigning the token.

10:19

And there it is.

10:20

On the right hand side it shows up as expected.

10:23

Uh, we have a white border around it just to make sure

10:25

that it's looking good.

10:28

that I created here in the CSS.

10:30

And you can also take a look if we were to change

10:33

this to, let's say 15 seconds and just reload, it

10:38

will update automatically to be able to show that

10:41

new frame at 15 seconds.

10:44

That's all we got today to talk about with, uh,

10:47

generating thumbnails and animated GITs on demand.

10:52

Until next time, see you folks.