mirror of
https://github.com/hsnks100/liveflow.git
synced 2025-12-24 12:47:59 +08:00
61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
package processes
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"image"
|
|
"image/jpeg"
|
|
"liveflow/media/streamer/pipe"
|
|
"os"
|
|
|
|
astiav "github.com/asticode/go-astiav"
|
|
)
|
|
|
|
type DumpProcess struct {
|
|
pipe.BaseProcess[[]*astiav.Frame, interface{}]
|
|
index int
|
|
i image.Image
|
|
}
|
|
|
|
func NewDumpProcess() *DumpProcess {
|
|
return &DumpProcess{}
|
|
}
|
|
|
|
func (v *DumpProcess) Init() error {
|
|
return nil
|
|
}
|
|
func (v *DumpProcess) Process(data []*astiav.Frame) (interface{}, error) {
|
|
// Decode data
|
|
ctx := context.Background()
|
|
_ = ctx
|
|
for _, frame := range data {
|
|
filename := fmt.Sprintf("frame_%d.jpg", v.index)
|
|
v.index++
|
|
fd := frame.Data()
|
|
if v.i == nil {
|
|
var err error
|
|
v.i, err = fd.GuessImageFormat()
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return nil, err
|
|
}
|
|
}
|
|
fd.ToImage(v.i)
|
|
saveAsJPEG(v.i, filename)
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
func saveAsJPEG(img image.Image, filename string) error {
|
|
file, err := os.Create(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
options := &jpeg.Options{
|
|
Quality: 90, // JPEG 품질 (1~100)
|
|
}
|
|
return jpeg.Encode(file, img, options)
|
|
}
|