Convert MP3 Files to WAV in Go

If you’re working with large audio files, retaining a high level of quality can be a difficult task. However, if you utilize the WAV file format, which is known for its high resolution, the difficulty is greatly diminished. In this article, we will discuss how you can convert popular file formats like MP3 to WAV instantly using the following API in Go.

Now, to start the process you will need to plug a few parameters into the code below, including the file to perform the operation on and your API key. You will notice there are two additional parameters for a file URL and the sample rate; these are optional but can be useful in certain situations.

import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/video/convert/to/wav"
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("sampleRate", "<number>")
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))
}

Your result will be a newly converted WAV file that you can use as needed. If you liked this tutorial, visit the Cloudmersive website to retrieve your free API key and check out our other conversion APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store