Golang如何循环遍历视频帧和像素?

huangapple go评论80阅读模式
英文:

Golang loop through video frames and pixels?

问题

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

我知道这是一个复杂的问题,并且其他语言的库(比如Java的processing)可能有更好的方法来实现这个功能,但我只是想知道Golang是否具备这个能力。

英文:

Is it possible in Golang to loop through an .mp4/.mov video file frame by and modify each frame's pixels?

I know this is a complicated answer and there are better methods of doing this in libraries for other languages such as processing for Java, but I am just wondering if Golang has this capability.

答案1

得分: 4

我无法想到使用Golang来自行编辑视频帧的项目。

大多数其他高级语言也不会这样做,因为它们的速度不够快。即使考虑到Golang的速度和并发策略,这种任务通常也是用C语言完成的。

解决这个问题的最佳方法可能是大多数项目所采用的方法:使用FFmpeg,可以通过调用它作为命令行工具,通过Golang绑定或者将FFmpeg嵌入到项目中并使用CGO调用

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

第二个链接的项目名为joy4,可以执行与流相关的任务,以及其他有趣且复杂的任务。

当然,在Golang中直接完成这个任务是可能的,但由于缺乏处理解码、编码和编辑不同视频格式的库,这并不容易实现。

英文:

I can't think of any project using Golang to edit videos frames by itself.

Most of other higher level languages also do not do that, simply because they are not fast enough to do that. Even considering the speed and concurrency strategies of Golang. This type of task is typically done in C.

Probably, the best approach to solve this problem is what most projects do: using FFmpeg, either by calling it as a command line tool, through a Golang binding or embedding FFmpeg itself to your project and calling it using CGO

The first linked project creates an API to do simple tasks to a video (like adding an image) and may serve as a good reference.

The second linked project, called joy4, does stream related tasks, among other interesting and complex tasks.

Of course, it is possible to do this directly in Golang, but it is not easily feasible due to the lack of libraries to do the hard work of decoding, encoding and editing different formats of video.

答案2

得分: 2

我为Go语言编写了一个处理视频输入输出的FFmpeg包装器。你只需要使用go get github.com/AlexEidt/Vidio命令安装该包,并下载FFmpegFFprobe并将它们添加到系统路径中作为环境变量。

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

video, err := vidio.NewVideo("input.mp4")
// 错误处理...

options := vidio.Options{
	FPS: video.FPS(),
	Bitrate: video.Bitrate(),
    // 如果你想将音频从输入复制到输出,请包括"Audio"。
    Audio: "input.mp4",
}

writer, err := vidio.NewVideoWriter(
    "output.mp4",
    video.Width(),
    video.Height(),
    &options,
)
// 错误处理...

defer writer.Close()

for video.Read() {
    frame := video.FrameBuffer()
    // "frame"是按行主序存储帧数据的字节数组。
    // 每个像素以RGB格式的3个连续字节存储。
    err := writer.Write(frame)
    // 错误处理...
}

项目源代码:https://github.com/AlexEidt/Vidio

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

希望对你有帮助!

英文:

I made an FFmpeg wrapper for Go that handles video I/O. All you need is to install the package using go get github.com/AlexEidt/Vidio and download FFmpeg and FFprobe and add them to your system path as environment variables.

The example below will loop through every frame in input.mp4 (where you can modify the frames as you need to) and then stores them in 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...
}

The project source: https://github.com/AlexEidt/Vidio

FFmpeg Download: https://ffmpeg.org/download.html

Hopefully it helps!

huangapple
  • 本文由 发表于 2017年3月10日 16:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/42713413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定