This is a creation in Article, where the information may have evolved or changed.
Life goes on and on go Go go!!!
A little busy, a little slack, continue.
About the static file server implemented by Golang has been written before:
Go combat –golang Implement static file server (file view, file upload, file download)
Right, recently in the video aspect of things, then a simple, to implement a MP4 video file to provide a server bar, and through the browser to access playback.
MP4 File Server
package Mainimport ( "log" "Net/http" /span> "OS" "time" ) Func servehttp (w http. Responsewriter, R *http. Request) {video, err: = OS. Open () if err! = nil {log. Fatal (Err)} defer video. Close () http. Servecontent (W, R, "Test.mp4" , time. Now (), video)}func Main () {http. Handlefunc ( "/" , Servehttp) http. Listenandserve ( ": 8080" , nil )}
PackageMainImport "Io/ioutil"Import "Log"Import "Net/http"Import "Strings"typeViewhandlerstruct{}func(VH *viewhandler) Servehttp (w http. Responsewriter, R *http. Request) {path: = R.url. Path[1:] data, err: = Ioutil. ReadFile (string(path))ifErr! =Nil{log. Printf ("Error with path%s:%v", path, err) W.writeheader(404) W.write ([]byte("404")) }ifStrings. Hassuffix (Path,". html") {W.header (). ADD ("Content-type","Text/html") }Else ifStrings. Hassuffix (Path,". mp4") {W.header (). ADD ("Content-type","Video/mp4")} w.write (data)}funcMain () {http. Handle ("/",New(Viewhandler)) http. Listenandserve (": 8080",Nil)}
Nareix/joy4
Golang Audio/video Library and streaming server
Let's start with a little bit of simple use:
PackageMainImport("FMT" "Github.com/nareix/joy4/av" "Github.com/nareix/joy4/av/avutil" "Github.com/nareix/joy4/format")funcInit () {format. RegisterAll ()}funcMain () {file, _: = Avutil. Open ("Test.mp4") streams, _: = file. Streams () for_, Stream: =Rangestreams {ifStream. Type (). Isaudio () {astream: = stream. ( Av. Audiocodecdata) fmt. Println (Astream. Type (), Astream. Samplerate (), Astream. Sampleformat (), Astream. Channellayout ())}Else ifStream. Type (). Isvideo () {vstream: = stream. ( Av. Videocodecdata) fmt. Println (Vstream. Type (), Vstream. Width (), Vstream. Height ())}} file. Close ()}
Output:
AAC 48000 FLTP 1ch
H264 960 720