通过Golang exec运行FFMPEG命令

时间:2016-09-22 15:56:45

标签: go ffmpeg

我需要运行ffmpeg命令从图像中创建视频,并将图像之间的交叉淡入淡出作为过渡。该命令源自此post。我需要通过Golang os/exec包运行它。我需要运行的命令是:

ffmpeg -loop 1 -t 5 -i img-0.png -loop 1 -t 5 -i img-1.png -loop 1 -t 5 -i img-2.png -filter_complex "[1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v];[2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v];[0:v][b1v][1:v][b2v][2:v]concat=n=5:v=1:a=0,format=yuv420p[v]" -map '[v]' -c:v libx264 -pix_fmt yuv420p -r 30 -s 1280x720 -aspect 16:9 -crf 1 -preset ultrafast output.mp4

如果直接在终端中运行此命令,它可以正常工作。但是,它不适用于我的代码。这是我的代码,它接受一个字符串命令并通过os/exec包运行它:

command := "ffmpeg -loop 1 -t 5 -i img-0.png -loop 1 -t 5 -i img-1.png -loop 1 -t 5 -i img-2.png -filter_complex "[1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v];[2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v];[0:v][b1v][1:v][b2v][2:v]concat=n=5:v=1:a=0,format=yuv420p[v]" -map '[v]' -c:v libx264 -pix_fmt yuv420p -r 30 -s 1280x720 -aspect 16:9 -crf 1 -preset ultrafast output.mp4"

lastQuote := rune(0)
f := func(c rune) bool {
    switch {
    case c == lastQuote:
        lastQuote = rune(0)
        return false
    case lastQuote != rune(0):
        return false
    case unicode.In(c, unicode.Quotation_Mark):
        lastQuote = c
        return false
    default:
        return unicode.IsSpace(c)
    }
}
parts := strings.FieldsFunc(command, f)

cmd := exec.Command(parts[0], parts[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

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

当我运行此操作时,我收到ffmpeg错误:没有此类过滤器:'"',配置过滤器时出错。我知道它与视频过滤器中的引号有关,但我已经尝试了一切来使其工作,我无法弄明白。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:4)

这实际上可以正常工作:

exec.Command("ffmpeg", "-loop", "1", "-t", "5", "-i", "img-0.png", "-loop",  "1", "-t", "5", "-i", "img-1.png", "-loop", "1", "-t", "5", "-i", "img-2.png", "-filter_complex", "[1:v][0:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b1v];[2:v][1:v]blend=all_expr='A*(if(gte(T,0.5),1,T/0.5))+B*(1-(if(gte(T,0.5),1,T/0.5)))'[b2v];[0:v][b1v][1:v][b2v][2:v]concat=n=5:v=1:a=0,format=yuv420p[v]", "-map", "[v]", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-r", "30", "-s", "1280x720", "-aspect", "16:9", "-crf", "1", "-preset", "ultrafast", "output.mp4")

请注意,我确实从-filter_complex参数和-map参数中的两个单引号中删除了开始和结束双引号。

但是,手动完成,不确定strings功能是否可以自动