Skip to content

Commit 558bc6b

Browse files
authored
Merge pull request #388 from bcc-code/feature/bcc-108-ffmpeg-scene-change-detection-for-short-generation
get scene change frames with ffmpeg & send them to yolo
2 parents b9c52d8 + 9826c10 commit 558bc6b

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

activities/crop_shorts.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package activities
33
import (
44
"context"
55
"fmt"
6-
"github.com/bcc-code/bcc-media-flows/paths"
76
"math"
87
"strconv"
98
"strings"
109

10+
"github.com/bcc-code/bcc-media-flows/paths"
11+
1112
"github.com/bcc-code/bcc-media-flows/services/ffmpeg"
1213
)
1314

@@ -125,7 +126,7 @@ func calculatePanDuration(distance float64) float64 {
125126
const (
126127
minDur = 0.1
127128
maxDur = 3.0
128-
speed = 200.0
129+
speed = 100.0
129130
)
130131
d := distance / speed
131132
if d < minDur {

activities/shorts.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import (
1212
var shortServiceURL = os.Getenv("SHORTS_SERVICE_URL")
1313

1414
type SubmitShortJobInput struct {
15-
InputPath string `json:"input_path"`
16-
OutputPath string `json:"output_path"`
17-
Model string `json:"model"`
18-
Debug bool `json:"debug"`
15+
InputPath string `json:"input_path"`
16+
OutputPath string `json:"output_path"`
17+
Model string `json:"model"`
18+
Debug bool `json:"debug"`
19+
SceneChanges []float64 `json:"scene_changes"`
1920
}
2021

2122
type Square struct {

activities/transcode.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package activities
33
import (
44
"context"
55
"fmt"
6+
"regexp"
7+
"strconv"
68

79
"github.com/bcc-code/bcc-media-flows/utils"
810

@@ -323,6 +325,33 @@ func (va VideoActivities) ExecuteFFmpeg(ctx context.Context, input ExecuteFFmpeg
323325
return nil, err
324326
}
325327

328+
func (va VideoActivities) ExecuteFFmpegDump(
329+
ctx context.Context,
330+
input ExecuteFFmpegInput,
331+
) ([]float64, error) {
332+
333+
stopChan, progressCallback := registerProgressCallback(ctx)
334+
defer close(stopChan)
335+
336+
out, err := ffmpeg.Do(input.Arguments, ffmpeg.StreamInfo{}, progressCallback)
337+
if err != nil {
338+
return nil, err
339+
}
340+
341+
raw := string(out)
342+
re := regexp.MustCompile(`(?m)pts_time:([\d.]+)`)
343+
matches := re.FindAllStringSubmatch(raw, -1)
344+
345+
var changes []float64
346+
for _, m := range matches {
347+
if len(m) >= 2 {
348+
t, _ := strconv.ParseFloat(m[1], 64)
349+
changes = append(changes, t)
350+
}
351+
}
352+
return changes, nil
353+
}
354+
326355
type SplitAudioChannelsInput struct {
327356
FilePath paths.Path
328357
OutputDir paths.Path

utils/workflows/files.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package wfutils
33
import (
44
"encoding/xml"
55
"fmt"
6+
"path/filepath"
7+
"strings"
8+
"time"
9+
610
"github.com/bcc-code/bcc-media-flows/services/notifications"
711
"github.com/bcc-code/bcc-media-flows/services/rclone"
812
"github.com/bcc-code/bcc-media-flows/services/telegram"
913
"go.temporal.io/sdk/temporal"
10-
"path/filepath"
11-
"strings"
12-
"time"
1314

1415
"github.com/bcc-code/bcc-media-flows/activities"
1516
"github.com/bcc-code/bcc-media-flows/environment"

workflows/export/generate_short.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package export
22

33
import (
44
"fmt"
5-
vsactivity "github.com/bcc-code/bcc-media-flows/activities/vidispine"
65
"strings"
76
"time"
87

8+
vsactivity "github.com/bcc-code/bcc-media-flows/activities/vidispine"
9+
910
"github.com/bcc-code/bcc-media-flows/activities"
1011
"github.com/bcc-code/bcc-media-flows/paths"
1112
"github.com/bcc-code/bcc-media-flows/services/vidispine"
@@ -28,6 +29,8 @@ type GenerateShortDataParams struct {
2829
OutputDirPath string `json:"OutputDir"`
2930
InSeconds float64 `json:"InSeconds"`
3031
OutSeconds float64 `json:"OutSeconds"`
32+
ModelSize string `json:"ModelSize"`
33+
DebugMode bool `json:"DebugMode"`
3134
}
3235

3336
func validationError(msg string) error {
@@ -109,11 +112,27 @@ func GenerateShort(ctx workflow.Context, params GenerateShortDataParams) (*Gener
109112
return nil, err
110113
}
111114

115+
sceneDetectArgs := []string{
116+
"-i", clipResult.VideoFile.Local(),
117+
"-filter_complex", "select='gt(scene,0.1)',metadata=print:file=-",
118+
"-f", "null", "-",
119+
}
120+
121+
sceneResult, err := wfutils.Execute(ctx,
122+
activities.Video.ExecuteFFmpegDump,
123+
activities.ExecuteFFmpegInput{Arguments: sceneDetectArgs},
124+
).Result(ctx)
125+
if err != nil {
126+
logger.Error("Scene-detect FFmpeg failed: " + err.Error())
127+
return nil, err
128+
}
129+
112130
submitJobParams := activities.SubmitShortJobInput{
113-
InputPath: clipResult.VideoFile.Linux(),
114-
OutputPath: tempFolder.Linux(),
115-
Model: "n",
116-
Debug: true,
131+
InputPath: clipResult.VideoFile.Linux(),
132+
OutputPath: tempFolder.Linux(),
133+
Model: params.ModelSize,
134+
Debug: params.DebugMode,
135+
SceneChanges: sceneResult,
117136
}
118137

119138
jobResult, err := wfutils.Execute(ctx, activities.Util.SubmitShortJobActivity, submitJobParams).Result(ctx)

workflows/misc/ffmpeg.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,22 @@ func ExecuteFFmpeg(
2525
Arguments: params.Arguments,
2626
}).Wait(ctx)
2727

28-
if err != nil {
29-
return err
30-
}
31-
3228
return err
3329
}
30+
31+
func ExecuteFFmpegDump(
32+
ctx workflow.Context,
33+
params ExecuteFFmpegInput,
34+
) (string, error) {
35+
logger := workflow.GetLogger(ctx)
36+
logger.Info("Starting ExecuteFFmpegDump")
37+
38+
ctx = workflow.WithActivityOptions(ctx, wfutils.GetDefaultActivityOptions())
39+
40+
var out string
41+
err := wfutils.Execute(ctx, activities.Video.ExecuteFFmpegDump,
42+
activities.ExecuteFFmpegInput{Arguments: params.Arguments},
43+
).Get(ctx, &out)
44+
45+
return out, err
46+
}

0 commit comments

Comments
 (0)