Search code examples
gogo-gin

serve video with go gin


I am writing a api server for upload and serve video and image by go-gin framework. I have uploaded my video to another host, and it worked

router := gin.Default()

    //config := cors.DefaultConfig()
    //config.AllowAllOrigins = true

    routerConfig := cors.Config{
        AllowAllOrigins:  true,
        AllowMethods:     []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
        AllowHeaders:     []string{"X-Requested-With", "Authorization", "Origin", "Content-Length", "Content-Type"},
        AllowCredentials: false,
        MaxAge:           12 * time.Hour,
    }

    router.Use(cors.New(routerConfig))

    router.StaticFS("/public", http.Dir("static"))

    err := router.Run(":5000")
    if err != nil {
        panic(err)
    }

When I try to access http://localhost:5000/public/{image_url}.png by chromium. It loads to browser. But when I access http://localhost:5000/public/{video_url}.mp4 by chromium, it can't load anything from browser (It can't receive)

Can someone explain for me what I am doing wrong?

Update:

Maybe I missed this content:

enter image description here

enter image description here

Here are my 2 http packages when I call a GET request


Solution

  • There are 2 possible explanation for your problem.

    1. It's not a problem because your browser doesn't have any feature to run video.
    2. (This is more possible explanation) You don't set the correct header for streaming video so the browser can't recognize or don't know what to do with that URL.

    For example I get from this video, when you check the Response Header, it should show

    content-length: 1570024
    content-range: bytes 0-1570023/1570024
    content-type: video/mp4
    

    My suggestion is use http.ServeFile which supports serving Range requests. See related questions, GoLang http webserver provide video (mp4) and How to serve http partial content with Go?.

    Updated

    After checking Gin StaticFS Godoc, I don't think it support Range header.

    After Test

    So I have try your code and I'm wrong, Gin StaticFS can serve content range like video.

    Here my screenshot, have tested it using Chromium and it works well:

    load test.mp4

    And my conclusions: There is a problem with your Chromium or your codecs or your video codecs not supported on your OS (I don't know what is your OS), check this MP4 not playing on Chrome version 27.0:

    due to Chrome removing support for h264, on some machines, mp4 videos encoded with it will either not work (throwing an Parser error when viewing under Firebug/Network tab - consistent with issue submitted here), or crash the browser, depending upon the encoding settings

    it isn't consistent - it entirely depends upon the codecs installed on the computer - while I didn't encounter this issue on my machine, we did have one in the office where the issue occurred (and thus we used this one for testing)

    it might to do with Quicktime / divX settings (the machine in question had an older version of Quicktime than my native one - we didn't want to loose our testing pc though, so we didn't update it).