在 ffmpeg-go init 之后,项目也收到了一些关注,还有几个同学发邮件探讨用法。不过时间有限,第一版本之后更新就比较少了,新增的功能不多,半年来新增的一些功能介绍如下:
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) } }
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
see complete example at: opencv
结果:
又到一年 315,面对大数据时代的消费,在给我们的日常生活带来不少便利,但同时...
随着企业数字化转型进程推进,各行业企业在数字基础设施一体化云平台、研运数字...
前言 本篇是SLS新版告警系列宣传与培训的第二篇 后续我们会推出20 系列直播与实...
描述 有一棵n个节点的树,节点编号是0至n?1,其中0号节点是根节点,i号节点的父...
我们知道,有了合适的技术,我们可以做得比仅仅跟上更新要好得多,并且如果我们...
一、Lambda架构需求 Lambda架构背后的需求是由于MR架构的延迟问题。MR虽然实现了...
显然,数据现在是我们所有生活的核心,在过去十年中,智能手机的蓬勃 发展推动了...
随着云计算快速发展 传统的商业数据库已经难以满足和响应快速变化 持续增长的业...
背景: 腾讯云CLB(负载均衡)与CLS(日志服务)集成 。然后看日志服务CLS专栏有...
十多年来,大数据一直在稳步增长。新兴的数据驱动公司已经崛起,并成为价值数十...