Home > OS >  Convert .mp4 to gif using ffmpeg in golang
Convert .mp4 to gif using ffmpeg in golang

Time:01-07

I want to convert my mp4 file to gif format. I was used the command that is working in command prompt. i.e., converting my .mp4 into gif but in go lang it is not done anything. Here is my command:

ffmpeg -i Untitled.mp4 -vf "fps=5,scale=320:-1:flags=lanczos" -c:v pam -f image2pipe - | convert -delay 5 - -loop 0 -layers optimize test.gif

I was used in go lang like this but it is not working. please any one help me to solve my problem.

    cmd3 := exec.Command("ffmpeg", "-i", "Untitled.mp4", "-vf", "`fps=5,scale=320:-1:flags=lanczos`", "-c:v", "pam", "-f", "image2pipe", "- |", "convert", "-delay", "5", "-", "-loop", "0", "-layers", "optimize", "test.gif")

stdout2, err2 := cmd3.StdoutPipe()
log.Println("gif", stdout2)

if err2 != nil {
    log.Fatal(err2, "............")
}
if err2 := cmd3.Start(); err2 != nil {
    log.Fatal(err2)
}

if any changes is there please inform me thanks in advance.

CodePudding user response:

This is not exactly what you are looking for, but it is possible to do it like this:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := "ffmpeg -i Untitled.mp4 -vf \"fps=5,scale=320:-1:flags=lanczos\" -c:v pam -f image2pipe - | convert -delay 5 - -loop 0 -layers optimize test.gif"
    _, err := exec.Command("bash","-c",cmd).Output()
    if err != nil {
        fmt.Println(fmt.Sprintf("Failed to execute command: %s", cmd))
    }
}
  •  Tags:  
  • Related