Go combat--golang Implement MP4 video File server (NAREIX/JOY4)

Source: Internet
Author: User
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

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.