Create different index.m3u8 for audio and video

This commit is contained in:
Zoe Roux 2024-07-01 17:20:29 +00:00
parent f6dab80a98
commit 9fad5da0a4
No known key found for this signature in database
4 changed files with 73 additions and 46 deletions

View File

@ -36,3 +36,29 @@ func (as *AudioStream) getTranscodeArgs(segments string) []string {
"-b:a", "128k",
}
}
func (ts *AudioStream) GetIndex() (string, error) {
index := `#EXTM3U
#EXT-X-VERSION:7
#EXT-X-PLAYLIST-TYPE:EVENT
#EXT-X-START:TIME-OFFSET=0
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-MAP:URI="init.mp4"
`
index += fmt.Sprintf("#EXT-X-TARGETDURATION:%d\n", int(OptimalFragmentDuration)+1)
count := int32((float64(ts.file.Info.Duration) / OptimalFragmentDuration))
for segment := int32(0); segment < count; segment++ {
index += fmt.Sprintf("#EXTINF:%.6f\n", OptimalFragmentDuration)
index += fmt.Sprintf("segment-%d.m4s\n", segment)
}
last_ts := float64(count) * OptimalFragmentDuration
if last_ts > 0 {
index += fmt.Sprintf("#EXTINF:%.6f\n", float64(ts.file.Info.Duration)-last_ts)
index += fmt.Sprintf("segment-%d.m4s\n", count)
}
index += `#EXT-X-ENDLIST`
return index, nil
}

View File

@ -114,24 +114,24 @@ func (fs *FileStream) GetMaster() string {
}
}
}
// for _, audio := range fs.Info.Audios {
// master += "#EXT-X-MEDIA:TYPE=AUDIO,"
// master += "GROUP-ID=\"audio\","
// if audio.Language != nil {
// master += fmt.Sprintf("LANGUAGE=\"%s\",", *audio.Language)
// }
// if audio.Title != nil {
// master += fmt.Sprintf("NAME=\"%s\",", *audio.Title)
// } else if audio.Language != nil {
// master += fmt.Sprintf("NAME=\"%s\",", *audio.Language)
// } else {
// master += fmt.Sprintf("NAME=\"Audio %d\",", audio.Index)
// }
// if audio.IsDefault {
// master += "DEFAULT=YES,"
// }
// master += fmt.Sprintf("URI=\"./audio/%d/index.m3u8\"\n", audio.Index)
// }
for _, audio := range fs.Info.Audios {
master += "#EXT-X-MEDIA:TYPE=AUDIO,"
master += "GROUP-ID=\"audio\","
if audio.Language != nil {
master += fmt.Sprintf("LANGUAGE=\"%s\",", *audio.Language)
}
if audio.Title != nil {
master += fmt.Sprintf("NAME=\"%s\",", *audio.Title)
} else if audio.Language != nil {
master += fmt.Sprintf("NAME=\"%s\",", *audio.Language)
} else {
master += fmt.Sprintf("NAME=\"Audio %d\",", audio.Index)
}
if audio.IsDefault {
master += "DEFAULT=YES,"
}
master += fmt.Sprintf("URI=\"./audio/%d/index.m3u8\"\n", audio.Index)
}
return master
}

View File

@ -26,6 +26,7 @@ type StreamHandle interface {
getTranscodeArgs(segments string) []string
getSegmentName() string
getFlags() Flags
GetIndex() (string, error)
}
type Stream struct {
@ -377,34 +378,6 @@ func (ts *Stream) run(start int32) error {
return nil
}
func (ts *Stream) GetIndex() (string, error) {
// playlist type is event since we can append to the list if Keyframe.IsDone is false.
// start time offset makes the stream start at 0s instead of ~3segments from the end (requires version 6 of hls)
index := `#EXTM3U
#EXT-X-VERSION:7
#EXT-X-PLAYLIST-TYPE:EVENT
#EXT-X-START:TIME-OFFSET=0
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-MAP:URI="init.mp4"
`
index += fmt.Sprintf("#EXT-X-TARGETDURATION:%d\n", int(OptimalFragmentDuration))
length, is_done := ts.file.Keyframes.Length()
for segment := int32(0); segment < length-1; segment++ {
index += fmt.Sprintf("#EXTINF:%.6f\n", ts.file.Keyframes.Get(segment+1)-ts.file.Keyframes.Get(segment))
index += fmt.Sprintf("segment-%d.m4s\n", segment)
}
// do not forget to add the last segment between the last keyframe and the end of the file
// if the keyframes extraction is not done, do not bother to add it, it will be retrieval on the next index retrieval
if is_done {
index += fmt.Sprintf("#EXTINF:%.6f\n", float64(ts.file.Info.Duration)-ts.file.Keyframes.Get(length-1))
index += fmt.Sprintf("segment-%d.m4s\n", length-1)
index += `#EXT-X-ENDLIST`
}
return index, nil
}
func (ts *Stream) GetInit() (string, error) {
// No need to lock, the channel won't change.
select {

View File

@ -72,3 +72,31 @@ func (vs *VideoStream) getTranscodeArgs(segments string) []string {
)
return args
}
func (ts *VideoStream) GetIndex() (string, error) {
// playlist type is event since we can append to the list if Keyframe.IsDone is false.
// start time offset makes the stream start at 0s instead of ~3segments from the end (requires version 6 of hls)
index := `#EXTM3U
#EXT-X-VERSION:7
#EXT-X-PLAYLIST-TYPE:EVENT
#EXT-X-START:TIME-OFFSET=0
#EXT-X-MEDIA-SEQUENCE:0
#EXT-X-INDEPENDENT-SEGMENTS
#EXT-X-MAP:URI="init.mp4"
`
index += fmt.Sprintf("#EXT-X-TARGETDURATION:%d\n", int(OptimalFragmentDuration)+1)
length, is_done := ts.file.Keyframes.Length()
for segment := int32(0); segment < length-1; segment++ {
index += fmt.Sprintf("#EXTINF:%.6f\n", ts.file.Keyframes.Get(segment+1)-ts.file.Keyframes.Get(segment))
index += fmt.Sprintf("segment-%d.m4s\n", segment)
}
// do not forget to add the last segment between the last keyframe and the end of the file
// if the keyframes extraction is not done, do not bother to add it, it will be retrieval on the next index retrieval
if is_done {
index += fmt.Sprintf("#EXTINF:%.6f\n", float64(ts.file.Info.Duration)-ts.file.Keyframes.Get(length-1))
index += fmt.Sprintf("segment-%d.m4s\n", length-1)
index += `#EXT-X-ENDLIST`
}
return index, nil
}