Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebP example #45

Closed
trong opened this issue May 12, 2016 · 0 comments
Closed

WebP example #45

trong opened this issue May 12, 2016 · 0 comments

Comments

@trong
Copy link

trong commented May 12, 2016

I use the following CLI-command to get webp image (screenshot):

ffmpeg -i video.ts -ss 00:00:00.100 -vframes 1 screen.webp

I tried to modify video-to-jpeg.go, but in result I got empty files with .webp extensions (if I use gmf.AV_PIX_FMT_RGB32) or error ... :

[libwebp_anim @ 0x709da00] Specified pixel format argb is invalid or not supported
2016/05/12 18:38:30 Error opening codec 'libwebp_anim:libwebp WebP image', averror: Invalid argument

... if I use gmf.AV_PIX_FMT_ARGB

Code:

package main

import (
    "log"
    "os"
    "strconv"

    "github.com/3d0c/gmf"
)

func assert(i interface{}, err error) interface{} {
    if err != nil {
        log.Fatal(err)
    }

    return i
}

var i int = 0

func writeFile(b []byte) {
    name := strconv.Itoa(i) + ".webp"

    fp, err := os.Create(name)
    if err != nil {
        log.Fatal(err)
    }

    defer func() {
        if err := fp.Close(); err != nil {
            log.Fatal(err)
        }
        i++
    }()

    if n, err := fp.Write(b); err != nil {
        log.Fatal(err)
    } else {
        log.Println(n, "bytes written to", name)
    }
}

func main() {
    srcFileName := "video.ts"

    inputCtx := assert(gmf.NewInputCtx(srcFileName)).(*gmf.FmtCtx)
    defer inputCtx.CloseInputAndRelease()

    srcVideoStream, err := inputCtx.GetBestStream(gmf.AVMEDIA_TYPE_VIDEO)
    if err != nil {
        log.Println("No video stream found in", srcFileName)
    }

    codec, err := gmf.FindEncoder(gmf.AV_CODEC_ID_WEBP)
    if err != nil {
        log.Fatal(err)
    }

    cc := gmf.NewCodecCtx(codec)
    defer gmf.Release(cc)

    cc.SetPixFmt(gmf.AV_PIX_FMT_ARGB).SetWidth(srcVideoStream.CodecCtx().Width()).SetHeight(srcVideoStream.CodecCtx().Height())

    if codec.IsExperimental() {
        cc.SetStrictCompliance(gmf.FF_COMPLIANCE_EXPERIMENTAL)
    }

    if err := cc.Open(nil); err != nil {
        log.Fatal(err)
    }

    swsCtx := gmf.NewSwsCtx(srcVideoStream.CodecCtx(), cc, gmf.SWS_BICUBIC)
    defer gmf.Release(swsCtx)

    dstFrame := gmf.NewFrame().SetWidth(srcVideoStream.CodecCtx().Width()).SetHeight(srcVideoStream.CodecCtx().Height()).SetFormat(gmf.AV_PIX_FMT_ARGB)
    defer gmf.Release(dstFrame)

    if err := dstFrame.ImgAlloc(); err != nil {
        log.Fatal(err)
    }

    for packet := range inputCtx.GetNewPackets() {
        if packet.StreamIndex() != srcVideoStream.Index() {
            // skip non video streams
            continue
        }
        ist := assert(inputCtx.GetStream(packet.StreamIndex())).(*gmf.Stream)

        for frame := range packet.Frames(ist.CodecCtx()) {
            swsCtx.Scale(frame, dstFrame)

            if p, ready, _ := dstFrame.EncodeNewPacket(cc); ready {
                writeFile(p.Data())
                defer gmf.Release(p)
            }
        }
        gmf.Release(packet)
    }

    gmf.Release(dstFrame)
}

Could you say how to do it right?

@trong trong closed this as completed Apr 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant