hls.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package v1
  2. import (
  3. b64 "encoding/base64"
  4. "fmt"
  5. "github.com/ChineseSubFinder/ChineseSubFinder/pkg"
  6. "github.com/ChineseSubFinder/ChineseSubFinder/pkg/decode"
  7. backend2 "github.com/ChineseSubFinder/ChineseSubFinder/pkg/types/backend"
  8. "github.com/gin-gonic/gin"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. )
  13. // HlsPlaylist 获取 m3u8 列表
  14. func (cb *ControllerBase) HlsPlaylist(c *gin.Context) {
  15. var err error
  16. defer func() {
  17. // 统一的异常处理
  18. cb.ErrorProcess(c, "HlsPlaylist", err)
  19. }()
  20. videoFPathBase64 := c.Param("videofpathbase64")
  21. // base64 解码
  22. videoFPathUrlEncodeStr, err := b64.StdEncoding.DecodeString(videoFPathBase64)
  23. if err != nil {
  24. return
  25. }
  26. // url 解码
  27. videoFPath, err := url.QueryUnescape(string(videoFPathUrlEncodeStr))
  28. if err != nil {
  29. return
  30. }
  31. // 暂时不支持蓝光的预览
  32. if pkg.IsFile(videoFPath) == false {
  33. bok, _, _ := decode.IsFakeBDMVWorked(videoFPath)
  34. if bok == true {
  35. c.JSON(http.StatusOK, backend2.ReplyCommon{Message: "not support blu-ray preview"})
  36. return
  37. } else {
  38. c.JSON(http.StatusOK, backend2.ReplyCommon{Message: "video file not found"})
  39. return
  40. }
  41. }
  42. // segments/720/0/videofpathbase64
  43. template := fmt.Sprintf("/%s/preview/segments/{{.Resolution}}/{{.Segment}}/%v", cb.GetVersion(), videoFPathBase64)
  44. err = cb.hslCenter.WritePlaylist(template, videoFPath, c.Writer)
  45. if err != nil {
  46. return
  47. }
  48. }
  49. // HlsSegment 获取具体一个 ts 文件
  50. func (cb *ControllerBase) HlsSegment(c *gin.Context) {
  51. var err error
  52. defer func() {
  53. // 统一的异常处理
  54. cb.ErrorProcess(c, "HlsSegment", err)
  55. }()
  56. resolution := c.Param("resolution")
  57. segment := c.Param("segment")
  58. videoFPathBase64 := c.Param("videofpathbase64")
  59. // base64 解码
  60. videoFPathUrlEncodeStr, err := b64.StdEncoding.DecodeString(videoFPathBase64)
  61. if err != nil {
  62. return
  63. }
  64. // url 解码
  65. videoFPath, err := url.QueryUnescape(string(videoFPathUrlEncodeStr))
  66. if err != nil {
  67. return
  68. }
  69. segmentInt64, err := strconv.ParseInt(segment, 0, 64)
  70. if err != nil {
  71. return
  72. }
  73. resolutionInt64, err := strconv.ParseInt(resolution, 0, 64)
  74. if err != nil {
  75. return
  76. }
  77. err = cb.hslCenter.WriteSegment(videoFPath, segmentInt64, resolutionInt64, c.Writer)
  78. if err != nil {
  79. return
  80. }
  81. }