How do I create thumbnails when I upload a video? aws lambda!

benjaminadk on February 23, 2019

Good question. 🤔 Introduction I have been playing around with a YouTube clone I call FooTube. I had set up video uploads to be sent f... [Read Full]
markdown guide
 

Hey ben,

Its nice to see someone refactored my code, especially with the newish layer system, but theres a couple of things you can do that will fit your use case better.

First in my original I use jpg and -ss because I only needed one screenshot and it had to be jpg because of the file system we were using at the time.

You would be better to change to output codec to png - you can just remove the vcodec (theres also another line in there I left in which doesnt do anything for your use case).

For permissions you can move the ffmpeg/ffprobe to /tmp and then run chmod there but you need to update you ffmpeg path.

Solutions to this are to send a POST request from the Lambda function to your backend when the processing is complete.

Actually, in your use case its actually better not to use any of this, it would be better to use something like aws elemental media convert to generate thumbnails/gifs etc.

Though if you wanted to use this function, its better to use SNS to push to your application when its completed. So send to SNS after completing upload - SNS pushes to your application/sends email w/e.

You could also be crazy and use AWS Rekognition to autogenerate tags for the videos.

With your current setup you may run into memory problems seeking 75% on larger files, whats your current largest test files? I assume in your current setup you also use ffprobe to output JSON to get the metadata right?

Its good to see a refactored version of my code for a fake youtube. Never though of using it like that.

 

Regarding the memory problem:

I wanted to implement this for one of my use cases and was worried about the memory issues too.
But this article here explains how the FFmpeg works with URL and seek. wistia.com/learn/marketing/faster-...

And looks like we are good?

 

Thanks for your article, it was helpful and the best out there on this topic. I added a Resources section to give credit where credit's due.

I did some testing with PNG and I was surprised the PNGs were larger. I've been under the general impression that PNG for graphics, JPG for photos.

JPG ~ 12KB

PNG ~ 61KB

Thanks for the tip on SNS, it looks very useful. I would have to do more testing and use some larger files, I think I largest video I tested with so far was only 15MB.

I figured trying to recreate what I could of YouTube would be a good way to learn stuff. So far it has. Obviously, their code is complex, but its fun to come with a function to determine what video to play next for example.

 

Hey Ben,

Thanks for the post. I get 3 jpg files in my bucket's 'cache' folder which are black frames with the following striped across the top.

The image “myapp.s3-us-west-1.amazonaws.com/c... cannot be displayed because it contains errors."

No thumbnail folder is created.

Any thoughts?

Thanks!

Thx!

 

I'm not sure. I guess the first thing i might try is logging the srcKey and dstKey variables since the dst key should be creating the thumbnails folder when an image is uploaded. Put the console.log statements in a then you can check them in the cloud watch reports. I copied my exact code to a gist just i case there was some kind of typo or something.

 

Thanks for the response. I ended up using my web server & ffmpeg to create the thumbs and upload them. You have inspired me to learn more about Lambda, however! Thx

 

Hey Ben

Nice guide - really useful for what i am coding now.

I pretty much copy-pasted the code after fitting variables to my needs.

I have an issue though. When i test the function the reports all well and green lights all the way, and it does generate the images, but the images are 0 byte size and empty.

Do you have any idea what could be wrong?

Cheers

code of conduct - report abuse