feat: update video cut and extract head image job
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/database/fields"
|
||||
"quyun/pkg/utils"
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/job"
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
var _ contracts.JobArgs = (*VideoCut)(nil)
|
||||
@@ -63,53 +62,34 @@ func (w *VideoCutWorker) Work(ctx context.Context, job *Job[VideoCut]) error {
|
||||
}
|
||||
|
||||
input := filepath.Join(w.app.StoragePath, media.Path)
|
||||
output := input[:len(input)-len(filepath.Ext(input))] + "-output" + filepath.Ext(input)
|
||||
output := input[:len(input)-len(filepath.Ext(input))] + "-short" + filepath.Ext(input)
|
||||
|
||||
log.Infof("cut video process %s to %s", input, output)
|
||||
cmd := exec.Command("ffmpeg", "-ss", "00:00:00", "-i", input, "-to", "00:01:00", "-c", "copy", output)
|
||||
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err := utils.CutMedia(input, output, 0, 60); err != nil {
|
||||
log.Errorf("Error cutting media: %v", err)
|
||||
return JobCancel(err)
|
||||
}
|
||||
|
||||
duration, err := utils.GetMediaDuration(input)
|
||||
if err != nil {
|
||||
log.Errorf("Error creating stdout pipe: %v", err)
|
||||
return err
|
||||
log.Errorf("Error getting media duration: %v", err)
|
||||
return JobCancel(err)
|
||||
}
|
||||
// update media metas
|
||||
metas := fields.MediaMetas{
|
||||
ParentHash: "",
|
||||
Short: false,
|
||||
Duration: duration,
|
||||
}
|
||||
if err := models.Medias.UpdateMetas(ctx, media.ID, metas); err != nil {
|
||||
log.Errorf("Error updating media metas: %v", err)
|
||||
return JobCancel(err)
|
||||
}
|
||||
|
||||
stderr, err := cmd.StderrPipe()
|
||||
if err != nil {
|
||||
log.Errorf("Error creating stderr pipe: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
log.Errorf("Error starting command: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
var eg errgroup.Group
|
||||
eg.Go(func() error {
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
for scanner.Scan() {
|
||||
log.Info(scanner.Text())
|
||||
}
|
||||
return nil
|
||||
// save to database
|
||||
return w.job.Add(&VideoStoreShort{
|
||||
MediaID: media.ID,
|
||||
FilePath: output,
|
||||
})
|
||||
|
||||
eg.Go(func() error {
|
||||
scanner := bufio.NewScanner(stderr)
|
||||
for scanner.Scan() {
|
||||
log.Error(scanner.Text())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
log.Errorf("Error waiting for command: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if err := eg.Wait(); err != nil {
|
||||
log.Errorf("Error waiting for command: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user