From a6076eb856ad96b1a36f30c748567a4140321310 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 13 Jan 2024 01:26:03 +0100 Subject: [PATCH] Add chapters in info --- transcoder/go.mod | 2 +- transcoder/go.sum | 6 ++++-- transcoder/src/info.go | 26 +++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/transcoder/go.mod b/transcoder/go.mod index bccb0e8b..5f684925 100644 --- a/transcoder/go.mod +++ b/transcoder/go.mod @@ -11,7 +11,7 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect - github.com/zelenin/go-mediainfo v1.0.0 // indirect + github.com/zoriya/go-mediainfo v0.0.0-20240113000440-36f500affcfd // indirect golang.org/x/crypto v0.17.0 // indirect golang.org/x/net v0.19.0 // indirect golang.org/x/sys v0.15.0 // indirect diff --git a/transcoder/go.sum b/transcoder/go.sum index 2b8854f9..54740a91 100644 --- a/transcoder/go.sum +++ b/transcoder/go.sum @@ -13,8 +13,10 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= -github.com/zelenin/go-mediainfo v1.0.0 h1:P0rKGyrSwKBCj37Ul7TLkrhf5OVBPlh3fFe9ZqjiaKQ= -github.com/zelenin/go-mediainfo v1.0.0/go.mod h1:6jxR5y1gDozFdCD7NYq/kUjCo5nqeogYCal6wpv81nY= +github.com/zoriya/go-mediainfo v0.0.0-20240112235842-ce0c807be738 h1:FV9TIvf/T84cRxRdBN6brSWKq+PqrGHmqbeDhmI+3tc= +github.com/zoriya/go-mediainfo v0.0.0-20240112235842-ce0c807be738/go.mod h1:jzun1oQGoJSh65g1XKaolTmjd6HW/34WHH7VMdJdbvM= +github.com/zoriya/go-mediainfo v0.0.0-20240113000440-36f500affcfd h1:AOdEpcmYJkmIW4I76TQim6LT4+9duYTdXNgkQsPHpuA= +github.com/zoriya/go-mediainfo v0.0.0-20240113000440-36f500affcfd/go.mod h1:jzun1oQGoJSh65g1XKaolTmjd6HW/34WHH7VMdJdbvM= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= diff --git a/transcoder/src/info.go b/transcoder/src/info.go index bc25134e..c3bc9943 100644 --- a/transcoder/src/info.go +++ b/transcoder/src/info.go @@ -8,7 +8,7 @@ import ( "strconv" "strings" - "github.com/zelenin/go-mediainfo" + "github.com/zoriya/go-mediainfo" ) type MediaInfo struct { @@ -96,7 +96,7 @@ type Chapter struct { func ParseFloat(str string) float32 { f, err := strconv.ParseFloat(str, 32) if err != nil { - panic(err) + return 0 } return float32(f) } @@ -104,11 +104,20 @@ func ParseFloat(str string) float32 { func ParseUint(str string) uint32 { i, err := strconv.ParseUint(str, 10, 32) if err != nil { - panic(err) + return 0 } return uint32(i) } +func ParseTime(str string) float32 { + x := strings.Split(str, ":") + hours, minutes, sms := ParseFloat(x[0]), ParseFloat(x[1]), x[2] + y := strings.Split(sms, ".") + seconds, ms := ParseFloat(y[0]), ParseFloat(y[1]) + + return (hours*60.+minutes)*60. + seconds + ms/1000. +} + // Stolen from the cmp.Or code that is not yet released // Or returns the first of its arguments that is not equal to the zero value. // If no argument is non-zero, it returns the zero value. @@ -162,6 +171,9 @@ func GetInfo(path string) (MediaInfo, error) { sha = hex.EncodeToString(h.Sum(nil)) } + chapters_begin := ParseUint(mi.Parameter(mediainfo.StreamMenu, 0, "Chapters_Pos_Begin")) + chapters_end := ParseUint(mi.Parameter(mediainfo.StreamMenu, 0, "Chapters_Pos_End")) + return MediaInfo{ Sha: sha, Path: path, @@ -217,5 +229,13 @@ func GetInfo(path string) (MediaInfo, error) { Link: link, } }), + Chapters: Map(make([]Chapter, chapters_end-chapters_begin-1), func(i int) Chapter { + 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), + } + }), }, nil }