forked from longtao.wu/droboticflow
158 lines
5.7 KiB
Go
158 lines
5.7 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Catch encoder synchronization dropping/duplicating selected source frames,
|
|
// including the second batch of eight outputs and non-zero slice starts.
|
|
func TestFrameSlicesPreserveFramesWithIrregularMediaPTS(t *testing.T) {
|
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
|
t.Skip("ffmpeg required")
|
|
}
|
|
root := t.TempDir()
|
|
source := filepath.Join(root, "irregular.mp4")
|
|
cmd := exec.Command("ffmpeg", "-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=96x64:rate=30:duration=3", "-vf", "setpts=PTS-floor(N/20)/(30*TB)", "-fps_mode", "passthrough", "-c:v", "libx264", "-preset", "ultrafast", source)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("fixture: %v: %s", err, out)
|
|
}
|
|
if n, err := ffprobeFrameCount(context.Background(), source); err != nil || n != 90 {
|
|
t.Fatalf("fixture frames=%d err=%v", n, err)
|
|
}
|
|
for _, mode := range []string{"single", "batch"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
cuts := []annotationFrameSlice{{0, 89, filepath.Join(root, mode+"-long.mp4")}}
|
|
for i := 0; i < 9; i++ {
|
|
cuts = append(cuts, annotationFrameSlice{i * 10, (i + 1) * 10, filepath.Join(root, fmt.Sprintf("%s-%d.mp4", mode, i))})
|
|
}
|
|
if mode == "batch" {
|
|
if err := writeFrameAccurateSlices(context.Background(), source, cuts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else {
|
|
for _, c := range cuts {
|
|
if err := writeFrameAccurateSlice(context.Background(), source, c.Output, c.Start, c.End); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
for i, c := range cuts {
|
|
want := 10
|
|
if i == 0 {
|
|
want = 89
|
|
}
|
|
got, err := ffprobeFrameCount(context.Background(), c.Output)
|
|
if err != nil || got != want {
|
|
t.Errorf("slice %d: decoded frames=%d want=%d err=%v", i, got, want, err)
|
|
continue
|
|
}
|
|
out, err := exec.Command("ffprobe", "-v", "error", "-select_streams", "v:0", "-show_frames", "-show_entries", "frame=best_effort_timestamp_time", "-of", "json", c.Output).Output()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var frames struct {
|
|
Frames []struct {
|
|
Time string `json:"best_effort_timestamp_time"`
|
|
} `json:"frames"`
|
|
}
|
|
if err = json.Unmarshal(out, &frames); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(frames.Frames) != want {
|
|
t.Fatalf("decoded timestamp rows=%d want=%d", len(frames.Frames), want)
|
|
}
|
|
for n, f := range frames.Frames {
|
|
v, e := strconv.ParseFloat(f.Time, 64)
|
|
if e != nil || math.Abs(v-float64(n)*2.866667/90) > 0.000002 {
|
|
t.Errorf("slice %d frame %d: timestamp=%s want=%f", i, n, f.Time, float64(n)*2.866667/90)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIrregularStereoMaterializationKeepsExportedClockAligned(t *testing.T) {
|
|
f, root := annotationProcessorFixture(t)
|
|
for _, side := range []string{"left", "right"} {
|
|
step := "6"
|
|
if side == "right" {
|
|
step = "8"
|
|
}
|
|
out, err := exec.Command("ffmpeg", "-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=size=64x48:rate=8:duration=4", "-vf", "setpts=PTS-floor(N/"+step+")/(8*TB)", "-fps_mode", "passthrough", "-c:v", "libx264", "-preset", "ultrafast", filepath.Join(root, side+".mp4")).CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("%v: %s", err, out)
|
|
}
|
|
}
|
|
p := &server{store: f.store, workspaceMountPath: root}
|
|
result, err := p.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, combinedAnnotationSubmissionInput{SliceSegments: []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 1.6875}, {ID: "b", Name: "测试切片", Start: 1.6875, End: 3.375}}, Segments: []domain.AnnotationSegment{{ID: "desc", Description: "test", Start: 0, End: 1.6875}, {ID: "desc2", Description: "test", Start: 1.6875, End: 3.375}}}, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range []string{"slice-000001", "slice-000002"} {
|
|
dir := filepath.Join(result.Artifact.Path, "slices", id)
|
|
data, err := os.ReadFile(filepath.Join(dir, "synchronized-slice.json"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var sync struct {
|
|
Left []float64 `json:"boundarySeconds"`
|
|
Right []float64 `json:"rightBoundarySeconds"`
|
|
}
|
|
if err = json.Unmarshal(data, &sync); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, side := range []string{"left", "right"} {
|
|
boundaries, wantDuration := sync.Left, 1.6875
|
|
if side == "right" {
|
|
boundaries, wantDuration = sync.Right, 1.8125
|
|
}
|
|
if len(boundaries) != 17 {
|
|
t.Fatalf("%s boundary count=%d", side, len(boundaries))
|
|
}
|
|
v := filepath.Join(dir, side+".mp4")
|
|
count, err := ffprobeFrameCount(f.ctx, v)
|
|
if err != nil || count != 16 {
|
|
t.Fatalf("%s decoded=%d err=%v", side, count, err)
|
|
}
|
|
dur, err := ffprobeDurationSeconds(f.ctx, v)
|
|
if err != nil || math.Abs(dur-wantDuration) > 0.000002 {
|
|
t.Fatalf("%s duration=%f want=%f err=%v", side, dur, wantDuration, err)
|
|
}
|
|
output, err := exec.Command("ffprobe", "-v", "error", "-select_streams", "v:0", "-show_frames", "-show_entries", "frame=best_effort_timestamp_time", "-of", "json", v).Output()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var decoded struct {
|
|
Frames []struct {
|
|
Time string `json:"best_effort_timestamp_time"`
|
|
} `json:"frames"`
|
|
}
|
|
if err = json.Unmarshal(output, &decoded); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for n, frame := range decoded.Frames {
|
|
stamp, err := strconv.ParseFloat(frame.Time, 64)
|
|
if err != nil || math.Abs(stamp-boundaries[n]) > 0.000002 {
|
|
t.Fatalf("%s frame %d timestamp=%s metadata=%f", side, n, frame.Time, boundaries[n])
|
|
}
|
|
}
|
|
pts, err := os.ReadFile(filepath.Join(dir, side+"_pts.csv"))
|
|
if err != nil || len(strings.Split(strings.TrimSpace(string(pts)), "\n")) != 17 {
|
|
t.Fatalf("PTS count mismatch: %v", err)
|
|
}
|
|
}
|
|
}
|
|
}
|