Rework extraction to use new cache

This commit is contained in:
Zoe Roux 2024-07-29 23:36:35 +02:00
parent a75bd48ccf
commit 5991916227
3 changed files with 67 additions and 68 deletions

View File

@ -189,7 +189,7 @@ func (h *Handler) GetInfo(c echo.Context) error {
// //
// Path: /:path/attachment/:name // Path: /:path/attachment/:name
func (h *Handler) GetAttachment(c echo.Context) error { func (h *Handler) GetAttachment(c echo.Context) error {
path, sha, err := GetPath(c) _, sha, err := GetPath(c)
if err != nil { if err != nil {
return err return err
} }
@ -198,12 +198,6 @@ func (h *Handler) GetAttachment(c echo.Context) error {
return err return err
} }
wait, err := src.Extract(path, sha)
if err != nil {
return err
}
<-wait
ret := fmt.Sprintf("%s/%s/att/%s", src.Settings.Metadata, sha, name) ret := fmt.Sprintf("%s/%s/att/%s", src.Settings.Metadata, sha, name)
return c.File(ret) return c.File(ret)
} }
@ -214,7 +208,7 @@ func (h *Handler) GetAttachment(c echo.Context) error {
// //
// Path: /:path/subtitle/:name // Path: /:path/subtitle/:name
func (h *Handler) GetSubtitle(c echo.Context) error { func (h *Handler) GetSubtitle(c echo.Context) error {
path, sha, err := GetPath(c) _, sha, err := GetPath(c)
if err != nil { if err != nil {
return err return err
} }
@ -223,12 +217,6 @@ func (h *Handler) GetSubtitle(c echo.Context) error {
return err return err
} }
wait, err := src.Extract(path, sha)
if err != nil {
return err
}
<-wait
ret := fmt.Sprintf("%s/%s/sub/%s", src.Settings.Metadata, sha, name) ret := fmt.Sprintf("%s/%s/sub/%s", src.Settings.Metadata, sha, name)
return c.File(ret) return c.File(ret)
} }

View File

@ -9,60 +9,67 @@ import (
var extracted = NewCMap[string, <-chan struct{}]() var extracted = NewCMap[string, <-chan struct{}]()
func Extract(path string, sha string) (<-chan struct{}, error) { const ExtractVersion = 1
ret := make(chan struct{})
existing, created := extracted.GetOrSet(sha, ret) func (s MetadataService) ExtractSubs(info *MediaInfo) (interface{}, error) {
if !created { get_running, set := s.extractLock.Start(info.Sha)
return existing, nil if get_running != nil {
return get_running()
} }
go func() { err := extractSubs(info)
defer printExecTime("Starting extraction of %s", path)() if err != nil {
info, err := GetInfo(path, sha) return set(nil, err)
if err != nil { }
extracted.Remove(sha) _, err = s.database.NamedExec(
close(ret) `update info set ver_extract = :version where sha = :sha`,
return map[string]interface{}{
} "sha": info.Sha,
attachment_path := fmt.Sprintf("%s/%s/att", Settings.Metadata, sha) "version": ExtractVersion,
subs_path := fmt.Sprintf("%s/%s/sub", Settings.Metadata, sha) },
os.MkdirAll(attachment_path, 0o755) )
os.MkdirAll(subs_path, 0o755) return set(nil, err)
}
// If there is no subtitles, there is nothing to extract (also fonts would be useless).
if len(info.Subtitles) == 0 { func extractSubs(info *MediaInfo) error {
close(ret) defer printExecTime("Starting extraction of %s", info.Path)()
return
} attachment_path := fmt.Sprintf("%s/%s/att", Settings.Metadata, info.Sha)
subs_path := fmt.Sprintf("%s/%s/sub", Settings.Metadata, info.Sha)
cmd := exec.Command(
"ffmpeg", os.MkdirAll(attachment_path, 0o755)
"-dump_attachment:t", "", os.MkdirAll(subs_path, 0o755)
// override old attachments
"-y", // If there is no subtitles, there is nothing to extract (also fonts would be useless).
"-i", path, if len(info.Subtitles) == 0 {
) return nil
cmd.Dir = attachment_path }
for _, sub := range info.Subtitles { cmd := exec.Command(
if ext := sub.Extension; ext != nil { "ffmpeg",
cmd.Args = append( "-dump_attachment:t", "",
cmd.Args, // override old attachments
"-map", fmt.Sprintf("0:s:%d", sub.Index), "-y",
"-c:s", "copy", "-i", info.Path,
fmt.Sprintf("%s/%d.%s", subs_path, sub.Index, *ext), )
) cmd.Dir = attachment_path
}
} for _, sub := range info.Subtitles {
log.Printf("Starting extraction with the command: %s", cmd) if ext := sub.Extension; ext != nil {
cmd.Stdout = nil cmd.Args = append(
err = cmd.Run() cmd.Args,
if err != nil { "-map", fmt.Sprintf("0:s:%d", sub.Index),
extracted.Remove(sha) "-c:s", "copy",
fmt.Println("Error starting ffmpeg extract:", err) fmt.Sprintf("%s/%d.%s", subs_path, sub.Index, *ext),
} )
close(ret) }
}() }
log.Printf("Starting extraction with the command: %s", cmd)
return ret, nil cmd.Stdout = nil
err := cmd.Run()
if err != nil {
fmt.Println("Error starting ffmpeg extract:", err)
return err
}
return nil
} }

View File

@ -8,6 +8,7 @@ type MetadataService struct {
database *sqlx.DB database *sqlx.DB
lock *RunLock[string, *MediaInfo] lock *RunLock[string, *MediaInfo]
thumbLock *RunLock[string, interface{}] thumbLock *RunLock[string, interface{}]
extractLock *RunLock[string, interface{}]
} }
func (s MetadataService) GetMetadata(path string, sha string) (*MediaInfo, error) { func (s MetadataService) GetMetadata(path string, sha string) (*MediaInfo, error) {
@ -19,6 +20,9 @@ func (s MetadataService) GetMetadata(path string, sha string) (*MediaInfo, error
if ret.Versions.Thumbs < ThumbsVersion { if ret.Versions.Thumbs < ThumbsVersion {
go s.ExtractThumbs(path, sha) go s.ExtractThumbs(path, sha)
} }
if ret.Versions.Extract < ExtractVersion {
go s.ExtractSubs(ret)
}
return ret, nil return ret, nil
} }