Store mediainfo in db

This commit is contained in:
Zoe Roux 2024-07-28 15:07:42 +02:00
parent 5e026449cc
commit e91e0151ba
2 changed files with 62 additions and 4 deletions

View File

@ -100,7 +100,7 @@ type Audio struct {
type Subtitle struct {
/// The index of this track on the media.
Index uint32 `json:"index" db:"idx"`
Index *uint32 `json:"index" db:"idx"`
/// The title of the stream.
Title *string `json:"title"`
/// The language of this stream (as a IETF-BCP-47 language code)
@ -336,8 +336,9 @@ func RetriveMediaInfo(path string, sha string) (*MediaInfo, error) {
path = fmt.Sprintf("%s/%s/sub/%d.%s", Settings.Metadata, sha, i, extension)
}
lang, _ := language.Parse(stream.Tags.Language)
idx := uint32(i)
return Subtitle{
Index: uint32(i),
Index: &idx,
Title: OrNull(stream.Tags.Title),
Language: NullIfUnd(lang.String()),
Codec: stream.CodecName,

View File

@ -6,6 +6,7 @@ import (
type MetadataService struct {
database *sqlx.DB
lock *RunLock[string, *MediaInfo]
}
func (s MetadataService) GetMetadata(path string, sha string) (*MediaInfo, error) {
@ -28,11 +29,14 @@ func (s MetadataService) GetMetadata(path string, sha string) (*MediaInfo, error
if err = rows.Err(); err != nil {
return nil, err
}
// TODO: retrieve mediainfo from file + store them in db.
return &ret, nil
return s.storeFreshMetadata(path, sha)
}
rows.StructScan(ret)
if ret.Versions.Info != InfoVersion {
return s.storeFreshMetadata(path, sha)
}
rows.NextResultSet()
for rows.Next() {
var video Video
@ -63,3 +67,56 @@ func (s MetadataService) GetMetadata(path string, sha string) (*MediaInfo, error
return &ret, nil
}
func (s MetadataService) storeFreshMetadata(path string, sha string) (*MediaInfo, error) {
get_running, set := s.lock.Start(sha)
if get_running != nil {
return get_running()
}
ret, err := RetriveMediaInfo(path, sha)
if err != nil {
return set(nil, err)
}
tx := s.database.MustBegin()
tx.NamedExec(
`insert into info(sha, path, extension, mime_codec, size, duration, container, fonts, ver_info)
values (:sha, :path, :extension, :mime_codec, :size, :duration, :container, :fonts, :ver_info)`,
ret,
)
for _, video := range ret.Videos {
tx.NamedExec(
`insert into videos(sha, idx, title, language, codec, mime_codec, width, height, bitrate)
values (:sha, :idx, :title, :language, :codec, :mime_codec, :width, :height, :bitrate)`,
video,
)
}
for _, audio := range ret.Audios {
tx.NamedExec(
`insert into audios(sha, idx, title, language, codec, mime_codec, is_default)
values (:sha, :idx, :title, :language, :codec, :mime_codec, :is_default)`,
audio,
)
}
for _, subtitle := range ret.Subtitles {
tx.NamedExec(
`insert into subtitles(sha, idx, title, language, codec, extension, is_default, is_forced, is_external, path)
values (:sha, :idx, :title, :language, :codec, :extension, :is_default, :is_forced, :is_external, :path)`,
subtitle,
)
}
for _, chapter := range ret.Chapters {
tx.NamedExec(
`insert into chapters(sha, start_time, end_time, name, type)
values (:sha, :start_time, :end_time, :name, :type)`,
chapter,
)
}
err = tx.Commit()
if err != nil {
return set(ret, err)
}
return set(ret, nil)
}