feat: snapshot frame_type add 2

This commit is contained in:
Justyer
2025-06-12 11:47:19 +08:00
parent 01b0189c9c
commit 387b74b62e
6 changed files with 79 additions and 18 deletions

View File

@@ -0,0 +1,41 @@
package main
import (
"context"
"fmt"
"github.com/fxkt-tech/liv/ffmpeg"
"github.com/fxkt-tech/liv/ffmpeg/codec"
"github.com/fxkt-tech/liv/ffmpeg/filter"
"github.com/fxkt-tech/liv/ffmpeg/input"
"github.com/fxkt-tech/liv/ffmpeg/output"
)
func main() {
var (
ctx = context.Background()
input1 = input.WithSimple("a1.aac")
input2 = input.WithSimple("a2.aac")
fAmix = filter.AMix(2)
)
err := ffmpeg.New(
ffmpeg.WithLogLevel(""),
ffmpeg.WithDebug(true),
ffmpeg.WithDry(true),
).AddInput(
input1, input2,
).AddFilter(
fAmix,
).
AddOutput(
output.New(
output.Map(fAmix),
output.AudioCodec(codec.AAC),
output.File("out.mp4"),
),
).Run(ctx)
if err != nil {
fmt.Println(err)
}
}

View File

@@ -19,7 +19,7 @@ func main() {
err := ffmpeg.New(
ffmpeg.WithLogLevel(""),
ffmpeg.WithDebug(true),
// ffmpeg.Dry(true),
ffmpeg.WithDry(true),
).AddInput(
input1,
).AddOutput(

View File

@@ -15,8 +15,9 @@ func main() {
Infile: "../../testdata/in.mp4",
Outfile: "ss/simple-%05d.jpg",
StartTime: 0,
// FrameType: 1,
Num: 4,
FrameType: 2,
Frames: []int32{1, 10, 300, 301},
// Num: 4,
// Interval: 5,
Width: 960,
Height: 540,

View File

@@ -82,14 +82,13 @@ func (ff *FFmpeg) DryRun() {
}
func (ff *FFmpeg) Run(ctx context.Context) (err error) {
if ff.debug {
if ff.dry {
ff.DryRun()
return nil
} else if ff.debug {
ff.DryRun()
} else {
if ff.dry {
ff.DryRun()
return nil
}
}
cc := exec.CommandContext(ctx, ff.bin, ff.Params()...)
retbytes, err := cc.CombinedOutput()
if err != nil {

View File

@@ -17,6 +17,7 @@ import (
"github.com/fxkt-tech/liv/ffmpeg/stream"
"github.com/fxkt-tech/liv/ffprobe"
"github.com/fxkt-tech/liv/pkg/math"
"github.com/fxkt-tech/liv/pkg/sugar"
)
type Snapshot struct {
@@ -54,12 +55,12 @@ func (ss *Snapshot) Simple(ctx context.Context, params *SnapshotParams) error {
lastFilter := stream.V(0)
// 使用普通帧截图时,必须要传截图间隔,除非只截一张
switch params.FrameType {
case 0: // 关键帧
case 0: // 关键帧截图
selectFilter := filter.Select("'eq(pict_type,I)'")
filters = append(filters, selectFilter)
lastFilter = selectFilter
outputOptions = append(outputOptions, output.VSync("vfr"))
case 1:
case 1: // 等间隔截图
if params.Num != 1 {
if params.IntervalFrames > 0 {
selectFilter := filter.Select(fmt.Sprintf("'not(mod(n,%d))'", params.IntervalFrames))
@@ -72,6 +73,17 @@ func (ss *Snapshot) Simple(ctx context.Context, params *SnapshotParams) error {
lastFilter = fpsFilter
}
}
case 2: // 指定帧序列截图
if len(params.Frames) > 0 {
selectExpr := fmt.Sprintf("'%s'",
strings.Join(sugar.Map(params.Frames, func(frame int32) string {
return fmt.Sprintf("eq(n,%d)", frame)
}), "+"))
selectFilter := filter.Select(selectExpr)
filters = append(filters, selectFilter)
lastFilter = selectFilter
outputOptions = append(outputOptions, output.VSync("vfr"))
}
}
if params.Width > 0 || params.Height > 0 {
scaleFilter := filter.Scale(params.Width, params.Height).Use(lastFilter)

View File

@@ -1,15 +1,23 @@
package liv
type SnapshotParams struct {
Infile string
Outfile string
StartTime float32
Infile string
Outfile string
// 0-仅关键帧截图 1-等间隔截图 2-指定帧序列截图
FrameType int32 // 截图类型
// 等间隔截图
Interval int32 // 间隔时间
IntervalFrames int32 // 间隔帧数
Num int32
FrameType int32 // 0-仅关键帧 1-指定时间点的帧
Width int32
Height int32
// 指定帧序列截图
Frames []int32 // 帧序列
StartTime float32 // 截图开始时间。截图类型为指定帧序列截图时不建议使用此参数
Num int32 // 截图最大数量
Width int32
Height int32
}
type SpriteParams struct {