diff --git a/src/services/publish/handler.go b/src/services/publish/handler.go index cdd8348..af24051 100644 --- a/src/services/publish/handler.go +++ b/src/services/publish/handler.go @@ -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) { diff --git a/src/services/videoprocessor/main.go b/src/services/videoprocessor/main.go index fa052ea..0ceb1d0 100644 --- a/src/services/videoprocessor/main.go +++ b/src/services/videoprocessor/main.go @@ -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" ) @@ -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 @@ -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 } @@ -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 }