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

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