Go Gin video screenshot extraction microservice

Nemanja Petrovic
2 min readJul 3, 2020

I had requirement to make microservice which only job (for now) will be to extract screenshot from one or more videos at every second of video.
While other microservices which are part of system are not written in GoLang, I decided to try it for this particular job.

Important info, for first step I needed just proof of concept, not perfect and production ready project, so requirements where:
- It must expose one REST endpoint which will accept one or more video files
- Endpoint must accept request, and return message that videos are being processed, processioning of videos should be done in the background
- It must extract screenshots at every second from every video file and save them in file system
- Microservice must register itself with Eureka service discovery

Tech stack

Next frameworks/libraries where used:
- Gin framework
- FFMPEG
- Wire
- github.com/xuanbo/eureka-client

Implementation

Before you start implementation make sure you have ffmpeg installed on your machine and that you have it in your PATH

First let’s connect our microservice to eureka so it will be visible to other services in the system

Next let’s create gin router and one POST endpoint

Handle videos method looks simple

It just calls video service and passes multipart form which contains videos, service contains logic for extracting videos, saving them on the system and calling util method which extracts screenshot.
First let’s implement service method

This is important part, we call util method in a new go routine, that way every video gets it’s own go routine and and we exit out of the method and return response from endpoint while videos are processing in the background

Now let’s see util method that does the extraction

We just call ffmpeg command and tell it to extract screenshot at every second of the video and save it, I can’t really explain this part in details since command is taken from ffmpeg website.

That’s all
You can check all code on git: https://github.com/dsl94/screenshot-extracting-microservice

--

--

Nemanja Petrovic

Software Engineer, Runner, Tech Enthusiast. Specialized in backend, but can swim in frontend waters too