Fix vtt for chapter display in scene players (#263)

This commit is contained in:
WithoutPants 2019-12-14 07:41:46 +11:00 committed by Leopere
parent da3e91193c
commit f8a760d729
3 changed files with 35 additions and 3 deletions

View File

@ -112,6 +112,33 @@ func (rs sceneRoutes) Webp(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath) http.ServeFile(w, r, filepath)
} }
func getChapterVttTitle(marker *models.SceneMarker) string {
if marker.Title != "" {
return marker.Title
}
qb := models.NewTagQueryBuilder()
primaryTag, err := qb.Find(marker.PrimaryTagID, nil)
if err != nil {
// should not happen
panic(err)
}
ret := primaryTag.Name
tags, err := qb.FindBySceneMarkerID(marker.ID, nil)
if err != nil {
// should not happen
panic(err)
}
for _, t := range tags {
ret += ", " + t.Name
}
return ret
}
func (rs sceneRoutes) ChapterVtt(w http.ResponseWriter, r *http.Request) { func (rs sceneRoutes) ChapterVtt(w http.ResponseWriter, r *http.Request) {
scene := r.Context().Value(sceneKey).(*models.Scene) scene := r.Context().Value(sceneKey).(*models.Scene)
qb := models.NewSceneMarkerQueryBuilder() qb := models.NewSceneMarkerQueryBuilder()
@ -121,10 +148,11 @@ func (rs sceneRoutes) ChapterVtt(w http.ResponseWriter, r *http.Request) {
} }
vttLines := []string{"WEBVTT", ""} vttLines := []string{"WEBVTT", ""}
for _, marker := range sceneMarkers { for i, marker := range sceneMarkers {
vttLines = append(vttLines, strconv.Itoa(i+1))
time := utils.GetVTTTime(marker.Seconds) time := utils.GetVTTTime(marker.Seconds)
vttLines = append(vttLines, time+" --> "+time) vttLines = append(vttLines, time+" --> "+time)
vttLines = append(vttLines, marker.Title) vttLines = append(vttLines, getChapterVttTitle(marker))
vttLines = append(vttLines, "") vttLines = append(vttLines, "")
} }
vtt := strings.Join(vttLines, "\n") vtt := strings.Join(vttLines, "\n")

View File

@ -5,7 +5,7 @@ import (
"time" "time"
) )
// GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss) // GetVTTTime returns a timestamp appropriate for VTT files (hh:mm:ss.mmm)
func GetVTTTime(totalSeconds float64) (s string) { func GetVTTTime(totalSeconds float64) (s string) {
totalSecondsString := strconv.FormatFloat(totalSeconds, 'f', -1, 64) totalSecondsString := strconv.FormatFloat(totalSeconds, 'f', -1, 64)
secondsDuration, _ := time.ParseDuration(totalSecondsString + "s") secondsDuration, _ := time.ParseDuration(totalSecondsString + "s")
@ -34,5 +34,8 @@ func GetVTTTime(totalSeconds float64) (s string) {
} }
s += strconv.Itoa(seconds) s += strconv.Itoa(seconds)
// videojs requires milliseconds
s += ".000"
return return
} }

View File

@ -91,6 +91,7 @@ export class VideoJSPlayer extends React.Component<IVideoJSPlayerProps> {
poster={this.props.scene.paths.screenshot} poster={this.props.scene.paths.screenshot}
controls controls
preload="auto"> preload="auto">
<track kind="chapters" label="Markers" src={this.props.scene.paths.chapters_vtt} default></track>
</video> </video>
</div> </div>
</div> </div>