【问题标题】:Golang loop through video frames and pixels?Golang遍历视频帧和像素?
【发布时间】:2022-04-19 03:07:27
【问题描述】:

在 Golang 中是否可以逐帧遍历 .mp4/.mov 视频文件并修改每一帧的像素?

我知道这是一个复杂的答案,并且有更好的方法在其他语言的库中执行此操作,例如 Java 处理,但我只是想知道 Golang 是否具有此功能。

【问题讨论】:

  • 语言本身?否(所有语言规范均不包括视频处理)。标准库?否(任何语言的标准库都不支持此)。任何外部/第 3 方库是否(部分)用 Go 编写?是的(就像大多数语言的库一样,谷歌它)。
  • 谢谢,但是有什么可以推荐的吗?当然,我事先在谷歌上搜索过,但我只找到了 videoq,它说它仅适用于 ubuntu。 (但尚未在 Windows 上测试过)

标签: go video-processing


【解决方案1】:

我想不出任何使用 Golang 自行编辑视频帧的项目。

大多数其他高级语言也没有这样做,仅仅是因为它们不够快,无法做到这一点。甚至考虑到 Golang 的速度和并发策略。这种类型的任务通常在 C 中完成。

可能,解决这个问题的最佳方法是大多数项目所做的:使用FFmpeg,将其称为command line tool, through a Golang bindingembedding FFmpeg itself to your project and calling it using CGO

第一个链接的项目创建了一个 API 来对视频执行简单的任务(例如添加图像),并且可以作为很好的参考。

第二个链接项目,称为joy4,除了其他有趣和复杂的任务外,还执行与流相关的任务。

当然,有可能直接在 Golang 中做到这一点,但由于缺乏库来完成对不同格式视频的解码、编码和编辑的艰苦工作,这并不容易实现.

【讨论】:

    【解决方案2】:

    我为 Go 制作了一个处理视频 I/O 的 FFmpeg 包装器。您只需使用go get github.com/AlexEidt/Vidio 安装软件包并下载FFmpegFFprobe 并将它们作为环境变量添加到您的系统路径中。

    下面的示例将遍历input.mp4 中的每一帧(您可以根据需要修改这些帧),然后将它们存储在output.mp4 中。

    video, err := vidio.NewVideo("input.mp4")
    // Error handling...
    
    options := vidio.Options{
        FPS: video.FPS(),
        Bitrate: video.Bitrate(),
        // Only include "Audio" if you'd like to copy audio from the
        // input to the output.
        Audio: "input.mp4",
    }
    
    writer, err := vidio.NewVideoWriter(
        "output.mp4",
        video.Width(),
        video.Height(),
        &options,
    )
    // Error handling...
    
    defer writer.Close()
    
    for video.Read() {
        frame := video.FrameBuffer()
        // "frame" is a byte array storing the frame data in row-major order.
        // Each pixel is stored as 3 sequential bytes in RGB format.
        err := writer.Write(frame)
        // Error handling...
    }
    

    项目来源:https://github.com/AlexEidt/Vidio

    FFmpeg 下载:https://ffmpeg.org/download.html

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      相关资源
      最近更新 更多