Skip to content

Commit

Permalink
Transcoder: Make Chapter parsing simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthi-chaud committed May 13, 2024
1 parent 8a2343e commit 324afc8
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions transcoder/src/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,6 @@ func Map[T, U any](ts []T, f func(T, int) U) []U {
return us
}

func Filter[T any](ss []T, test func(T, int) bool) (ret []T) {
for i, s := range ss {
if test(s, i) {
ret = append(ret, s)
}
}
return
}

func OrNull(str string) *string {
if str == "" {
return nil
Expand Down Expand Up @@ -349,30 +340,32 @@ func chapterTimeIsValid(chapterTime string) bool {
func getChapters(chapters_begin uint32, chapters_end uint32, mi *mediainfo.File, duration float32) []Chapter {
chapterCount := max(chapters_end-chapters_begin, 1)
chapterIterationCount := chapterCount
chapters := make([]Chapter, chapterCount)
chapterIndex := 0

for i := uint32(0); i < chapterIterationCount; i++ {
startTime := mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin+i), mediainfo.InfoName)
if !chapterTimeIsValid(startTime) {
for i := 0; i < int(chapterIterationCount); i++ {
rawStartTime := mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i, mediainfo.InfoName)
rawEndTime := mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i+1, mediainfo.InfoName)
// If true, this "chapter" is invalid. We skip it
if !chapterTimeIsValid(rawStartTime) {
chapterIterationCount = chapterIterationCount + 1
continue
}
}
return Filter(Map(make([]Chapter, chapterIterationCount), func(_ Chapter, i int) Chapter {
startTime := mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i, mediainfo.InfoName)
if !chapterTimeIsValid(startTime) {
return Chapter{}
}
// +1 is safe, the value at chapters_end contains the right duration
rawEndTime := mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i+1, mediainfo.InfoName)
parsedEndTime := duration
var endTime float32
// If this fails, we probably are at the end of the video
// Since there would be no following chapter,
// we defacto set the end time to the end of the video (i.e. its duration)
if chapterTimeIsValid(rawEndTime) {
parsedEndTime = ParseTime(rawEndTime)
endTime = ParseTime(rawEndTime)
} else {
endTime = duration
}
return Chapter{
StartTime: ParseTime(startTime),
EndTime: parsedEndTime,
chapters[chapterIndex] = Chapter{
StartTime: ParseTime(rawStartTime),
EndTime: endTime,
Name: mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i, mediainfo.InfoText),
}
}), func(chapter Chapter, i int) bool {
return chapter != Chapter{}
})
chapterIndex++
}
return chapters
}

0 comments on commit 324afc8

Please sign in to comment.