I
I
ixon2019-06-08 11:59:32
go
ixon, 2019-06-08 11:59:32

How to create a webm video using golang?

With what help to implement this, in which direction to dig?
A golang program, when executed, creates a video in webm format, the first 3 seconds the background is filled with RGB (255,0,0), from 3 to 6 RGB (0,255,0) and from 6 to 9 seconds RGB (0,0,255).
How can this be done, what libraries can be used for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2019-06-09
@pav5000

It can be so. The ffmpeg console utility must be installed on the system.

Click to reveal the code
package main

import (
  "io"
  "os"
  "os/exec"
  "strings"
)

func main() {
  resulution := "640x480" // Разрешение видео
  framerate := "1"        // Частота кадров, здесб нам достаточно 1 кадр в секунду
  outfile := "out.webm"   // Файл, в который писать, расширение задаст его формат
  color1 := "0xFF0000"    // RGB (255,0,0)
  color2 := "0x00FF00"    // RGB (0,255,0)
  color3 := "0x0000FF"    // RGB (0,0,255)

  //////////////////////////////
  filtergraph := []string{
    "color=c=" + color1 + ":size=" + resulution + ":duration=3:s=qcif:r=" + framerate + " [v1]",
    "color=c=" + color2 + ":size=" + resulution + ":duration=3:s=qcif:r=" + framerate + " [v2]",
    "color=c=" + color3 + ":size=" + resulution + ":duration=3:s=qcif:r=" + framerate + " [v3]",
    `[v1] [v2] [v3] concat=n=3 [v]`,
  }

  allOptions := []string{
    "-filter_complex", strings.Join(filtergraph, ";"),
    "-map", "[v]",
    "-threads", "0", "-y", outfile,
  }

  RunCmd("ffmpeg", allOptions...)
}

func ReadAndPrint(r io.Reader) {
  io.Copy(os.Stdout, r)
}

func RunCmd(name string, args ...string) {
  cmd := exec.Command(name, args...)
  stdout, err := cmd.StdoutPipe()
  if err != nil {
    panic(err)
  }
  stderr, err := cmd.StderrPipe()
  if err != nil {
    panic(err)
  }

  go ReadAndPrint(stdout)
  go ReadAndPrint(stderr)

  err = cmd.Run()
  if err != nil {
    panic(err)
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question