Skip to content

Commit

Permalink
fix: fix update cover,video
Browse files Browse the repository at this point in the history
  • Loading branch information
Attack825 committed Aug 21, 2023
1 parent 355fcf5 commit c8cd71d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
19 changes: 15 additions & 4 deletions src/services/publish/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,36 @@ func (a PublishServiceImpl) ListVideo(ctx context.Context, req *publish.ListVide
}
return
}
// todo: 使用协程完成,开 go func
videoIds := make([]uint32, 0, len(videos))
for _, video := range videos {
videoIds = append(videoIds, video.ID)
}

//todo: go func
queryVideoResp, err := FeedClient.QueryVideos(ctx, &feed.QueryVideosRequest{
ActorId: req.ActorId,
VideoIds: videoIds,
})
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Warnf("queryVideoResp failed to obtain")
logging.SetSpanError(span, err)
resp = &publish.ListVideoResponse{
StatusCode: strings.FeedServiceInnerErrorCode,
StatusMsg: strings.FeedServiceInnerError,
}
return
}

logger.WithFields(logrus.Fields{
"response": resp,
}).Debug("all process done, ready to launch response")
return &publish.ListVideoResponse{
resp = &publish.ListVideoResponse{
StatusCode: strings.ServiceOKCode,
StatusMsg: strings.ServiceOK,
VideoList: queryVideoResp.VideoList,
}, nil
}
return
}

func (a PublishServiceImpl) CountVideo(ctx context.Context, req *publish.CountVideoRequest) (resp *publish.CountVideoResponse, err error) {
Expand Down
52 changes: 35 additions & 17 deletions src/services/videoprocessor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import (
"GuGoTik/src/utils/logging"
"GuGoTik/src/utils/pathgen"
"GuGoTik/src/utils/rabbitmq"
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"github.com/streadway/amqp"
"os"
"os/exec"
"sync"
)
Expand Down Expand Up @@ -112,7 +111,6 @@ func Consume(channel *amqp.Channel) {
}).Errorf("Error when adding watermark to video.")
logging.SetSpanError(span, err)
}
//todo: update封面

// 保存到数据库
err = database.Client.WithContext(ctx).Create(&raw).Error
Expand Down Expand Up @@ -145,21 +143,28 @@ func extractVideoCover(ctx context.Context, video *models.RawVideo) error {
RawFileName := video.FileName
CoverFileName := video.CoverName
RawFilePath := file.GetLocalPath(ctx, RawFileName)
CoverFilePath := file.GetLocalPath(ctx, CoverFileName)
cmdArgs := []string{
"-i", RawFilePath,
"-ss", "00:00:01",
"-vframes", "1",
CoverFilePath,
"-i", RawFilePath, "-vframes", "1", "-an", "-f", "image2pipe", "-",
}
cmd := exec.Command("ffmpeg", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Create a bytes.Buffer to capture stdout
var buf bytes.Buffer
cmd.Stdout = &buf

err := cmd.Run()
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Warnf("failed to get video cover")
}).Warnf("cmd.Run() failed with %s\n", err)
logging.SetSpanError(span, err)
return err
}
// buf.Bytes() now contains the image data. You can use it to write to a file or send it to an output stream.
_, err = file.Upload(ctx, CoverFileName, bytes.NewReader(buf.Bytes()))
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Warnf("failed to upload video cover")
logging.SetSpanError(span, err)
return err
}
Expand All @@ -174,20 +179,33 @@ func addWatermarkToVideo(ctx context.Context, video *models.RawVideo) error {
RawFileName := video.FileName
FinalFileName := pathgen.GenerateFinalVideoName(video.ActorId, video.Title, video.VideoId)
RawFilePath := file.GetLocalPath(ctx, RawFileName)
FinalFilePath := file.GetLocalPath(ctx, FinalFileName)
cmdArgs := []string{
"-i", RawFilePath,
"-vf", fmt.Sprintf("drawtext=text='%s':x=(w-text_w-10):y=10:fontsize=24:fontcolor=white", video.Title),
FinalFilePath,
"-vf", "drawtext=text=" + video.Title + ":x=(w-text_w-10):y=10:fontsize=24:fontcolor=white",
"-c:v", "copy",
"-f", "mp4",
"-",
}

cmd := exec.Command("ffmpeg", cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
var buf bytes.Buffer
cmd.Stdout = &buf

// Execute the command
err := cmd.Run()
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Warnf("failed to add video watermark")
}).Warnf("cmd.Run() failed with %s\n", err)
logging.SetSpanError(span, err)
}

// Write the captured stdout to a file
_, err = file.Upload(ctx, FinalFileName, bytes.NewReader(buf.Bytes()))
if err != nil {
logger.WithFields(logrus.Fields{
"err": err,
}).Warnf("failed to upload video with watermark")
logging.SetSpanError(span, err)
return err
}
Expand Down

0 comments on commit c8cd71d

Please sign in to comment.