当前位置:主页 > 查看内容

ffmpeg-go 近期进展

发布时间:2021-04-19 00:00| 位朋友查看

简介:近期进展 在 ffmpeg-go init 之后,项目也收到了一些关注,还有几个同学发邮件探讨用法。不过时间有限,第一版本之后更新就比较少了,新增的功能不多,半年来新增的一些功能介绍如下: 支持 cpu 限制 ffmpeg-go 现在支持设置 cpu/memory limit 了 (只支持 li……

近期进展

ffmpeg-go init 之后,项目也收到了一些关注,还有几个同学发邮件探讨用法。不过时间有限,第一版本之后更新就比较少了,新增的功能不多,半年来新增的一些功能介绍如下:

支持 cpu 限制

ffmpeg-go 现在支持设置 cpu/memory limit 了 (只支持 linux 环境),这在非容器环境下,多进程的场景会比较有用,这个 命令设置 cpu 限制可以比较公平在在进程之间分配资源。使用例子如下:

func ComplexFilterExample(testInputFile, testOverlayFile, testOutputFile string) *ffmpeg.Stream {
	split := ffmpeg.Input(testInputFile).VFlip().Split()
	split0, split1 := split.Get("0"), split.Get("1")
	overlayFile := ffmpeg.Input(testOverlayFile).Crop(10, 10, 158, 112)
	return ffmpeg.Concat([]*ffmpeg.Stream{
		split0.Trim(ffmpeg.KwArgs{"start_frame": 10, "end_frame": 20}),
		split1.Trim(ffmpeg.KwArgs{"start_frame": 30, "end_frame": 40})}).
		Overlay(overlayFile.HFlip(), "").
		DrawBox(50, 50, 120, 120, "red", 5).
		Output(testOutputFile).
		OverWriteOutput()
}

// PID    USER       PR  NI    VIRT    RES    SHR S  %CPU   %MEM     TIME+ COMMAND
// 1386105 root      20   0 2114152 273780  31672 R  50.2   1.7      0:16.79 ffmpeg
func TestLimitCpu(t *testing.T) {
	e := ComplexFilterExample("./sample_data/in1.mp4", "./sample_data/overlay.png", "./sample_data/out2.mp4")
	err := e.WithCpuCoreRequest(0.1).WithCpuCoreLimit(0.5).RunLinux()
	if err != nil {
		assert.Nil(t, err)
	}
}

支持输出 s3 地址

ffmpeg-go 支持设置 s3 为输出地址了,这种模式对于 stream 方式的输出比较有用,可以在 ffmpeg 在处理的时候同时做上传动作,以节约整体的处理时间。对于兼容 s3 协议的如 腾讯云,阿里云,七牛云对象存储也可以使用这个功能(未来还会在相关对接云资源的使用上做一些功能),例子如下:

func TestSimpleS3StreamExample(t *testing.T) {
	err := ffmpeg.Input("./sample_data/in1.mp4", nil).
		Output("s3://data-1251825869/test_out.ts", ffmpeg.KwArgs{
			"aws_config": &aws.Config{
				Credentials: credentials.NewStaticCredentials("xx", "yyy", ""),
				//Endpoint:    aws.String("xx"),
				Region: aws.String("yyy"),
			},
			// outputS3 use stream output, so you can only use supported format
			// if you want mp4 format for example, you can output it to a file, and then call s3 sdk to do upload
			"format": "mpegts",
		}).
		Run()
	assert.Nil(t, err)
}

增加了更多例子

这点是收到几个相关咨询使用的邮件后发现,例子还是比较少,部分例子分散在不同地方,所以对一些简单的例子做了一些整理。

附第一版的介绍

使用例子

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()

抽帧

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

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")

结果

progress:  .0
progress:  0.72
progress:  1.00
progress:  done

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

see complete example at: opencv

结果:

支持生成 flowchart


本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:微服务架构之雪崩效应 下一篇:没有了

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐