GoLang – 通过字节流遍历视频

huangapple go评论133阅读模式
英文:

GoLang - Seeking through a video (serving as bytes)

问题

我正在使用golang编写一个服务器,并成功实现了基本的.mp4文件服务。它通过字节流进行传输。问题是我无法在视频中进行定位/跳转。我尝试在Stack Overflow和Google上搜索答案,但没有找到解决方法。

以下是我的代码:

  1. package main
  2. import (
  3. "net/http"
  4. "io/ioutil"
  5. "fmt"
  6. "os"
  7. "log"
  8. "bytes"
  9. )
  10. func ServeHTTP(w http.ResponseWriter, r *http.Request) {
  11. // 读取生成的receipt.pdf文件并将其流式传输到浏览器
  12. streamPDFbytes, err := ioutil.ReadFile("./video.mp4")
  13. log.Println(r)
  14. if err != nil {
  15. fmt.Println(err)
  16. os.Exit(1)
  17. }
  18. b := bytes.NewBuffer(streamPDFbytes)
  19. // 直接流式传输到客户端(浏览器)
  20. w.Header().Set("Content-type", "video/mp4")
  21. if _, err := b.WriteTo(w); err != nil { // <----- 这里!
  22. fmt.Fprintf(w, "%s", err)
  23. }
  24. w.Write([]byte("Video Completed"))
  25. }
  26. func main() {
  27. http.Handle("/", new(MyHandler))
  28. http.ListenAndServe(":8080", nil)
  29. }

有人知道如何在golang中实现定位吗?

谢谢,祝您有愉快的一天!

英文:

I'm writing a server in golang and I got it to serve a basic .mp4 file. It serves it by bytes. The problem is that I can not seek/skip through the video. I've tried searching throughout stackover flow and google to find an answer but I came up short..

Here is my code:

  1. package main
  2. import (
  3. "net/http"
  4. "io/ioutil"
  5. "fmt"
  6. "os"
  7. "log"
  8. "bytes"
  9. )
  10. func ServeHTTP(w http.ResponseWriter, r *http.Request) {
  11. // grab the generated receipt.pdf file and stream it to browser
  12. streamPDFbytes, err := ioutil.ReadFile("./video.mp4")
  13. log.Println(r)
  14. if err != nil {
  15. fmt.Println(err)
  16. os.Exit(1)
  17. }
  18. b := bytes.NewBuffer(streamPDFbytes)
  19. // stream straight to client(browser)
  20. w.Header().Set("Content-type", "video/mp4")
  21. if _, err := b.WriteTo(w); err != nil { // <----- here!
  22. fmt.Fprintf(w, "%s", err)
  23. }
  24. w.Write([]byte("Video Completed"))
  25. }
  26. func main() {
  27. http.Handle("/", new(MyHandler))
  28. http.ListenAndServe(":8080", nil)
  29. }

Does anyone have the answer to how seeking works in golang?

Thanks,
Have a great day!

答案1

得分: 5

在Go语言中,实现支持定位的最简单的流式传输MP4视频的方法如下:

  1. package main
  2. import (
  3. "net/http"
  4. )
  5. func main() {
  6. fs := http.FileServer(http.Dir("."))
  7. http.Handle("/", http.StripPrefix("/", fs))
  8. http.ListenAndServe(":8080", nil)
  9. }

视频将在 http://localhost:8080/video.mp4 上可用。

更复杂的方法如下:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "time"
  7. )
  8. func ServeHTTP(w http.ResponseWriter, r *http.Request) {
  9. video, err := os.Open("./video.mp4")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. defer video.Close()
  14. http.ServeContent(w, r, "video.mp4", time.Now(), video)
  15. }
  16. func main() {
  17. http.HandleFunc("/", ServeHTTP)
  18. http.ListenAndServe(":8080", nil)
  19. }

如果你需要更灵活的方法,你可以实现自己的渐进式流式传输服务器。

在你的代码中,你忘记添加和处理 Range/Accept-Range 头部,这就是为什么 FF 和 Chrome 都没有显示进度条。但是,我认为将整个MP4文件保存在内存中并不是一个好主意。

英文:

The simplest way to stream MP4 video on Go with seeking support is

  1. package main
  2. import (
  3. "net/http"
  4. )
  5. func main() {
  6. fs := http.FileServer(http.Dir("."))
  7. http.Handle("/", http.StripPrefix("/", fs))
  8. http.ListenAndServe(":8080", nil)
  9. }

And video will be available at http://localhost:8080/video.mp4

More complex is

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "time"
  7. )
  8. func ServeHTTP(w http.ResponseWriter, r *http.Request) {
  9. video, err := os.Open("./video.mp4")
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. defer video.Close()
  14. http.ServeContent(w, r, "video.mp4", time.Now(), video)
  15. }
  16. func main() {
  17. http.HandleFunc("/", ServeHTTP)
  18. http.ListenAndServe(":8080", nil)
  19. }

If you need something more flexible you should implement your own progressive streaming server.

In your code you forgot to add and process Range/Accept-Range headers that's why nor FF, nor Chrome doesn't show you seek bar but anyway I don't think that keeping whole MP4 file in memory is good idea.

huangapple
  • 本文由 发表于 2016年2月27日 16:29:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/35667501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定