asticode / go-astiav

Golang ffmpeg and libav C bindings
MIT License
351 stars 38 forks source link

Add avdevice support #29

Closed Tryanks closed 1 year ago

Tryanks commented 1 year ago

Add avdevice support to turn on local cameras or other video devices. Already tested with v4l2 on Linux, works great. Example code:

    astiav.RegisterAllDevices()
    dictionary := astiav.NewDictionary()
    dictionary.Set("pixel_format", "mjpeg", 0)
    dictionary.Set("framerate", "30", 0)
    dictionary.Set("video_size", "1920x1080", 0)
    inputFormat := astiav.FindInputFormat("video4linux2")
    formatContext := astiav.AllocFormatContext()
    err := formatContext.OpenInput("/dev/video0", inputFormat, dictionary)
    if err != nil {
        panic(err)
    }
    err = formatContext.FindStreamInfo(nil)
    if err != nil {
        panic(err)
    }
    pkt := astiav.AllocPacket()
    for {
        err = formatContext.ReadFrame(pkt)
        if err != nil {
            panic(err)
        }
        // jpeg data per frame
        data := pkt.Data()
        fmt.Println(len(data))
    }
Tryanks commented 1 year ago

Thanks for the suggested changes. I didn't realize: the addition of av_find_input_format to support avdevice is a major change. I think there may be a need to add a series of peripheral features and unit tests around FindInputFormat?

asticode commented 1 year ago

Thanks for the suggested changes. I didn't realize: the addition of av_find_input_format to support avdevice is a major change. I think there may be a need to add a series of peripheral features and unit tests around FindInputFormat?

For now the tests you've added should be sufficient 👍 But if new methods are added to the InputFormat struct, tests should be updated as well indeed

asticode commented 1 year ago

Thanks for the PR! ❤️