diff --git a/cmd/ffwebp/main.go b/cmd/ffwebp/main.go index d512bf3..4ad3a93 100644 --- a/cmd/ffwebp/main.go +++ b/cmd/ffwebp/main.go @@ -46,6 +46,10 @@ func main() { Aliases: []string{"c"}, Usage: "force output codec (jpeg, png, ...)", }, + &cli.StringFlag{ + Name: "input.codec", + Usage: "force input codec, skip sniffing (jpeg, png, ...)", + }, &cli.BoolFlag{ Name: "sniff", Aliases: []string{"f"}, @@ -265,7 +269,9 @@ func run(_ context.Context, cmd *cli.Command) error { func processOne(input, output string, cmd *cli.Command, common *opts.Common, logger io.Writer) error { var ( reader io.Reader = os.Stdin - writer *countWriter = &countWriter{w: os.Stdout} + writer *countWriter = &countWriter{ + w: os.Stdout, + } ) if input != "-" { @@ -283,14 +289,16 @@ func processOne(input, output string, cmd *cli.Command, common *opts.Common, log logx.Fprintf(logger, "reading input from \n") } - sniffed, reader2, err := codec.Sniff(reader, input, cmd.Bool("sniff")) + forced := cmd.String("input.codec") + + sniffed, reader2, err := codec.Sniff(reader, input, forced, cmd.Bool("sniff")) if err != nil { return err } reader = reader2 - logx.Fprintf(logger, "sniffed codec: %s (%q)\n", sniffed.Codec, sniffed) + logx.Fprintf(logger, "sniffed codec: %s (%q, forced=%v)\n", sniffed.Codec, sniffed, forced != "") var mappedFromDir bool diff --git a/internal/codec/detect.go b/internal/codec/detect.go index 8ca7ca2..f36a58e 100644 --- a/internal/codec/detect.go +++ b/internal/codec/detect.go @@ -30,7 +30,20 @@ func (s *Sniffed) String() string { return builder.String() } -func Sniff(reader io.Reader, input string, ignoreExtension bool) (*Sniffed, io.Reader, error) { +func Sniff(reader io.Reader, input, force string, ignoreExtension bool) (*Sniffed, io.Reader, error) { + if force != "" { + codec, err := FindCodec(strings.ToLower(force), false) + if err != nil { + return nil, nil, err + } + + return &Sniffed{ + Header: []byte(force), + Confidence: 100, + Codec: codec, + }, reader, nil + } + var ( hintedExt string hintedCodec Codec @@ -38,6 +51,7 @@ func Sniff(reader io.Reader, input string, ignoreExtension bool) (*Sniffed, io.R if !ignoreExtension { hintedExt = strings.ToLower(strings.TrimPrefix(filepath.Ext(input), ".")) + if hintedExt != "" { hintedCodec, _ = FindCodec(hintedExt, false) }