update m3u8 ts name
This commit is contained in:
parent
16326daf66
commit
17d3d8540d
@ -89,7 +89,7 @@ func (server *Server) setupRouter() {
|
|||||||
|
|
||||||
router.HandleFunc("/play/{xid}", server.videoView).Methods(http.MethodGet)
|
router.HandleFunc("/play/{xid}", server.videoView).Methods(http.MethodGet)
|
||||||
router.HandleFunc("/media/{xid}/stream/", server.stream).Methods(http.MethodGet)
|
router.HandleFunc("/media/{xid}/stream/", server.stream).Methods(http.MethodGet)
|
||||||
router.HandleFunc("/media/{xid}/stream/{segName:index[0-9]+.ts}", server.stream).Methods(http.MethodGet)
|
router.HandleFunc("/media/{xid}/stream/{segName:[a-z0-9]+.ts}", server.stream).Methods(http.MethodGet)
|
||||||
|
|
||||||
subRouter := router.PathPrefix("/").Subrouter()
|
subRouter := router.PathPrefix("/").Subrouter()
|
||||||
subRouter.Use(server.authorize)
|
subRouter.Use(server.authorize)
|
||||||
|
|||||||
@ -1,12 +1,16 @@
|
|||||||
package convert
|
package convert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/zhang2092/mediahls/internal/pkg/fileutil"
|
"github.com/zhang2092/mediahls/internal/pkg/fileutil"
|
||||||
"github.com/zhang2092/mediahls/internal/pkg/logger"
|
"github.com/zhang2092/mediahls/internal/pkg/logger"
|
||||||
|
"github.com/zhang2092/mediahls/internal/pkg/rand"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ConvertHLS(savePath, filePath string) error {
|
func ConvertHLS(savePath, filePath string) error {
|
||||||
@ -35,7 +39,67 @@ func ConvertHLS(savePath, filePath string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
// 替换 m3u8 文件信息
|
||||||
|
return replaceM3u8(savePath)
|
||||||
|
|
||||||
// ffmpeg -i upload/20231129/o6e6qKaMdk0VC1Ys2SHnr.mp4 -profile:v baseline -level 3.0 -s 1920x1080 -start_number 0 -f hls -hls_time 10 -segment_list web/statics/js/index.m3u8 web/statics/mmm/index.m3u8
|
// ffmpeg -i upload/20231129/o6e6qKaMdk0VC1Ys2SHnr.mp4 -profile:v baseline -level 3.0 -s 1920x1080 -start_number 0 -f hls -hls_time 10 -segment_list web/statics/js/index.m3u8 web/statics/mmm/index.m3u8
|
||||||
// ffmpeg -i upload/20231129/o6e6qKaMdk0VC1Ys2SHnr.mp4 -c copy -map 0 -f segment -segment_list web/statics/js/index.m3u8 -segment_time 20 web/statics/js/index_%3d.ts
|
// ffmpeg -i upload/20231129/o6e6qKaMdk0VC1Ys2SHnr.mp4 -c copy -map 0 -f segment -segment_list web/statics/js/index.m3u8 -segment_time 20 web/statics/js/index_%3d.ts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type newOldName struct {
|
||||||
|
old string
|
||||||
|
new string
|
||||||
|
}
|
||||||
|
|
||||||
|
func replaceM3u8(savePath string) error {
|
||||||
|
f, err := os.OpenFile(savePath+"/index.m3u8", os.O_RDWR, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
reader := bufio.NewReader(f)
|
||||||
|
var result []newOldName
|
||||||
|
var content string
|
||||||
|
for {
|
||||||
|
line, _, err := reader.ReadLine()
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ls := string(line)
|
||||||
|
if strings.HasSuffix(ls, ".ts") {
|
||||||
|
ext := path.Ext(ls)
|
||||||
|
filename := strings.TrimSuffix(ls, ext)
|
||||||
|
temp := rand.RandomString(5)
|
||||||
|
result = append(result, newOldName{
|
||||||
|
old: ls,
|
||||||
|
new: temp + ext,
|
||||||
|
})
|
||||||
|
ls = strings.ReplaceAll(ls, filename, temp)
|
||||||
|
}
|
||||||
|
content += ls + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
fw, err := os.OpenFile(savePath+"/index.m3u8", os.O_WRONLY|os.O_TRUNC, 0777)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer fw.Close()
|
||||||
|
|
||||||
|
writer := bufio.NewWriter(fw)
|
||||||
|
writer.WriteString(content)
|
||||||
|
if err := writer.Flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range result {
|
||||||
|
if err := os.Rename(savePath+item.old, savePath+item.new); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
25
internal/pkg/rand/rand.go
Normal file
25
internal/pkg/rand/rand.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package rand
|
||||||
|
|
||||||
|
import (
|
||||||
|
rd "crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RandomBytes(n int) []byte {
|
||||||
|
b := make([]byte, n)
|
||||||
|
bs, err := io.ReadFull(rd.Reader, b)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if bs < n {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func RandomString(n int) string {
|
||||||
|
b := RandomBytes(n)
|
||||||
|
return hex.EncodeToString(b)
|
||||||
|
}
|
||||||
4
main.go
4
main.go
@ -19,10 +19,6 @@ var templateFS embed.FS
|
|||||||
var staticFS embed.FS
|
var staticFS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// filename, _ := nanoId.Nanoid()
|
|
||||||
// log.Println(filename)
|
|
||||||
// return
|
|
||||||
|
|
||||||
// Set up templates
|
// Set up templates
|
||||||
templates, err := fs.Sub(templateFS, "web/templates")
|
templates, err := fs.Sub(templateFS, "web/templates")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user