mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Transcode stream refactor (#609)
* Remove forceMkv and forceHEVC * Add HLS support and refactor * Add new streaming endpoints
This commit is contained in:
59
pkg/utils/byterange.go
Normal file
59
pkg/utils/byterange.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ByteRange struct {
|
||||
Start int64
|
||||
End *int64
|
||||
RawString string
|
||||
}
|
||||
|
||||
func CreateByteRange(s string) ByteRange {
|
||||
// strip bytes=
|
||||
r := strings.TrimPrefix(s, "bytes=")
|
||||
e := strings.Split(r, "-")
|
||||
|
||||
ret := ByteRange{
|
||||
RawString: s,
|
||||
}
|
||||
if len(e) > 0 {
|
||||
ret.Start, _ = strconv.ParseInt(e[0], 10, 64)
|
||||
}
|
||||
if len(e) > 1 && e[1] != "" {
|
||||
end, _ := strconv.ParseInt(e[1], 10, 64)
|
||||
ret.End = &end
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (r ByteRange) getBytesToRead() int64 {
|
||||
if r.End == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return *r.End - r.Start + 1
|
||||
}
|
||||
|
||||
func (r ByteRange) ToHeaderValue(fileLength int64) string {
|
||||
if r.End == nil {
|
||||
return ""
|
||||
}
|
||||
end := *r.End
|
||||
return "bytes " + strconv.FormatInt(r.Start, 10) + "-" + strconv.FormatInt(end, 10) + "/" + strconv.FormatInt(fileLength, 10)
|
||||
}
|
||||
|
||||
func (r ByteRange) Apply(bytes []byte) []byte {
|
||||
if r.End == nil {
|
||||
return bytes[r.Start:]
|
||||
}
|
||||
|
||||
end := *r.End + 1
|
||||
if int(end) > len(bytes) {
|
||||
end = int64(len(bytes))
|
||||
}
|
||||
return bytes[r.Start:end]
|
||||
}
|
||||
Reference in New Issue
Block a user