Skip to content

Commit

Permalink
feat: support fix invalid image format
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxsen committed Jul 8, 2024
1 parent 832ba72 commit 9776e44
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func defaultConfig() *Config {
"avsox",
},
Handlers: []string{
"image_transcoder",
"poster_cropper",
"watermark_maker",
"tag_padder",
"duration_fixer",
"image_transcoder",
"plot_translater",
},
}
Expand Down
2 changes: 0 additions & 2 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ func (p *FFMpeg) ConvertToYuv420pJpegFromBytes(ctx context.Context, data []byte)
}()
cmd := exec.Command(p.cmd, "-i", "pipe:0", "-vf", "format=yuv420p", "-f", "image2", dstFile)
cmd.Stdin = bytes.NewReader(data)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
return nil, fmt.Errorf("call ffmpeg to conv failed, err:%w", err)
Expand Down
24 changes: 22 additions & 2 deletions processor/handler/image_transcode_handler.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package handler

import (
"av-capture/ffmpeg"
"av-capture/image"
"av-capture/model"
"av-capture/store"
"context"
"fmt"
"strings"

"github.com/xxxsen/common/logutil"
"go.uber.org/zap"
)

type imageTranscodeHandler struct {
ffmpegInst *ffmpeg.FFMpeg
}

func (p *imageTranscodeHandler) Name() string {
Expand Down Expand Up @@ -44,7 +47,15 @@ func (p *imageTranscodeHandler) transcode(ctx context.Context, name string, f *m
return f //不丢弃, 后续处理的时候报错, 方便发现问题
}
raw, err := image.TranscodeToJpeg(data)
if err != nil { //出现异常的, 都直接把图片丢掉, 这种通常是格式不对
if err != nil && strings.Contains(err.Error(), "luma/chroma subsampling ratio") && p.ffmpegInst != nil {
data, err = p.ffmpegInst.ConvertToYuv420pJpegFromBytes(ctx, data)
if err != nil {
logger.Error("use ffmpeg to correct invalid image data failed", zap.Error(err))
return nil
}
raw, err = image.TranscodeToJpeg(data)
}
if err != nil {
logger.Error("unable to convert image to jpeg format", zap.Error(err))
return nil
}
Expand All @@ -58,6 +69,15 @@ func (p *imageTranscodeHandler) transcode(ctx context.Context, name string, f *m
return f
}

func createImageTranscodeHandler(args interface{}) (IHandler, error) {
ffmpegInst, err := ffmpeg.NewFFMpeg()
if err != nil {
logutil.GetLogger(context.Background()).Error("unable to create ffmpeg instance, will not able to use some feature", zap.Error(err))
ffmpegInst = nil
}
return &imageTranscodeHandler{ffmpegInst: ffmpegInst}, nil
}

func init() {
Register(HImageTranscoder, HandlerToCreator(&imageTranscodeHandler{}))
Register(HImageTranscoder, createImageTranscodeHandler)
}
1 change: 0 additions & 1 deletion processor/handler/poster_crop_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func (c *posterCropHandler) Handle(ctx context.Context, fc *model.FileContext) e
if err != nil {
return fmt.Errorf("save cutted poster data failed, err:%w", err)
}
//TODO: 如果实在无法裁剪出有效的封面, 那么尝试取一张竖屏的, 或者直接使用骑兵的裁剪逻辑
fc.Meta.Poster = &model.File{
Name: "./poster.jpg",
Key: key,
Expand Down

0 comments on commit 9776e44

Please sign in to comment.