问题描述
我使用Go lang创建了一个单页应用程序(在前端显然是javascript)。
我想要找到一种方法,使用Go lang获得视频的第一帧。
有没有办法使用Go lang获取此视频的第一帧?
应该可以在前端使用Javascript来做,但我不认为这是解决这个问题的正确方法。
我有不知道如何使用Go lang来实现它,而且我还没有找到任何有用的架构师,甚至没有内置的函数可以帮助我解决这个问题。
每个
正如评论中所建议的那样,使用将是最简单的方法。下面是一个从改编的例子: package main
import(
bytes
fmt
os / exec
)
func main(){
filename:=test.mp4
width:= 640
height:= 360
cmd:= exec.Command(ffmpeg, -i,文件名,-vframes,1,-s,fmt.Sprintf(%dx%d,宽度,高度),-f,singlejpeg, - )
var buffer bytes.Buffer
cmd.Stdout =& buffer
if cmd.Run()!= nil {
panic(could not generate frame)
}
//使用包含JPEG图像的缓冲区做一些事情
}
I'm creating a single page application with Go lang on backend (and obviously javascript on frontend).I´d like to find a way how to get a first frame of a video using Go lang.
First, I upload a .mp4 video file to the server. It is saved on the server.
Is there a way to get first frame of this video, using Go lang?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 Go lang, and I haven't find any useful librariers, not even built-in functions that could help me to solve this.
Every piece of advice or any recoomendations will be much appretiated.
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
}
这篇关于视频的第一帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!