I was wondering what's the best way to stream a video file (mpg4/avi - or any other format) in Go. Possibly, I'd like to be able to play it using a simple tag.
I've tried playing the famous Big Buck Bunny file with this code:
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
func serveHTTP(w http.ResponseWriter, r *http.Request) {
video, err := os.Open("./bunny.avi")
if err != nil {
log.Fatal(err)
}
http.ServeContent(w, r, "bunny.avi", time.Now(), video)
defer video.Close()
}
func main() {
http.HandleFunc("/", serveHTTP)
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println(err.Error())
}
}
But when loading the html page in my browser nothing plays and only one http request is actually triggered and only one response with 206 Partial content is sent to the page.
The html page contains the following code in the body:
<video width="320" height="240" controls autoplay>
<source src="http://localhost:8080">
Your browser does not support the video tag.
</video>
Thank you!