Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Video GoLang-通过视频搜索(用作字节)_Video_Go_Streaming_Skip_Seek - Fatal编程技术网

Video GoLang-通过视频搜索(用作字节)

Video GoLang-通过视频搜索(用作字节),video,go,streaming,skip,seek,Video,Go,Streaming,Skip,Seek,我正在用golang编写一个服务器,我用它来提供一个基本的.mp4文件。它以字节为单位提供服务。问题是我无法搜索/跳过视频。我试着在stackover flow和google中搜索,以找到答案,但我没有找到答案 这是我的密码: package main import ( "net/http" "io/ioutil" "fmt" "os" "log" "bytes" ) func ServeHTTP(w http.ResponseWriter,

我正在用golang编写一个服务器,我用它来提供一个基本的.mp4文件。它以字节为单位提供服务。问题是我无法搜索/跳过视频。我试着在stackover flow和google中搜索,以找到答案,但我没有找到答案

这是我的密码:

package main

import (
    "net/http"
    "io/ioutil"
    "fmt"
    "os"
    "log"
    "bytes"
)

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
           // grab the generated receipt.pdf file and stream it to browser
           streamPDFbytes, err := ioutil.ReadFile("./video.mp4")
           log.Println(r)
           if err != nil {
                   fmt.Println(err)
                   os.Exit(1)
           }

           b := bytes.NewBuffer(streamPDFbytes)

           // stream straight to client(browser)
           w.Header().Set("Content-type", "video/mp4")

           if _, err := b.WriteTo(w); err != nil { // <----- here!
                   fmt.Fprintf(w, "%s", err)
           }

           w.Write([]byte("Video Completed"))
}

func main() {
    http.Handle("/", new(MyHandler))
    http.ListenAndServe(":8080", nil)
}
主程序包
进口(
“net/http”
“io/ioutil”
“fmt”
“操作系统”
“日志”
“字节”
)
func ServeHTTP(w http.ResponseWriter,r*http.Request){
//抓取生成的receive.pdf文件并将其流式传输到浏览器
streamPDFbytes,err:=ioutil.ReadFile(“./video.mp4”)
log.Println(r)
如果错误!=零{
fmt.Println(错误)
操作系统退出(1)
}
b:=字节。新缓冲区(streamPDFbytes)
//直接流到客户端(浏览器)
w、 Header().Set(“内容类型”、“视频/mp4”)

如果,err:=b.WriteTo(w); err!=nil{/在寻求支持的过程中,最简单的MP4视频流方式是

package main

import (
    "net/http"
)

func main() {
    fs := http.FileServer(http.Dir("."))
    http.Handle("/", http.StripPrefix("/", fs))
    http.ListenAndServe(":8080", nil)
}
视频将在

更复杂的是

package main

import (
    "log"
    "net/http"
    "os"
    "time"
)

func ServeHTTP(w http.ResponseWriter, r *http.Request) {
    video, err := os.Open("./video.mp4")
    if err != nil {
        log.Fatal(err)
    }
    defer video.Close()

    http.ServeContent(w, r, "video.mp4", time.Now(), video)
}

func main() {
    http.HandleFunc("/", ServeHTTP)
    http.ListenAndServe(":8080", nil)
}
如果您需要更灵活的方式,您应该实现自己的服务器

在您的代码中,您忘记添加和处理
范围
/
接受范围
标题,这就是为什么nor FF和nor Chrome不显示搜索栏,但无论如何,我不认为将整个MP4文件保存在内存中是个好主意