mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-08 18:54:22 -04:00
Transcoder: Prevent error caused by buggy chapters parsing by mediainfo (#485)
This commit is contained in:
commit
b532bb31d1
@ -13,6 +13,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
"unicode"
|
||||||
|
|
||||||
"github.com/zoriya/go-mediainfo"
|
"github.com/zoriya/go-mediainfo"
|
||||||
)
|
)
|
||||||
@ -242,13 +243,14 @@ func getInfo(path string) (*MediaInfo, error) {
|
|||||||
|
|
||||||
// fmt.Printf("%s", mi.Option("info_parameters", ""))
|
// fmt.Printf("%s", mi.Option("info_parameters", ""))
|
||||||
|
|
||||||
|
// duration in seconds
|
||||||
|
duration := ParseFloat(mi.Parameter(mediainfo.StreamGeneral, 0, "Duration")) / 1000
|
||||||
ret := MediaInfo{
|
ret := MediaInfo{
|
||||||
Path: path,
|
Path: path,
|
||||||
// Remove leading .
|
// Remove leading .
|
||||||
Extension: filepath.Ext(path)[1:],
|
Extension: filepath.Ext(path)[1:],
|
||||||
Size: ParseUint64(mi.Parameter(mediainfo.StreamGeneral, 0, "FileSize")),
|
Size: ParseUint64(mi.Parameter(mediainfo.StreamGeneral, 0, "FileSize")),
|
||||||
// convert ms to seconds
|
Duration: duration,
|
||||||
Duration: ParseFloat(mi.Parameter(mediainfo.StreamGeneral, 0, "Duration")) / 1000,
|
|
||||||
Container: OrNull(mi.Parameter(mediainfo.StreamGeneral, 0, "Format")),
|
Container: OrNull(mi.Parameter(mediainfo.StreamGeneral, 0, "Format")),
|
||||||
Videos: Map(make([]Video, ParseUint(mi.Parameter(mediainfo.StreamVideo, 0, "StreamCount"))), func(_ Video, i int) Video {
|
Videos: Map(make([]Video, ParseUint(mi.Parameter(mediainfo.StreamVideo, 0, "StreamCount"))), func(_ Video, i int) Video {
|
||||||
return Video{
|
return Video{
|
||||||
@ -300,14 +302,7 @@ func getInfo(path string) (*MediaInfo, error) {
|
|||||||
Link: link,
|
Link: link,
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Chapters: Map(make([]Chapter, max(chapters_end-chapters_begin, 1)-1), func(_ Chapter, i int) Chapter {
|
Chapters: getChapters(chapters_begin, chapters_end, mi, duration),
|
||||||
return Chapter{
|
|
||||||
StartTime: ParseTime(mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i, mediainfo.InfoName)),
|
|
||||||
// +1 is safe, the value at chapters_end contains the right duration
|
|
||||||
EndTime: ParseTime(mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i+1, mediainfo.InfoName)),
|
|
||||||
Name: mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i, mediainfo.InfoText),
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
Fonts: Map(
|
Fonts: Map(
|
||||||
attachments,
|
attachments,
|
||||||
func(font string, _ int) string {
|
func(font string, _ int) string {
|
||||||
@ -337,3 +332,40 @@ func getInfo(path string) (*MediaInfo, error) {
|
|||||||
}
|
}
|
||||||
return &ret, nil
|
return &ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func chapterTimeIsValid(chapterTime string) bool {
|
||||||
|
return len(chapterTime) > 0 && unicode.IsDigit(rune(chapterTime[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 := 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
|
||||||
|
}
|
||||||
|
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) {
|
||||||
|
endTime = ParseTime(rawEndTime)
|
||||||
|
} else {
|
||||||
|
endTime = duration
|
||||||
|
}
|
||||||
|
chapters[chapterIndex] = Chapter{
|
||||||
|
StartTime: ParseTime(rawStartTime),
|
||||||
|
EndTime: endTime,
|
||||||
|
Name: mi.GetI(mediainfo.StreamMenu, 0, int(chapters_begin)+i, mediainfo.InfoText),
|
||||||
|
}
|
||||||
|
chapterIndex++
|
||||||
|
}
|
||||||
|
return chapters
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user