How to Convert a Video to WEBM in Go

Cloudmersive
2 min readAug 24, 2021

--

With the popularity of the online video steadily rising, the WEBM file format is also gaining popularity due to its compatibility with most web browsers. However, if you don’t have the proper tools, transforming your videos to this relatively new format can be quite difficult. In this quick tutorial, we will show you how to easily convert videos to WEBM format by using an API in Go.

We will start the process by inputting the file (or file URL if over 2 GB) and the API key into the following code:

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/video/convert/to/webm"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("fileUrl", "<string>")
req.Header.Add("maxWidth", "<integer>")
req.Header.Add("maxHeight", "<integer>")
req.Header.Add("preserveAspectRatio", "<boolean>")
req.Header.Add("frameRate", "<integer>")
req.Header.Add("quality", "<integer>")
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}

With this simple operation, you will be able to optimize any of your videos for sharing on the web! You can retrieve your free API key from the Cloudmersive website, which will provide access to 800 monthly calls across our library of APIs.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet