Skip to content

Commit 0782179

Browse files
author
wanglei.w
committed
add more example doc
1 parent 5029339 commit 0782179

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

README.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ffmpeg-go is golang port of https://github.com/kkroening/ffmpeg-python
55
check ffmpeg_test.go for examples.
66

77

8-
# example
8+
# Examples
99

1010
```go
1111
split := Input(TestInputFile1).VFlip().Split()
@@ -21,7 +21,77 @@ err := Concat([]*Stream{
2121
Run()
2222
```
2323

24-
# view
24+
## Task Frame From Video
25+
26+
```bash
27+
func ExampleReadFrameAsJpeg(inFileName string, frameNum int) io.Reader {
28+
buf := bytes.NewBuffer(nil)
29+
err := ffmpeg.Input(inFileName).
30+
Filter("select", ffmpeg.Args{fmt.Sprintf("gte(n,%d)", frameNum)}).
31+
Output("pipe:", ffmpeg.KwArgs{"vframes": 1, "format": "image2", "vcodec": "mjpeg"}).
32+
WithOutput(buf, os.Stdout).
33+
Run()
34+
if err != nil {
35+
panic(err)
36+
}
37+
return buf
38+
}
39+
40+
reader := ExampleReadFrameAsJpeg("./sample_data/in1.mp4", 5)
41+
img, err := imaging.Decode(reader)
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
err = imaging.Save(img, "./sample_data/out1.jpeg")
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
```
50+
result :
51+
52+
![image](./examples/sample_data/out1.jpeg)
53+
54+
55+
## Show Ffmpeg Progress
56+
57+
see complete example at: [showProgress](./examples/showProgress.go)
58+
59+
```bash
60+
func ExampleShowProgress(inFileName, outFileName string) {
61+
a, err := ffmpeg.Probe(inFileName)
62+
if err != nil {
63+
panic(err)
64+
}
65+
totalDuration := gjson.Get(a, "format.duration").Float()
66+
67+
err = ffmpeg.Input(inFileName).
68+
Output(outFileName, ffmpeg.KwArgs{"c:v": "libx264", "preset": "veryslow"}).
69+
GlobalArgs("-progress", "unix://"+TempSock(totalDuration)).
70+
OverWriteOutput().
71+
Run()
72+
if err != nil {
73+
panic(err)
74+
}
75+
}
76+
ExampleShowProgress("./sample_data/in1.mp4", "./sample_data/out2.mp4")
77+
```
78+
79+
result
80+
81+
```bash
82+
progress: .0
83+
progress: 0.72
84+
progress: 1.00
85+
progress: done
86+
```
87+
88+
## Use Ffmpeg-go With Open-cv (gocv) For Face-detect
89+
90+
see complete example at: [opencv](./examples/opencv.go)
91+
92+
result: ![image](./examples/sample_data/face-detect.jpg)
93+
94+
# View Graph
2595
2696
function view generate [mermaid](https://mermaid-js.github.io/mermaid/#/) chart, which can be use in markdown or view [online](https://mermaid-js.github.io/mermaid-live-editor/)
2797

examples/opencv.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,25 @@ func openCvProcess(xmlFile string, reader io.ReadCloser, w, h int) {
6262
if img.Empty() {
6363
continue
6464
}
65+
img2 := gocv.NewMat()
66+
gocv.CvtColor(img, &img2, gocv.ColorBGRToRGB)
6567

6668
// detect faces
67-
rects := classifier.DetectMultiScale(img)
69+
rects := classifier.DetectMultiScale(img2)
6870
fmt.Printf("found %d faces\n", len(rects))
6971

7072
// draw a rectangle around each face on the original image,
7173
// along with text identifing as "Human"
7274
for _, r := range rects {
73-
gocv.Rectangle(&img, r, blue, 3)
75+
gocv.Rectangle(&img2, r, blue, 3)
7476

7577
size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2)
7678
pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2)
77-
gocv.PutText(&img, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
79+
gocv.PutText(&img2, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2)
7880
}
7981

8082
// show the image in the window, and wait 1 millisecond
81-
window.IMShow(img)
83+
window.IMShow(img2)
8284
if window.WaitKey(10) >= 0 {
8385
break
8486
}
45.5 KB
Loading

0 commit comments

Comments
 (0)