go-astiav

repository·master·Indexed 20 days ago

https://github.com/asticode/go-astiav

A Go library providing idiomatic C bindings for FFmpeg version n8.0. It features typed constants, standard error patterns, and struct-based functions to improve developer experience over older bindings. The library includes Go implementations of official FFmpeg examples and provides utilities for memory management (Alloc, Unref, Free), audio buffering via AudioFifo, and bitstream filter processing through BitStreamFilterContext.

Tokens
57.9K
Snippets
243
Records
320
Agent score
72%

What's inside go-astiav

  1. Overview of astiav

    master

    astiav is a Golang library providing C bindings for FFmpeg. It is specifically designed to be compatible with FFmpeg n8.0.

    Key features include:

    • A more idiomatic Go API (standard error patterns, typed constants/flags, and struct-based functions).
    • Go implementations of official FFmpeg examples.
    • Full test coverage.

    Warning: Breaking changes may be introduced without following the v2 Go module pattern. Check BREAKING_CHANGES.md for details.

  2. Memory management patterns: Alloc(), Unref(), and Free()

    master

    When working with FFmpeg objects like packets or frames, you must manage memory carefully to avoid leaks or use-after-free errors.

    General Pattern:

    1. Alloc(): Use this to allocate an object once, often outside of loops, to allow for reuse.
    2. defer .Free(): Always ensure the allocated object is freed when it is no longer needed by the application.
    3. .Unref(): Inside loops where an object (like a packet) is being populated by a method like ReadFrame(), you must call .Unref() to release the internal data 'injected' by the FFmpeg C function. Using a closure inside the loop is a recommended way to handle this with defer.
    // You can allocate the packet once and reuse the same object in the for loop below
    pkt := astiav.AllocPacket()
    
    // However, once you're done using the packet, you need to make sure to free it
    defer pkt.Free()
    
    // Loop
    for {
        // We'll use a closure to ease unreferencing the packet
        func() {
            // Read frame using the same packet every time
            formatContext.ReadFrame(pkt)
    
            // However make sure to unreference the packet once you're done with what 
            // have been "injected" by the .ReadFrame() method
            defer pkt.Unref()
    
            // Here you can do whatever you feel like with your packet
        }()
    }
  3. Build astiav on Windows using MSYS2

    master

    Building on Windows requires the msys2 / mingw64 gcc toolchain.

    1. Install MSYS2.
    2. Open the Mingw64 shell from the installation folder.
    3. Run the following commands to update packages, install build requirements, and clone the repository:

    Note: For pkg-config, it is recommended to use pkgconfiglite from Chocolatey (choco). Ensure CGO and PKG_CONFIG environment variables are set to point to your FFmpeg build folder.

    # Update Packages
    pacman -Syu
    
    # Install Requirements to Build
    pacman -S --noconfirm --needed git diffutils mingw-w64-x86_64-toolchain pkg-config make nasm
    
    # Clone the repository using git
    git clone https://github.com/asticode/go-astiav
    cd go-astiav
  4. Configure environment variables for FFmpeg dependency

    master

    To ensure your Go code automatically picks up the FFmpeg dependency during compilation, you must set the following environment variables. Replace {{ path to your ffmpeg directory }} with the absolute path to your FFmpeg installation directory.

    export CGO_CFLAGS="-I{{ path to your ffmpeg directory }}/include/",
    export CGO_LDFLAGS="-L{{ path to your ffmpeg directory }}/lib/",
    export PKG_CONFIG_PATH="{{ path to your ffmpeg directory }}/lib/pkgconfig",
  5. Explore astiav FFmpeg examples

    master

    The examples/ directory contains Go implementations that mirror the official FFmpeg C examples. Use these to understand specific workflows:

    • BitStream Filtering: examples/bit_stream_filtering/main.go
    • Custom IO Demuxing: examples/custom_io_demuxing/main.go
    • Custom IO Muxing: examples/custom_io_muxing/main.go
    • Demuxing/Decoding: examples/demuxing_decoding/main.go
    • Filtering: examples/filtering/main.go
    • Frame data manipulation: examples/frame_data_manipulation/main.go
    • Hardware Decoding/Filtering: examples/hardware_decoding_filtering/main.go
    • Hardware Encoding: examples/hardware_encoding/main.go
    • Remuxing: examples/remuxing/main.go
    • Resampling audio: examples/resampling_audio/main.go
    • Scaling video: examples/scaling_video/main.go
    • Transcoding: examples/transcoding/main.go

    Tip: You can use the video samples in the testdata directory for your own tests.

  6. Migrate CodecParameters and HardwareFrameContext (v0.25.0)

    master

    Several API renames and replacements occurred in version 0.25.0:

    • Codec Type: CodecParameters.CodecType and CodecParameters.SetCodecType were removed. Use CodecParameters.MediaType and CodecParameters.SetMediaType instead.
    • Hardware Pixel Format: HardwareFrameContext.SetPixelFormat was replaced by HardwareFrameContext.SetHardwarePixelFormat.
    • Interrupt Callbacks: FormatContext.SetInterruptCallback was replaced by FormatContext.SetIOInterrupter.
  7. Use Buffersink/Buffersrc filter contexts instead of NewFilterContext (v0.29.0 and v0.24.0)

    master

    The generic NewFilterContext has been removed. When working with filter graphs, you must now use specific buffer sink or source contexts:

    1. Use FilterGraph.NewBuffersinkFilterContext or FilterGraph.NewBuffersrcFilterContext.
    2. For Buffersink: You do not need additional steps after creation. Use BuffersinkFilterContext.GetFrame to retrieve frames.
    3. For Buffersrc: The args parameter was removed from the constructor. Instead, after calling NewBuffersrcFilterContext, you must call BuffersrcFilterContext.SetParameters followed by BuffersrcFilterContext.Initialize. Use BuffersrcFilterContext.AddFrame to provide frames.
    4. When using FilterInOut.SetFilterContext, use the .FilterContext field from the respective buffer context.
  8. Migrate Codec supported format fields in v0.40.0

    master

    In version 0.40.0, the fields used to query supported formats on a Codec have been renamed for clarity. Replace the old names with the new Supported prefix:

    • Codec.ChannelLayouts $\rightarrow$ Codec.SupportedChannelLayouts
    • Codec.PixelFormats $\rightarrow$ Codec.SupportedPixelFormats
    • Codec.SampleFormats $\rightarrow$ Codec.SupportedSampleFormats