mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Store mediainfo in db
This commit is contained in:
parent
5e026449cc
commit
e91e0151ba
@ -100,7 +100,7 @@ type Audio struct {
|
|||||||
|
|
||||||
type Subtitle struct {
|
type Subtitle struct {
|
||||||
/// The index of this track on the media.
|
/// 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.
|
/// The title of the stream.
|
||||||
Title *string `json:"title"`
|
Title *string `json:"title"`
|
||||||
/// The language of this stream (as a IETF-BCP-47 language code)
|
/// 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)
|
path = fmt.Sprintf("%s/%s/sub/%d.%s", Settings.Metadata, sha, i, extension)
|
||||||
}
|
}
|
||||||
lang, _ := language.Parse(stream.Tags.Language)
|
lang, _ := language.Parse(stream.Tags.Language)
|
||||||
|
idx := uint32(i)
|
||||||
return Subtitle{
|
return Subtitle{
|
||||||
Index: uint32(i),
|
Index: &idx,
|
||||||
Title: OrNull(stream.Tags.Title),
|
Title: OrNull(stream.Tags.Title),
|
||||||
Language: NullIfUnd(lang.String()),
|
Language: NullIfUnd(lang.String()),
|
||||||
Codec: stream.CodecName,
|
Codec: stream.CodecName,
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
type MetadataService struct {
|
type MetadataService struct {
|
||||||
database *sqlx.DB
|
database *sqlx.DB
|
||||||
|
lock *RunLock[string, *MediaInfo]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s MetadataService) GetMetadata(path string, sha string) (*MediaInfo, error) {
|
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 {
|
if err = rows.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// TODO: retrieve mediainfo from file + store them in db.
|
return s.storeFreshMetadata(path, sha)
|
||||||
return &ret, nil
|
|
||||||
}
|
}
|
||||||
rows.StructScan(ret)
|
rows.StructScan(ret)
|
||||||
|
|
||||||
|
if ret.Versions.Info != InfoVersion {
|
||||||
|
return s.storeFreshMetadata(path, sha)
|
||||||
|
}
|
||||||
|
|
||||||
rows.NextResultSet()
|
rows.NextResultSet()
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var video Video
|
var video Video
|
||||||
@ -63,3 +67,56 @@ func (s MetadataService) GetMetadata(path string, sha string) (*MediaInfo, error
|
|||||||
|
|
||||||
return &ret, nil
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user