The Go language call context pulls up the ffmpeg and introduces a simple streaming media server made by Livego

Source: Internet
Author: User

Tags: share picture one std using interface erro duration sed elf

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: