近期进展
在 https://github.com/u2takey/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
来源: https://www.qcloud.com/developer/article/1814657