Want to push a set of video playback scheme for the project team, used to play the real-time stream of ip camera , many people have already done on the net, my side is using Golang, pull up the context call FFmpeg command, get RTSP go to Livego service, Then the interface found me how to get the stream to play.
The implementation is as follows:
The part that invokes the command line is as follows
Package Ffmpegimport ("bytes" "Context" "OS" "os/exec" "Syscall" " Time") type C struct {NamestringCTX Context. Context Cancel context. Cancelfunc cmd*exec. Cmd}func New (Namestring, args []string) C {var c c c.name=name C.ctx, C.cancel=context. Withcancel (context. Background ()) C.cmd= Exec.commandcontext (C.ctx,"FFmpeg", args ...) C.cmd.sysprocattr= &syscall. Sysprocattr{hidewindow:true} c.cmd.stdout=OS. Stdout return C}func (c*c) Run () error {return C.cmd.start ()}func (c*c) runthenclose (Ch chanint) Error {err:=C.cmd.start () c.cancel ( )//Tell go I take itreturn Err}func (c*C) Close () {c.cancel ()}//Run and wait for backFunc runandclose (args []string, callback Func (c, t Time. Duration)) Error {cmd:= Exec.command ("FFmpeg", args ...) ifcallback = =Nil {var cmderr bytes. Buffer cmd. Stderr= &CmderrifERR: = cmd. Run (); Err! =Nil {return extracterror (err, cmderr.string ())} return nil}//ffmpeg stdout is stderrstderr, err: =cmd. Stderrpipe ()ifErr! =Nil {return err}W, Err: =Newwatcher (stderr, callback)ifErr! =Nil {return err} deferW. Close () defer cmd. Process.wait ()ifERR: = cmd. Run (); Err! =Nil {return err} return nil}
View Code
The Go language call context pulls up the ffmpeg and introduces a simple streaming media server made by Livego