78 lines
2.6 KiB
Go
78 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
type annotationFrameSlice struct {
|
|
Start, End int
|
|
Output string
|
|
}
|
|
|
|
// Use the same frame clock as annotationBoundaryGrid and exported stereo metadata.
|
|
// Nominal FPS is insufficient: real source MP4s can contain repeated media PTS.
|
|
func annotationOutputClock(ctx context.Context, source string) (string, string, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return "", "", err
|
|
}
|
|
count, err := ffprobeDeclaredFrameCount(ctx, source)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
err = ctx.Err()
|
|
}
|
|
return "", "", err
|
|
}
|
|
duration, err := ffprobeDurationSeconds(ctx, source)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
err = ctx.Err()
|
|
}
|
|
return "", "", err
|
|
}
|
|
micros := int64(math.Round(duration * 1e6))
|
|
if micros <= 0 {
|
|
return "", "", fmt.Errorf("invalid source frame clock")
|
|
}
|
|
return fmt.Sprintf("settb=1/1000000,setpts=N*%d/%d", micros, count), fmt.Sprintf("setts=pts=PTS:dts=DTS:duration=%d/%d", micros, count), nil
|
|
}
|
|
|
|
// Decode each input once per bounded group. Rebuild both frame timestamps and
|
|
// packet durations on the shared clock; passthrough must not add/drop frames.
|
|
func writeFrameAccurateSlices(ctx context.Context, source string, slices []annotationFrameSlice) error {
|
|
if len(slices) == 0 {
|
|
return nil
|
|
}
|
|
clock, packetClock, err := annotationOutputClock(ctx, source)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for offset := 0; offset < len(slices); offset += 8 {
|
|
group := slices[offset:min(offset+8, len(slices))]
|
|
filters := []string{}
|
|
split := fmt.Sprintf("[0:v]split=%d", len(group))
|
|
for i := range group {
|
|
split += fmt.Sprintf("[in%d]", i)
|
|
}
|
|
filters = append(filters, split)
|
|
for i, s := range group {
|
|
if s.Start < 0 || s.End <= s.Start {
|
|
return fmt.Errorf("invalid frame slice")
|
|
}
|
|
filters = append(filters, fmt.Sprintf("[in%d]trim=start_frame=%d:end_frame=%d,%s[out%d]", i, s.Start, s.End, clock, i))
|
|
}
|
|
args := []string{"-nostdin", "-hide_banner", "-loglevel", "error", "-y", "-threads", "2", "-i", source, "-filter_complex_threads", "1", "-filter_complex", strings.Join(filters, ";")}
|
|
for i, s := range group {
|
|
args = append(args, "-map", fmt.Sprintf("[out%d]", i), "-an", "-c:v", "libx264", "-threads", "2", "-preset", "ultrafast", "-crf", "23", "-pix_fmt", "yuv420p", "-enc_time_base", "1:1000000", "-video_track_timescale", "1000000", "-fps_mode", "passthrough", "-bsf:v", packetClock, s.Output)
|
|
}
|
|
out, e := exec.CommandContext(ctx, "ffmpeg", args...).CombinedOutput()
|
|
if e != nil {
|
|
return fmt.Errorf("batch frame slicing: %w: %s", e, out)
|
|
}
|
|
}
|
|
return nil
|
|
}
|