2023-04-09:使用 Golang 重写的 ffmpeg 示例encode_video.c,实现视频编码并将编码后

2023-04-09:使用 Golang 重写的 ffmpeg 示例encode_video.c,实现视频编码并将编码后的数据封装为容器格式,最终写入输出文件。

答案2023-04-09:

本文介绍的是使用 Golang 重写的 ffmpeg 示例代码 encode_video.c,该示例代码实现了将视频编码并封装为容器格式,并最终写入输出文件的功能。

这个示例程序的主要流程如下:

  1. 解析命令行参数,获取输出文件名和所用的编码器名称。

  2. 根据编码器名称查找对应的编码器。

  3. 分配和初始化一个编码器上下文结构体(AVCodecContext)。

  4. 设置编码器参数:比特率、分辨率、帧率等。

  5. 打开编码器。

  6. 创建一个 AVFrame 结构体并为其分配空间,用于存储待编码的视频帧数据。

  7. 创建一个 AVPacket 结构体,用于存储编码后的数据。

  8. 循环编码每一帧视频数据:

    a. 将待编码的视频数据填充到 AVFrame 结构体中。

    b. 发送视频帧到编码器,得到编码后的数据包。

    c. 将编码后的数据包写入输出文件。

  9. 关闭编码器,并在需要时向输出文件写入结束标记。

  10. 释放资源。

在该示例代码中,默认使用 H.264 编码器和 YUV420P 像素格式。在设置编码器参数时,需要指定视频的比特率、分辨率和帧率等参数。通过创建 AVFrame 结构体并为其分配空间,可以将待编码的视频数据填入其中,并发送给编码器进行编码。编码后的数据通过 AVPacket 结构体进行封装,最终写入输出文件。

需要注意的是,在实际应用中,还需要根据具体需求进行相应的配置和优化,例如设置 GOP 大小、调整编码速度等参数,以提高视频质量和编码效率。同时,还需要考虑容器格式的选择,以满足不同场景下的需求。

总之,这个示例代码提供了一个简单的视频编码和封装的实现,为使用 ffmpeg 进行视频处理和转码提供了参考和思路。

代码见moonfdd/ffmpeg-go库。

命令如下:

go run ./examples/internalexamples/encode_video/main.go ./out/encode_video.mp4 mpeg2video

./lib/ffplay  ./out/encode_video.mp4

go代码如下:

package main

import (
    "fmt"
    "os"
    "unsafe"

    "github.com/moonfdd/ffmpeg-go/ffcommon"
    "github.com/moonfdd/ffmpeg-go/libavcodec"
    "github.com/moonfdd/ffmpeg-go/libavutil"
)

func main0() (ret ffcommon.FInt) {
    var filename, codec_name string
    var codec *libavcodec.AVCodec
    var c *libavcodec.AVCodecContext
    var i, x, y ffcommon.FInt
    var f *os.File
    var frame *libavutil.AVFrame
    var pkt *libavcodec.AVPacket
    endcode := [...]ffcommon.FUint8T{0, 0, 1, 0xb7}

    if len(os.Args) <= 2 {
        fmt.Printf("Usage: %s <output file> <codec name>\n", os.Args[0])
        return 0
    }
    filename = os.Args[1]
    codec_name = os.Args[2]

    /* find the mpeg1video encoder */
    codec = libavcodec.AvcodecFindEncoderByName(codec_name)
    if codec == nil {
        fmt.Printf("Codec '%s' not found\n", codec_name)
        os.Exit(1)
    }

    c = codec.AvcodecAllocContext3()
    if c == nil {
        fmt.Printf("Could not allocate video codec context\n")
        os.Exit(1)
    }

    pkt = libavcodec.AvPacketAlloc()
    if pkt == nil {
        os.Exit(1)
    }

    /* put sample parameters */
    c.BitRate = 400000
    /* resolution must be a multiple of two */
    c.Width = 352
    c.Height = 288
    /* frames per second */
    c.TimeBase = libavutil.AVRational{1, 25}
    c.Framerate = libavutil.AVRational{25, 1}

    /* emit one intra frame every ten frames
     * check frame pict_type before passing frame
     * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
     * then gop_size is ignored and the output of encoder
     * will always be I frame irrespective to gop_size
     */
    c.GopSize = 10
    c.MaxBFrames = 1
    c.PixFmt = libavutil.AV_PIX_FMT_YUV420P

    if codec.Id == libavcodec.AV_CODEC_ID_H264 {
        libavutil.AvOptSet(c.PrivData, "preset", "slow", 0)
    }

    /* open it */
    if c.AvcodecOpen2(codec, nil) < 0 {
        fmt.Printf("Could not open codec\n")
        os.Exit(1)
    }

    f, _ = os.Create(filename)
    if f == nil {
        fmt.Printf("Could not open %s\n", filename)
        os.Exit(1)
    }

    frame = libavutil.AvFrameAlloc()
    if frame == nil {
        fmt.Printf("Could not allocate video frame\n")
        os.Exit(1)
    }
    frame.Format = c.PixFmt
    frame.Width = c.Width
    frame.Height = c.Height

    ret = frame.AvFrameGetBuffer(0)
    if ret < 0 {
        fmt.Printf("Could not allocate the video frame data\n")
        os.Exit(1)
    }
    /* encode 1 second of video */
    for i = 0; i < 25; i++ {
        // fflush(stdout);

        /* make sure the frame data is writable */
        ret = frame.AvFrameMakeWritable()
        if ret < 0 {
            os.Exit(1)
        }

        /* prepare a dummy image */
        /* Y */
        for y = 0; y < c.Height; y++ {
            for x = 0; x < c.Width; x++ {
                *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(frame.Data[0])) + uintptr(y*frame.Linesize[0]+x))) = byte((x + y + i*3) % 256)
            }
        }

        /* Cb and Cr */
        for y = 0; y < c.Height/2; y++ {
            for x = 0; x < c.Width/2; x++ {
                *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(frame.Data[1])) + uintptr(y*frame.Linesize[1]+x))) = byte((128 + y + i*2) % 256)
                *(*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(frame.Data[2])) + uintptr(y*frame.Linesize[2]+x))) = byte((64 + x + i*5) % 256)
            }
        }

        frame.Pts = int64(i)

        /* encode the image */
        encode(c, frame, pkt, f)
    }

    /* flush the encoder */
    encode(c, nil, pkt, f)

    /* add sequence end code to have a real MPEG file */
    if codec.Id == libavcodec.AV_CODEC_ID_MPEG1VIDEO || codec.Id == libavcodec.AV_CODEC_ID_MPEG2VIDEO {
        f.Write(endcode[:])
    }
    f.Close()

    libavutil.AvFrameFree(&frame)
    libavcodec.AvPacketFree(&pkt)
    libavcodec.AvcodecFreeContext(&c)

    return 0
}

func encode(enc_ctx *libavcodec.AVCodecContext, frame *libavutil.AVFrame, pkt *libavcodec.AVPacket, output *os.File) {
    var ret ffcommon.FInt

    /* send the frame to the encoder */
    if frame != nil {
        fmt.Printf("Send frame %3d\n", frame.Pts)
    }

    ret = enc_ctx.AvcodecSendFrame(frame)
    if ret < 0 {
        fmt.Printf("rror sending a frame for encoding\n")
        os.Exit(1)
    }

    for ret >= 0 {
        ret = enc_ctx.AvcodecReceivePacket(pkt)
        if ret == -libavutil.EAGAIN || ret == libavutil.AVERROR_EOF {
            return
        } else if ret < 0 {
            fmt.Printf("Error during encoding\n")
            os.Exit(1)
        }
        fmt.Printf("Write packet %3d (size=%5d)\n", pkt.Pts, pkt.Size)
        output.Write(ffcommon.ByteSliceFromByteP(pkt.Data, int(pkt.Size)))
        pkt.AvPacketUnref()
    }
}

func main() {
    os.Setenv("Path", os.Getenv("Path")+";./lib")
    ffcommon.SetAvutilPath("./lib/avutil-56.dll")
    ffcommon.SetAvcodecPath("./lib/avcodec-58.dll")
    ffcommon.SetAvdevicePath("./lib/avdevice-58.dll")
    ffcommon.SetAvfilterPath("./lib/avfilter-56.dll")
    ffcommon.SetAvformatPath("./lib/avformat-58.dll")
    ffcommon.SetAvpostprocPath("./lib/postproc-55.dll")
    ffcommon.SetAvswresamplePath("./lib/swresample-3.dll")
    ffcommon.SetAvswscalePath("./lib/swscale-5.dll")

    genDir := "./out"
    _, err := os.Stat(genDir)
    if err != nil {
        if os.IsNotExist(err) {
            os.Mkdir(genDir, 0777) //  Everyone can read write and execute
        }
    }

    main0()
}

在这里插入图片描述

本作品采用《CC 协议》,转载必须注明作者和本文链接
微信公众号:福大大架构师每日一题。最新面试题,涉及golang,rust,mysql,redis,云原生,算法,分布式,网络,操作系统。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
473
粉丝
21
喜欢
37
收藏
22
排名:457
访问:2.0 万
私信
所有博文
社区赞助商