feat: cut video

This commit is contained in:
yanghao05
2025-04-21 20:31:22 +08:00
parent 75bbca00cf
commit 326a9e523b
8 changed files with 129 additions and 115 deletions

View File

@@ -1,77 +0,0 @@
package jobs
import (
"context"
"time"
"quyun/app/models"
"quyun/providers/ali"
"quyun/providers/job"
. "github.com/riverqueue/river"
log "github.com/sirupsen/logrus"
_ "go.ipao.vip/atom"
"go.ipao.vip/atom/contracts"
)
var _ contracts.JobArgs = (*WechatCallback)(nil)
type ExtractAudioFromVideo struct {
MediaID int64 `json:"media_id"`
}
func (s ExtractAudioFromVideo) InsertOpts() InsertOpts {
return InsertOpts{
Queue: QueueDefault,
Priority: PriorityDefault,
}
}
func (s ExtractAudioFromVideo) Kind() string { return "extract_audio_from_video" }
func (a ExtractAudioFromVideo) UniqueID() string { return a.Kind() }
var _ Worker[ExtractAudioFromVideo] = (*ExtractAudioFromVideoWorker)(nil)
// @provider(job)
type ExtractAudioFromVideoWorker struct {
WorkerDefaults[ExtractAudioFromVideo]
oss *ali.OSSClient
job *job.Job
}
func (w *ExtractAudioFromVideoWorker) NextRetry(job *Job[ExtractAudioFromVideo]) time.Time {
return time.Now().Add(30 * time.Second)
}
func (w *ExtractAudioFromVideoWorker) Work(ctx context.Context, job *Job[ExtractAudioFromVideo]) error {
log := log.WithField("job", job.Args.Kind())
log.Infof("[Start] Working on job with strings: %+v", job.Args)
defer log.Infof("[End] Finished %s", job.Args.Kind())
media, err := models.Medias.GetByID(ctx, job.Args.MediaID)
if err != nil {
log.Errorf("Error getting media by ID: %v", err)
return JobCancel(err)
}
_ = media
// TODO
// path := "/Users/rogee/Projects/self/quyun/backend/fixtures/oss/"
// dst := filepath.Join(path, media.Path)
// // use ffmpeg to extract audio from video
// audioPath := filepath.Join(path, media.Hash+".mp3")
// cmd := exec.Command("ffmpeg", "-i", dst, audioPath)
// if err := cmd.Run(); err != nil {
// log.Errorf("Error extracting audio: %v", err)
// return err
// }
// log.Infof("Successfully extracted audio to: %s", audioPath)
return nil
}

View File

@@ -0,0 +1,115 @@
package jobs
import (
"bufio"
"context"
"os/exec"
"path/filepath"
"time"
"quyun/app/models"
"quyun/providers/ali"
"quyun/providers/app"
"quyun/providers/job"
. "github.com/riverqueue/river"
log "github.com/sirupsen/logrus"
_ "go.ipao.vip/atom"
"go.ipao.vip/atom/contracts"
"golang.org/x/sync/errgroup"
)
var _ contracts.JobArgs = (*VideoCut)(nil)
type VideoCut struct {
MediaID int64 `json:"media_id"`
}
func (s VideoCut) InsertOpts() InsertOpts {
return InsertOpts{
Queue: QueueDefault,
Priority: PriorityDefault,
}
}
func (s VideoCut) Kind() string { return "video_cut" }
func (a VideoCut) UniqueID() string { return a.Kind() }
var _ Worker[VideoCut] = (*VideoCutWorker)(nil)
// @provider(job)
type VideoCutWorker struct {
WorkerDefaults[VideoCut]
oss *ali.OSSClient
job *job.Job
app *app.Config
}
func (w *VideoCutWorker) NextRetry(job *Job[VideoCut]) time.Time {
return time.Now().Add(30 * time.Second)
}
func (w *VideoCutWorker) Work(ctx context.Context, job *Job[VideoCut]) error {
log := log.WithField("job", job.Args.Kind())
log.Infof("[Start] Working on job with strings: %+v", job.Args)
defer log.Infof("[End] Finished %s", job.Args.Kind())
media, err := models.Medias.GetByID(ctx, job.Args.MediaID)
if err != nil {
log.Errorf("Error getting media by ID: %v", err)
return JobCancel(err)
}
input := filepath.Join(w.app.StoragePath, media.Path)
output := input[:len(input)-len(filepath.Ext(input))] + "-output" + 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 != nil {
log.Errorf("Error creating stdout pipe: %v", err)
return 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
})
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
}