• Stars
    star
    1,713
  • Rank 27,245 (Top 0.6 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

golang binding for ffmpeg

ffmpeg-go

ffmpeg-go is golang port of https://github.com/kkroening/ffmpeg-python

check examples/example_test.go and ffmpeg_test.go for more examples.

Examples

split := Input(TestInputFile1).VFlip().Split()
	split0, split1 := split.Get("0"), split.Get("1")
	overlayFile := Input(TestOverlayFile).Crop(10, 10, 158, 112)
err := 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()

Transcoding From One Codec To Another

err := ffmpeg.Input("./sample_data/in1.mp4").
		Output("./sample_data/out1.mp4", ffmpeg.KwArgs{"c:v": "libx265"}).
		OverWriteOutput().ErrorToStdOut().Run()

Cut Video From Timestamp

err := ffmpeg.Input("./sample_data/in1.mp4", ffmpeg.KwArgs{"ss": 1}).
    Output("./sample_data/out1.mp4", ffmpeg.KwArgs{"t": 1}).OverWriteOutput().Run()
assert.Nil(t, err)

Add Watermark For Video

// show watermark with size 64:-1 in the top left corner after seconds 1
overlay := ffmpeg.Input("./sample_data/overlay.png").Filter("scale", ffmpeg.Args{"64:-1"})
err := ffmpeg.Filter(
    []*ffmpeg.Stream{
        ffmpeg.Input("./sample_data/in1.mp4"),
        overlay,
    }, "overlay", ffmpeg.Args{"10:10"}, ffmpeg.KwArgs{"enable": "gte(t,1)"}).
    Output("./sample_data/out1.mp4").OverWriteOutput().ErrorToStdOut().Run()

result:

img.png

Cut Video For Gif

err := ffmpeg.Input("./sample_data/in1.mp4", ffmpeg.KwArgs{"ss": "1"}).
    Output("./sample_data/out1.gif", ffmpeg.KwArgs{"s": "320x240", "pix_fmt": "rgb24", "t": "3", "r": "3"}).
    OverWriteOutput().ErrorToStdOut().Run()

result:

img.png

Task Frame From Video

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

result :

image

Get Multiple Output

// get multiple output with different size/bitrate
input := ffmpeg.Input("./sample_data/in1.mp4").Split()
out1 := input.Get("0").Filter("scale", ffmpeg.Args{"1920:-1"}).
Output("./sample_data/1920.mp4", ffmpeg.KwArgs{"b:v": "5000k"})
out2 := input.Get("1").Filter("scale", ffmpeg.Args{"1280:-1"}).
Output("./sample_data/1280.mp4", ffmpeg.KwArgs{"b:v": "2800k"})

err := ffmpeg.MergeOutputs(out1, out2).OverWriteOutput().ErrorToStdOut().Run()

Show FFmpeg Progress

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

result

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

Integrate FFmpeg-go With Open-CV (gocv) For Face-detect

see complete example at: opencv

result: image

Set Cpu limit/request For FFmpeg-go

e := ComplexFilterExample("./sample_data/in1.mp4", "./sample_data/overlay.png", "./sample_data/out2.mp4")
err := e.RunWithResource(0.1, 0.5)
if err != nil {
    assert.Nil(t, err)
}

result from command top: we will see ffmpeg used 0.5 core as expected.

> top 
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

View Progress Graph

function view generate mermaid chart, which can be use in markdown or view online

split := Input(TestInputFile1).VFlip().Split()
	split0, split1 := split.Get("0"), split.Get("1")
	overlayFile := Input(TestOverlayFile).Crop(10, 10, 158, 112)
b, err := 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().View(ViewTypeFlowChart)
fmt.Println(b)

image

More Repositories

1

ymir

Go
44
star
2

go-annotation

Annotation libraries and tools for golang.
Go
40
star
3

awesome_chatgpt_ch

chatgpt 项目、中文站点、资源收集
26
star
4

chatgpt-go

golang version chatgpt api sdk and service
Go
20
star
5

kubectl-clusters

Python
19
star
6

learn-yacc

learn go-yacc
Go
17
star
7

tensorflow-in-depth

tensorflow in depth
14
star
8

malcolm

Continuous Delivery platform running on kubernetes.
Go
6
star
9

k8s-scheduler-extender-example

Go
6
star
10

notebook-hub

collection of jupyter notebooks
TypeScript
6
star
11

ss-ambassador

容器翻墙方案
Shell
6
star
12

jupyter-lab-serverless

Run and Manager ServerLess Functions in JupyterLab
Python
6
star
13

wiki

query wiki pages generated by gpt and `tldr`
Go
5
star
14

imageEncryptServer

图片加密解密app,包括一个ios app,h5 页面,和后台代码,这部分是后台和h5代码
Python
5
star
15

registry-bench

registry bench test
Go
4
star
16

starlark-go-lib

Go
4
star
17

SimulateKey

一个iosapp用于模拟游戏手柄
Objective-C
4
star
18

cgo-bench

Go
4
star
19

ansible-k8s

demo for ansible for k8s https://cloud.tencent.com/developer/article/1564975
Dockerfile
4
star
20

gopher-lua-lib

Go
3
star
21

go-utils

go utils from everywhere in one place
Go
3
star
22

console-stock

PHP
3
star
23

example_reedsolomon

Go
3
star
24

gotf

map golang library functions to template
Go
3
star
25

go-cache

3 kind of cache with golang for different scenario
Go
2
star
26

docker-cloc

Shell
2
star
27

jupyterlab-docker

Dockerfile
2
star
28

imageEncrypt

图片加密解密app,包括一个ios app,h5 页面,和后台代码,这部分是ios app代码
Objective-C
2
star
29

docker-file

save/recover your file with docker
Shell
2
star
30

junit2html

junit2html
Go
1
star
31

sample_hello_tencent

Go
1
star
32

gopyter

Go
1
star
33

phpadmin_tunnel

1
star
34

runc-mysql

Go
1
star
35

u2takey

1
star
36

docker-cosfs

docker image for cosfs
Shell
1
star
37

rudo

1
star
38

gptwiki-pages

1
star
39

tencentcloud-thanos-querier

Go
1
star
40

trace-example

Go
1
star
41

intellij-go-converter

Kotlin
1
star
42

mq-gateway

Go
1
star
43

darwin_amd64

1
star
44

u2takey.github.io

HTML
1
star
45

kirk_helloworld

1
star
46

ascii_clock

ascii clock for fun
Python
1
star
47

extend_k8s_example

Go
1
star
48

photoSpider

1
star
49

osdb

1
star
50

forBlog

Jupyter Notebook
1
star
51

nsq-gateway

1
star
52

kci-sdk-go

Go
1
star
53

k8sOnk8s

1
star
54

registry_k8s

1
star
55

clipboard-os

Go
1
star
56

mongoexpress_tunnel

mongoexpress with tunnel for debug inner-network mongo service
1
star