Skip to content

Commit

Permalink
Merge pull request #4 from DeliverBle/fix/webm-to-mp3
Browse files Browse the repository at this point in the history
Fix/webm to mp3
  • Loading branch information
sigridjineth authored Dec 30, 2022
2 parents 8af6ab7 + 54e44f4 commit fdb793c
Show file tree
Hide file tree
Showing 14 changed files with 435 additions and 19 deletions.
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
FROM golang:1.18.2-alpine AS builder
MAINTAINER "Sigrid Jin From SOPT 30th Deliverble"

# install dependencies
RUN apk update && apk add --no-cache \
alpine-sdk \
ffmpeg

ENV GO111MODULE=on \
CGO_ENABLED=0 \
GOOS=linux \
Expand All @@ -14,6 +19,9 @@ COPY go.sum ./
RUN go mod download
COPY . ./

# test only ./test/FFMpegConvert_test.go
RUN go test -v ./test/FFMpegConvert_test.go

RUN go build deliverble-recording-msa/server/s3_server

EXPOSE 8020
Expand Down
30 changes: 30 additions & 0 deletions go.sum

Large diffs are not rendered by default.

130 changes: 130 additions & 0 deletions preprocess/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
recordingpb "deliverble-recording-msa/protos/v1/recording"
userpb "deliverble-recording-msa/protos/v1/user"
"deliverble-recording-msa/server/s3_server/client"
ffw "deliverble-recording-msa/server/s3_server/client"
"fmt"
_ "github.com/aws/aws-sdk-go-v2/aws"
_ "github.com/aws/aws-sdk-go-v2/config"
Expand All @@ -25,6 +26,7 @@ import (
"log"
"net/http"
"os"
"strings"
"time"
)

Expand Down Expand Up @@ -176,6 +178,134 @@ func (s *S3Server) UploadRecording(_ context.Context, req *recordingpb.UploadRec
}
}

func (s *S3Server) UploadRecordingV2(_ context.Context, req *recordingpb.UploadRecordingRequest) (*recordingpb.UploadRecordingResponse, error) {
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
info := client.S3Info{
AwsS3Region: os.Getenv("AP_NORTHEAST_2"),
BucketName: os.Getenv("DELIVERBLE_BUCKET_NAME"),
AwsAccessKey: os.Getenv("DELIVERBLE_ACCESS_KEY"),
AwsSecretKey: os.Getenv("DELIVERBLE_SECRET_KEY"),
}

client, err := info.InitS3DefaultConfig()
if err != nil {
log.Println(err)
return nil, err
}
log.Println("S3 client init success ::::::::::::: ", client)

// 0. work with given uploaded file
filename := fmt.Sprintf("%v.mp3", time.Now().Unix())
filepath := "/tmp/" + filename

err = ioutil.WriteFile(filepath, req.Recording, 0644)
if err != nil {
log.Println("Error writing to file: ", err)
return nil, err
}

// 1. change `uploaded` mp3 file to webm file
fileNameWebm, errChange := ffw.ChangeFileNameMp3ToWebm(filepath)
if errChange != nil {
log.Println("UploadRecordingV2 Error ::::::: ", errChange)
response := &recordingpb.UploadRecordingResponse{
Result: false,
Url: "",
Key: "",
}
return response, err
}
filename = *fileNameWebm

// 2. convert converted `.webm` file to `.mp3` file
// filename to remove `.mp3` but not to add `.webm`
errConvert := ffw.ConvertWebmBlobToMp3File(strings.Replace(filename, ".mp3", "", -1))
if errConvert != nil {
log.Println("UploadRecordingV2 Error ::::::: ", errConvert)
response := &recordingpb.UploadRecordingResponse{
Result: false,
Url: "",
Key: "",
}
return response, err
}

var response *recordingpb.UploadRecordingResponse
recording, err := info.UploadRecordingV2(filename, filepath)
if err != nil {
response = &recordingpb.UploadRecordingResponse{
Result: false,
Url: "",
Key: "",
}
return response, err
} else {
response = &recordingpb.UploadRecordingResponse{
Result: true,
Url: recording.Location,
Key: *recording.Key,
}
return response, nil
}
}

func UploadRecordingHandlerV2(c echo.Context) error {
file, err := c.FormFile("file") // file : "file" parsing
if err != nil {
c.JSON(http.StatusBadRequest, err)
return err
}

src, err := file.Open() // file api open
if err != nil {
log.Println("Error opening file: ", err)
err := c.JSON(http.StatusInternalServerError, err)
if err != nil {
c.JSON(http.StatusInternalServerError, err)
return err
}
return err
}

defer src.Close()
buffer := make([]byte, file.Size) // file size buf define
_, err = src.Read(buffer)
if err != nil {
c.JSON(http.StatusInternalServerError, err)
return err
} // file read

ctx := context.Background()

conn, err := grpc.Dial("localhost:8020", grpc.WithInsecure())
if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, nil)
}

taskClient := recordingpb.NewRecordingTaskClient(conn)
r, err := taskClient.UploadRecordingV2(ctx, &recordingpb.UploadRecordingRequest{Recording: buffer})

if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, nil)
}

if r.Result == false {
return c.JSON(http.StatusInternalServerError, r)
}

successResponse := &client.UploadRecordingHandlerResponse{
Code: http.StatusCreated,
Url: r.Url,
Key: r.Key,
}
return c.JSON(http.StatusCreated, successResponse)
}

func UploadRecordingHandler(c echo.Context) error {
file, err := c.FormFile("file") // file : "file" parsing
if err != nil {
Expand Down
41 changes: 25 additions & 16 deletions protos/v1/recording/recording.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions protos/v1/recording/recording.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option go_package = "github.com/DeliverBle/deliverble-recording-microservice";

service recording_task {
rpc UploadRecording (UploadRecordingRequest) returns (UploadRecordingResponse) {}
rpc UploadRecordingV2 (UploadRecordingRequest) returns (UploadRecordingResponse) {}
}

// UploadRecordingRequest wrapped with byte data
Expand Down
38 changes: 37 additions & 1 deletion protos/v1/recording/recording_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions server/s3_server/client/FFMpeg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package client

import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
)

func ChangeFileNameMp3ToWebm(inputFileName string) (*string, error) {
// Check if the input file exists
if _, err := os.Stat(inputFileName); os.IsNotExist(err) {
return nil, fmt.Errorf("file %s does not exist", inputFileName)
}

// Use a regular expression to remove all instances of ".mp3" from the file name
r := regexp.MustCompile("\\.mp3")
outputFileName := r.ReplaceAllString(inputFileName, "")

// Append the new extension to the file name
outputFileNameWithWebm := outputFileName + ".webm"

// Rename the input file to the output file
err := os.Rename(inputFileName, outputFileNameWithWebm)
if err != nil {
return nil, fmt.Errorf("failed to rename file %s to %s: %v", inputFileName, outputFileNameWithWebm, err)
}

return &outputFileName, nil
}

func ConvertWebmBlobToMp3File(input string) error {
cmd := exec.Command("ffmpeg", "-i", input+".webm", input+".mp3")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
return fmt.Errorf("ffmpeg command failed")
}
fmt.Println("Result: " + out.String())

return nil
}
Loading

0 comments on commit fdb793c

Please sign in to comment.