douguaidian8021 2017-12-09 00:31 采纳率: 100%
浏览 437
已采纳

如何为html5视频标签流式传输视频文件

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. Response

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!

  • 写回答

2条回答 默认 最新

  • duanduan8439 2017-12-09 02:14
    关注

    Your go code looks fine, leading me to think this is probably a problem with your video. avi is usually not supported for html5, see here for more details on containers/codecs for html5.

    I would try with a known working video. eg: https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4

    Maybe even simplify your code and just use http.ServeFile, although the important part of video serving (range requests) is in ServeContent anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示