mirror of
https://github.com/EvolutionAPI/evolution-audio-converter.git
synced 2025-07-13 15:14:50 -06:00
175 lines
3.8 KiB
Go
175 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/joho/godotenv"
|
|
)
|
|
|
|
var (
|
|
apiKey string
|
|
httpClient = &http.Client{}
|
|
bufferPool = sync.Pool{
|
|
New: func() interface{} {
|
|
return new(bytes.Buffer)
|
|
},
|
|
}
|
|
)
|
|
|
|
func init() {
|
|
// Carrega o arquivo .env
|
|
err := godotenv.Load()
|
|
if err != nil {
|
|
fmt.Println("Erro ao carregar o arquivo .env")
|
|
}
|
|
|
|
// Inicializa a variável apiKey
|
|
apiKey = os.Getenv("API_KEY")
|
|
if apiKey == "" {
|
|
fmt.Println("API_KEY não configurada no arquivo .env")
|
|
}
|
|
}
|
|
|
|
func validateAPIKey(c *gin.Context) bool {
|
|
if apiKey == "" {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Erro interno no servidor"})
|
|
return false
|
|
}
|
|
|
|
requestApiKey := c.GetHeader("apikey")
|
|
if requestApiKey == "" {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "API_KEY não fornecida"})
|
|
return false
|
|
}
|
|
|
|
if requestApiKey != apiKey {
|
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "API_KEY inválida"})
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func convertAudioToOpusWithDuration(inputData []byte) ([]byte, int, error) {
|
|
cmd := exec.Command("ffmpeg", "-i", "pipe:0", "-ac", "1", "-ar", "16000", "-c:a", "libopus", "-f", "ogg", "pipe:1")
|
|
|
|
outBuffer := bufferPool.Get().(*bytes.Buffer)
|
|
errBuffer := bufferPool.Get().(*bytes.Buffer)
|
|
defer bufferPool.Put(outBuffer)
|
|
defer bufferPool.Put(errBuffer)
|
|
|
|
outBuffer.Reset()
|
|
errBuffer.Reset()
|
|
|
|
cmd.Stdin = bytes.NewReader(inputData)
|
|
cmd.Stdout = outBuffer
|
|
cmd.Stderr = errBuffer
|
|
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
return nil, 0, fmt.Errorf("error during conversion: %v, details: %s", err, errBuffer.String())
|
|
}
|
|
|
|
convertedData := make([]byte, outBuffer.Len())
|
|
copy(convertedData, outBuffer.Bytes())
|
|
|
|
// Parsing da duração
|
|
outputText := errBuffer.String()
|
|
splitTime := strings.Split(outputText, "time=")
|
|
|
|
if len(splitTime) < 2 {
|
|
return nil, 0, errors.New("duração não encontrada")
|
|
}
|
|
|
|
re := regexp.MustCompile(`(\d+):(\d+):(\d+\.\d+)`)
|
|
matches := re.FindStringSubmatch(splitTime[2])
|
|
if len(matches) != 4 {
|
|
return nil, 0, errors.New("formato de duração não encontrado")
|
|
}
|
|
|
|
hours, _ := strconv.ParseFloat(matches[1], 64)
|
|
minutes, _ := strconv.ParseFloat(matches[2], 64)
|
|
seconds, _ := strconv.ParseFloat(matches[3], 64)
|
|
duration := int(hours*3600 + minutes*60 + seconds)
|
|
|
|
return convertedData, duration, nil
|
|
}
|
|
|
|
func fetchAudioFromURL(url string) ([]byte, error) {
|
|
resp, err := httpClient.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
return io.ReadAll(resp.Body)
|
|
}
|
|
|
|
func getInputData(c *gin.Context) ([]byte, error) {
|
|
if file, _, err := c.Request.FormFile("file"); err == nil {
|
|
return io.ReadAll(file)
|
|
}
|
|
|
|
if base64Data := c.PostForm("base64"); base64Data != "" {
|
|
return base64.StdEncoding.DecodeString(base64Data)
|
|
}
|
|
|
|
if url := c.PostForm("url"); url != "" {
|
|
return fetchAudioFromURL(url)
|
|
}
|
|
|
|
return nil, errors.New("Nenhum arquivo, base64 ou URL fornecido")
|
|
}
|
|
|
|
func processAudio(c *gin.Context) {
|
|
if !validateAPIKey(c) {
|
|
return
|
|
}
|
|
|
|
inputData, err := getInputData(c)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
// Conversão e resposta
|
|
convertedData, duration, err := convertAudioToOpusWithDuration(inputData)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"duration": duration,
|
|
"audio": base64.StdEncoding.EncodeToString(convertedData),
|
|
})
|
|
}
|
|
|
|
func main() {
|
|
// Obtém a porta do arquivo .env ou usa a porta 8080 por padrão
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
|
|
router := gin.Default()
|
|
|
|
// Endpoint para processar o áudio
|
|
router.POST("/process-audio", processAudio)
|
|
|
|
// Inicia o servidor na porta especificada
|
|
router.Run(":" + port)
|
|
}
|