gobot.io/x/gobot/v2@v2.1.0/platforms/parrot/bebop/client/examples/video.go (about)

     1  //go:build example
     2  // +build example
     3  
     4  //
     5  // Do not build by default.
     6  
     7  /*
     8  This example will connect to the Bebop and stream its video to a webpage
     9  via ffserver. This requires you to have both ffmpeg and ffserver installed
    10  on your computer.
    11  
    12  In order to run this example you will first need to  start ffserver with:
    13  
    14  	$ ffserver -f ff.conf
    15  
    16  then in a separate terminal run this program:
    17  
    18  	$ go run video.go
    19  
    20  You will then be able to view the video feed by navigation to
    21  http://localhost:8090/bebop.mjpeg in a web browser. *NOTE* firefox works
    22  best for viewing the video feed.
    23  */
    24  package main
    25  
    26  import (
    27  	"fmt"
    28  	"io/ioutil"
    29  	"os/exec"
    30  	"time"
    31  
    32  	"gobot.io/x/gobot/v2/platforms/parrot/bebop/client"
    33  )
    34  
    35  func main() {
    36  	bebop := client.New()
    37  
    38  	if err := bebop.Connect(); err != nil {
    39  		fmt.Println(err)
    40  		return
    41  	}
    42  
    43  	if err := bebop.VideoEnable(true); err != nil {
    44  		fmt.Println(err)
    45  		return
    46  	}
    47  
    48  	if err := bebop.VideoStreamMode(0); err != nil {
    49  		fmt.Println(err)
    50  		return
    51  	}
    52  
    53  	ffmpeg := exec.Command("ffmpeg", "-i", "pipe:0", "http://localhost:8090/bebop.ffm")
    54  
    55  	ffmpegErr, err := ffmpeg.StderrPipe()
    56  
    57  	if err != nil {
    58  		fmt.Println(err)
    59  		return
    60  	}
    61  
    62  	ffmpegIn, err := ffmpeg.StdinPipe()
    63  
    64  	if err != nil {
    65  		fmt.Println(err)
    66  		return
    67  	}
    68  
    69  	if err := ffmpeg.Start(); err != nil {
    70  		fmt.Println(err)
    71  		return
    72  	}
    73  
    74  	go func() {
    75  		for {
    76  			buf, err := ioutil.ReadAll(ffmpegErr)
    77  			if err != nil {
    78  				fmt.Println(err)
    79  			}
    80  			if len(buf) > 0 {
    81  				fmt.Println(string(buf))
    82  			}
    83  		}
    84  	}()
    85  
    86  	go func() {
    87  		for {
    88  			if _, err := ffmpegIn.Write(<-bebop.Video()); err != nil {
    89  				fmt.Println(err)
    90  			}
    91  		}
    92  	}()
    93  
    94  	time.Sleep(99 * time.Second)
    95  }