From 4f80decce4002887c93d9663825c2e45d1929629 Mon Sep 17 00:00:00 2001 From: StashAppDev Date: Mon, 11 Feb 2019 12:12:08 -0800 Subject: [PATCH] Added transcode task --- api/resolver_model_scene.go | 2 +- manager/manager_tasks.go | 5 ++-- manager/task_transcode.go | 47 +++++++++++++++++++++++++++++++++++++ manager/utils.go | 29 +++++++++++++++++------ 4 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 manager/task_transcode.go diff --git a/api/resolver_model_scene.go b/api/resolver_model_scene.go index ed63c1254..636a911cf 100644 --- a/api/resolver_model_scene.go +++ b/api/resolver_model_scene.go @@ -87,7 +87,7 @@ func (r *sceneResolver) Paths(ctx context.Context, obj *models.Scene) (models.Sc } func (r *sceneResolver) IsStreamable(ctx context.Context, obj *models.Scene) (bool, error) { - return manager.IsStreamable(obj.Path, obj.Checksum) + return manager.IsStreamable(obj) } func (r *sceneResolver) SceneMarkers(ctx context.Context, obj *models.Scene) ([]models.SceneMarker, error) { diff --git a/manager/manager_tasks.go b/manager/manager_tasks.go index c221c7928..d111ecee8 100644 --- a/manager/manager_tasks.go +++ b/manager/manager_tasks.go @@ -98,9 +98,8 @@ func (s *singleton) Generate(sprites bool, previews bool, markers bool, transcod } if transcodes { - go func() { - wg.Done() // TODO - }() + task := GenerateTranscodeTask{Scene: scene} + go task.Start(&wg) } wg.Wait() diff --git a/manager/task_transcode.go b/manager/task_transcode.go new file mode 100644 index 000000000..beaf6d8be --- /dev/null +++ b/manager/task_transcode.go @@ -0,0 +1,47 @@ +package manager + +import ( + "github.com/stashapp/stash/ffmpeg" + "github.com/stashapp/stash/logger" + "github.com/stashapp/stash/models" + "os" + "sync" +) + +type GenerateTranscodeTask struct { + Scene models.Scene +} + +func (t *GenerateTranscodeTask) Start(wg *sync.WaitGroup) { + defer wg.Done() + videoCodec := t.Scene.VideoCodec.String + if ffmpeg.IsValidCodec(videoCodec) { + return + } + + hasTranscode, _ := HasTranscode(&t.Scene) + if hasTranscode { + return + } + + logger.Infof("[transcode] <%s> scene has codec %s", t.Scene.Checksum, t.Scene.VideoCodec.String) + + videoFile, err := ffmpeg.NewVideoFile(instance.StaticPaths.FFProbe, t.Scene.Path) + if err != nil { + logger.Errorf("[transcode] error reading video file: %s", err.Error()) + return + } + + outputPath := instance.Paths.Generated.GetTmpPath(t.Scene.Checksum+".mp4") + options := ffmpeg.TranscodeOptions{ + OutputPath: outputPath, + } + encoder := ffmpeg.NewEncoder(instance.StaticPaths.FFMPEG) + encoder.Transcode(*videoFile, options) + if err := os.Rename(outputPath, instance.Paths.Scene.GetTranscodePath(t.Scene.Checksum)); err != nil { + logger.Errorf("[transcode] error generating transcode: %s", err.Error()) + return + } + logger.Debug("[transcode] <%s> created transcode: ", t.Scene.Checksum) + return +} \ No newline at end of file diff --git a/manager/utils.go b/manager/utils.go index 61cac3829..fed5ba515 100644 --- a/manager/utils.go +++ b/manager/utils.go @@ -1,17 +1,32 @@ package manager -import "github.com/stashapp/stash/utils" +import ( + "fmt" + "github.com/stashapp/stash/models" + "github.com/stashapp/stash/utils" +) -func IsStreamable(videoPath string, checksum string) (bool, error) { - fileType, err := utils.FileType(videoPath) +func IsStreamable(scene *models.Scene) (bool, error) { + if scene == nil { + return false, fmt.Errorf("nil scene") + } + fileType, err := utils.FileType(scene.Path) if err != nil { return false, err } - if fileType.MIME.Value == "video/quicktime" || fileType.MIME.Value == "video/mp4" || fileType.MIME.Value == "video/webm" || fileType.MIME.Value == "video/x-m4v" { + switch fileType.MIME.Value { + case "video/quicktime", "video/mp4", "video/webm", "video/x-m4v": return true, nil - } else { - transcodePath := instance.Paths.Scene.GetTranscodePath(checksum) - return utils.FileExists(transcodePath) + default: + return HasTranscode(scene) } +} + +func HasTranscode(scene *models.Scene) (bool, error) { + if scene == nil { + return false, fmt.Errorf("nil scene") + } + transcodePath := instance.Paths.Scene.GetTranscodePath(scene.Checksum) + return utils.FileExists(transcodePath) } \ No newline at end of file