ffmpeg 命令的 golang binding 工具

2020-11-20 17:38:34 浏览数 (1)

项目地址:

https://github.com/u2takey/ffmpeg-go

使用例子

代码语言:txt复制
import (
	ffmpeg "github.com/u2takey/ffmpeg-go"
)

split := ffmpeg.Input(TestInputFile1).VFlip().Split()
	split0, split1 := split.Get("0"), split.Get("1")
	overlayFile := Input(TestOverlayFile).Crop(10, 10, 158, 112)
err := ffmpeg.Concat([]*Stream{
    split0.Trim(KwArgs{"start_frame": 10, "end_frame": 20}),
    split1.Trim(KwArgs{"start_frame": 30, "end_frame": 40})}).
        Overlay(overlayFile.HFlip(), "").
        DrawBox(50, 50, 120, 120, "red", 5).
        Output(TestOutputFile1).
        OverWriteOutput().Run()

抽帧

代码语言:txt复制
func ExampleReadFrameAsJpeg(inFileName string, frameNum int) io.Reader {
	buf := bytes.NewBuffer(nil)
	err := ffmpeg.Input(inFileName).
		Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", frameNum)}).
		Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
		WithOutput(buf, os.Stdout).
		Run()
	if err != nil {
		panic(err)
	}
	return buf
}

reader := ExampleReadFrameAsJpeg("./sample_data/in1.mp4", 5)
img, err := imaging.Decode(reader)
if err != nil {
    t.Fatal(err)
}
err = imaging.Save(img, "./sample_data/out1.jpeg")
if err != nil {
    t.Fatal(err)
}

结果 :

显示处理进度

see complete example at: showProgress

代码语言:txt复制
func ExampleShowProgress(inFileName, outFileName string) {
	a, err := ffmpeg.Probe(inFileName)
	if err != nil {
		panic(err)
	}
	totalDuration := gjson.Get(a, "format.duration").Float()

	err = ffmpeg.Input(inFileName).
		Output(outFileName, ffmpeg.KwArgs{"c:v": "libx264", "preset": "veryslow"}).
		GlobalArgs("-progress", "unix://" TempSock(totalDuration)).
		OverWriteOutput().
		Run()
	if err != nil {
		panic(err)
	}
}
ExampleShowProgress("./sample_data/in1.mp4", "./sample_data/out2.mp4")

结果

代码语言:txt复制
progress:  .0
progress:  0.72
progress:  1.00
progress:  done

和 Open-cv (gocv) 一起使用做视频人脸识别

see complete example at: opencv

结果:

支持生成 flowchart

0 人点赞