gpt4 book ai didi

video - GoLang - 通过视频搜索(作为字节)

转载 作者:IT王子 更新时间:2023-10-29 01:27:05 24 4
gpt4 key购买 nike

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

这是我的代码:

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)
}

有没有人知道在 golang 中搜索是如何工作的?

谢谢,祝你有美好的一天!

最佳答案

通过寻求支持在 Go 上流式传输 MP4 视频的最简单方法是

package main

import (
"net/http"
)

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

视频将在 http://localhost:8080/video.mp4 提供

更复杂的是

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)
}

如果您需要更灵活的东西,您应该实现自己的 progressive streaming服务器。

在您的代码中,您忘记添加和处理 Range/Accept-Range header ,这就是为什么 FF 和 Chrome 都不显示您的搜索栏,但无论如何我不'认为将整个 MP4 文件保存在内存中是个好主意。

关于video - GoLang - 通过视频搜索(作为字节),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35667501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com