Tutorials

Generating Thumbnails from Videos using ApyHub’s API: A step-by-step guide in Go

The tutorial introduces the ApyHub video thumbnail generator API, providing a step-by-step guide on how to utilize the API for requesting video thumbnails using Go.
Generating Thumbnails from Videos using ApyHub’s API: A step-by-step guide in Go
SO
Sohail Pathan
Last updated on February 06, 2024

Introduction:

As we have discussed in the previous article, video thumbnails have a lot of benefits and possible uses, including better visual appeal, improved user experience, and of course a boost in brand identity and recognition. In this tutorial, we will get a bit more technical - focusing on the ApyHub video thumbnail generator API.
No worries, this will try to be as simple and detailed as possible. We will go step by step, guiding you through every little detail. We will cover the API's core functionalities, including how to request thumbnails from videos using simple HTTP requests.
Moreover, we will show something even more cool: How to custamize thumbnail dimensions and extract thumbnails from various time points within a video. Finally, we will show how to implement the generated thumbnails into your applications or websites.

Prerequisite:

Step 1: Set up the project

First things first — We’ll start by creating a new directory and initialize it with Go modules.
1
> mkdir video-thumbnail-api-golang
2
> cd video-thumbnail-api-golang
3
> go mod init video-thumbnail-generator
Note: go mod init video-thumbnail-generator initializes a new Go module by creating a go.mod file in the current directory which tracks the project's dependencies.

Step 2: Create a new file main.go, and open it in your IDE or code editor:

This file will contain the logic to interact with ApyHub’s Video Thumbnail Generator API.
ide

Step 3: Import the necessary packages:

In this step, the necessary packages are imported to make a successful API call.
1
package main
2
3
import (
4
"bytes"
5
"encoding/json"
6
"fmt"
7
"net/http"
8
"io/ioutil"
9
)
  • bytes: Required for buffering and manipulating byte slices.
  • encoding/JSON: Required for encoding and decoding JSON, the format used for the API request and response.
  • fmt: Provides I/O formatting functions.
  • net/http: Required to make HTTP requests to the API.
  • io/ioutil: Helps in reading the response body

Step 4: Define the payload:

This code defines a struct ThumbnailRequest representing the data you will send to the API and tells Go how to encode the data into JSON.
1
type ThumbnailRequest struct {
2
VideoURL string `json:"video_url"`
3
StartTime string `json:"start_time"`
4
Duration string `json:"duration"`
5
Size string `json:"size"`
6
}

Step 5: Creating the HTTP Request:

We are creating a generateThumbnail function, which will encapsulate the logic to generate Thumbnail using ApyHub’s Generate Video Thumbnail API.
1
func generateThumbnail(apiToken, videoURL string) {
2
apiUrl:= "https://api.apyhub.com/generate/video-thumbnail/url?output=test-sample.mp4"
3
payload := ThumbnailRequest{
4
VideoURL: videoURL,
5
StartTime: "0",
6
Duration: "2",
7
Size: "400x300",
8
}
9
jsonData, err := json.Marshal(payload)
10
11
if err != nil {
12
fmt.Println("Error marshalling data:", err)
13
return
14
}
15
16
req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonData))
17
if err != nil {
18
fmt.Println("Error creating request:", err)
19
return
20
}
21
req.Header.Set("Content-Type", "application/json")
22
req.Header.Set("apy-token", apiToken)
23
client := &http.Client{}
24
resp, err := client.Do(req)
25
if err != nil {
26
fmt.Println("Error sending request to the API:", err)
27
return
28
}
29
defer resp.Body.Close()
30
body, err := ioutil.ReadAll(resp.Body)
31
if err != nil {
32
fmt.Println("Error reading response body:", err)
33
return
34
}
35
fmt.Println("Response:", string(body))
36
}

Step 6: Create the main Function:

This function initialises variables for our apiToken and the videoURL, then calls the generateThumbnail function with these parameters to generate a thumbnail for the specified video.
1
func main() {
2
apiToken := "YOUR_APY_TOKEN" // Replace with your APY token
3
videoURL := "https://assets.apyhub.com/samples/sample.mp4"
4
generateThumbnail(apiToken, videoURL)
5
}

Step 7: Running the Program:

Finally, execute the program by running go run main.go to your terminal.
1
go run main.go
Tip: go run command is used in Go to compile and run a Go program in a single step. allows you to quickly execute a Go source file without explicitly building an executable binary.
This will make an API request to APYHub to generate a thumbnail from the specified video URL, and you should see the API response in your terminal.
1
Response: { "data" : "outputFileURL" }
Note: You can also find the complete code in this repository. Also, if you’re looking for a NodsJS integration guide, you can find it here.

Conclusion

That's it! It wasn't so difficult, right? We have successfully integrated the Video Thumbnail API using Go Lang!
Using this service, we can generate video thumbnails from literally any part of a video file or URL (e.g. YouTube). This way, we automate extracting thumbnails from videos, allowing for efficient and consistent extraction of thumbnails without the need for any manual work.
The ApyHub Video Thumbnail API can also be integrated into existing workflows and platforms, making it easy to incorporate thumbnail extraction into existing processes.
Good luck with using the API. Looking forward to any feedback on Discord.