diff --git a/README.md b/README.md index 18427e8..d84ec65 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ffmpeg-go is golang port of https://github.com/kkroening/ffmpeg-python check ffmpeg_test.go for examples. -# example +# Examples ```go split := Input(TestInputFile1).VFlip().Split() @@ -21,7 +21,77 @@ err := Concat([]*Stream{ Run() ``` -# view +## Task Frame From Video + +```bash +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](./examples/sample_data/out1.jpeg) + + +## Show Ffmpeg Progress + +see complete example at: [showProgress](./examples/showProgress.go) + +```bash +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 + +```bash +progress: .0 +progress: 0.72 +progress: 1.00 +progress: done +``` + +## Use Ffmpeg-go With Open-cv (gocv) For Face-detect + +see complete example at: [opencv](./examples/opencv.go) + +result: ![image](./examples/sample_data/face-detect.jpg) + +# View Graph 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/) diff --git a/examples/opencv.go b/examples/opencv.go index 928ac32..646be2b 100644 --- a/examples/opencv.go +++ b/examples/opencv.go @@ -62,23 +62,25 @@ func openCvProcess(xmlFile string, reader io.ReadCloser, w, h int) { if img.Empty() { continue } + img2 := gocv.NewMat() + gocv.CvtColor(img, &img2, gocv.ColorBGRToRGB) // detect faces - rects := classifier.DetectMultiScale(img) + rects := classifier.DetectMultiScale(img2) fmt.Printf("found %d faces\n", len(rects)) // draw a rectangle around each face on the original image, // along with text identifing as "Human" for _, r := range rects { - gocv.Rectangle(&img, r, blue, 3) + gocv.Rectangle(&img2, r, blue, 3) size := gocv.GetTextSize("Human", gocv.FontHersheyPlain, 1.2, 2) pt := image.Pt(r.Min.X+(r.Min.X/2)-(size.X/2), r.Min.Y-2) - gocv.PutText(&img, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2) + gocv.PutText(&img2, "Human", pt, gocv.FontHersheyPlain, 1.2, blue, 2) } // show the image in the window, and wait 1 millisecond - window.IMShow(img) + window.IMShow(img2) if window.WaitKey(10) >= 0 { break } diff --git a/examples/sample_data/face-detect.jpg b/examples/sample_data/face-detect.jpg new file mode 100644 index 0000000..e496691 Binary files /dev/null and b/examples/sample_data/face-detect.jpg differ