视频的第一帧

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

First frame of video

问题

我正在使用Golang作为后端和JavaScript作为前端创建一个单页面应用程序。

我想找到一种方法,使用Golang获取视频的第一帧。

首先,我将一个.mp4视频文件上传到服务器,并保存在服务器上。

有没有办法使用Golang获取这个视频的第一帧?使用JavaScript在前端应该是可行的,但我觉得这不是解决这个问题的正确方式。

我不知道如何使用Golang实现它,也没有找到任何有用的库或内置函数可以帮助我解决这个问题。

非常感谢任何建议或推荐。

英文:

I'm creating a single page application with Golang on the backend and javascript on the frontend.
I´d like to find a way how to get the first frame of a video using Golang.

First, I upload a .mp4 video file to the server. It is saved on the server.

Is there a way to get the first frame of this video, using Golang?
It should be possible to do it using Javascript on frontend, but I don't feel like it is the right way to solve this issue.

I have no idea how to implement it using Golang, and I haven't found any useful libraries, not even built-in functions that could help me to solve this.

Every piece of advice or any recommendations will be much appreciated.

答案1

得分: 12

如评论中建议的那样,使用ffmpeg是最简单的方法。下面是一个改编自这个答案的示例:

package main

import (
	"bytes"
	"fmt"
	"os/exec"
)

func main() {
	filename := "test.mp4"
	width := 640
	height := 360
	cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", "1", "-s", fmt.Sprintf("%dx%d", width, height), "-f", "singlejpeg", "-")
	var buffer bytes.Buffer
	cmd.Stdout = &buffer
	if cmd.Run() != nil {
		panic("could not generate frame")
	}
	// 对包含JPEG图像的buffer进行处理
}

请注意,这是一个Go语言的示例代码,用于使用ffmpeg从视频中提取一帧JPEG图像。

英文:

As suggested in the comments, using ffmpeg would be the easiest approach. Below is an example adapted from this answer:

package main

import (
	"bytes"
	"fmt"
	"os/exec"
)
func main() {
	filename := "test.mp4"
	width := 640
	height := 360
	cmd := exec.Command("ffmpeg", "-i", filename, "-vframes", "1", "-s", fmt.Sprintf("%dx%d", width, height), "-f", "singlejpeg", "-")
	var buffer bytes.Buffer
	cmd.Stdout = &buffer
	if cmd.Run() != nil {
		panic("could not generate frame")
	}
	// Do something with buffer, which contains a JPEG image
}

huangapple
  • 本文由 发表于 2016年2月15日 22:11:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/35411585.html
匿名

发表评论

匿名网友

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

确定