4

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.

taraf
  • 777
  • 2
  • 10
  • 28
Matúš Čongrády
  • 1,340
  • 3
  • 17
  • 29
  • First frame of many videos is just a black, blank picture (e.g. movies). You should aim at something more meaningful, e.g. a frame from its middle. – icza Feb 15 '16 at 14:13
  • You could: a) decode the video stream and take any frame you like, b) find a library that decodes the video stream and take any frame you like or c) use an external program to do that for you, I guess `ffmpeg` could do it – mrd0ll4r Feb 15 '16 at 14:14

1 Answers1

12

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
}
Community
  • 1
  • 1
  • Tim your answer's helped me. Tanks you Tim. I'm extended your example. I'm using stdin and stdout for work ffmpeg. [stdin/stdout gist](https://gist.github.com/ArseniySavin/04f3d50ff6f38bbb472dc2f715b1936f) – Арсений Савин Dec 05 '20 at 18:47