Skip to content

Commit

Permalink
fix: fix duration unit
Browse files Browse the repository at this point in the history
  • Loading branch information
xhyqaq committed Mar 14, 2024
1 parent b41988c commit f4b8451
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion internal/core/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -146,7 +148,12 @@ func (c *Community) SaveMedia(ctx context.Context, userId string, data []byte, f
var duration string = ""
if fileInfo.Format == "video/mp4" {
duration, err = c.GetVideoDuration(c.domain + fileKey + "?avinfo")
duration = strings.ReplaceAll(duration, ".", ":")
if err != nil {
c.xLog.Warn(err.Error())
return 0, err
}
duration, err = formatVideoDuration(duration)
c.xLog.Info(duration)
if err != nil {
c.xLog.Warn(err.Error())
return 0, err
Expand All @@ -168,6 +175,19 @@ func (c *Community) SaveMedia(ctx context.Context, userId string, data []byte, f
return id, err
}

func formatVideoDuration(duration string) (string, error) {
seconds, err := strconv.ParseFloat(duration, 64)
if err != nil {
return "", err
}

minutes := math.Floor(seconds / 60)
secondsRemainder := int(seconds) % 60

formattedTime := fmt.Sprintf("%d:%02d", int(minutes), secondsRemainder)
return formattedTime, nil
}

// for internal use,no need to add ctx
func (c *Community) getMediaInfo(fileKey string) (*File, error) {
r, err := c.S3Service.NewReader(context.Background(), fileKey, nil)
Expand Down

0 comments on commit f4b8451

Please sign in to comment.