Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,9 @@ func (s *server) SendVideo() http.HandlerFunc {
Id string
JPEGThumbnail []byte
MimeType string
Seconds uint32 `json:"Seconds,omitempty"`
Width uint32 `json:"Width,omitempty"`
Height uint32 `json:"Height,omitempty"`
ContextInfo waE2E.ContextInfo
QuotedMessage *waE2E.Message `json:"QuotedMessage,omitempty"`
}
Expand Down Expand Up @@ -1571,6 +1574,19 @@ func (s *server) SendVideo() http.HandlerFunc {
return
}

// Extract video metadata (duration, dimensions) via ffprobe.
// If caller provided Seconds/Width/Height in payload, those take priority.
videoMeta := getVideoMetadata(filedata)
if t.Seconds > 0 {
videoMeta.DurationSeconds = t.Seconds
}
if t.Width > 0 {
videoMeta.Width = t.Width
}
if t.Height > 0 {
videoMeta.Height = t.Height
}

msg := &waE2E.Message{VideoMessage: &waE2E.VideoMessage{
Caption: proto.String(t.Caption),
URL: proto.String(uploaded.URL),
Expand All @@ -1586,6 +1602,9 @@ func (s *server) SendVideo() http.HandlerFunc {
FileSHA256: uploaded.FileSHA256,
FileLength: proto.Uint64(uint64(len(filedata))),
JPEGThumbnail: t.JPEGThumbnail,
Seconds: proto.Uint32(videoMeta.DurationSeconds),
Width: proto.Uint32(videoMeta.Width),
Height: proto.Uint32(videoMeta.Height),
}}

if t.ContextInfo.StanzaID != nil {
Expand Down
76 changes: 76 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os/exec"
"regexp"
"runtime/debug"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -772,6 +773,81 @@ func runFFmpegConversion(input []byte, inputExt string, ffmpegArgs func(inPath,
return os.ReadFile(outPath)
}

// VideoMetadata holds extracted video properties (duration, dimensions).
type VideoMetadata struct {
DurationSeconds uint32
Width uint32
Height uint32
}

// getVideoMetadata uses ffprobe to extract duration, width, and height from video data.
// Returns zero values if extraction fails (graceful degradation).
func getVideoMetadata(filedata []byte) VideoMetadata {
meta := VideoMetadata{}

cmd := exec.Command("ffprobe",
"-v", "quiet",
"-print_format", "json",
"-show_format",
"-show_streams",
"-select_streams", "v:0",
"-i", "-",
)
cmd.Stdin = bytes.NewReader(filedata)

var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr

if err := cmd.Run(); err != nil {
log.Warn().Err(err).Str("stderr", stderr.String()).Msg("getVideoMetadata: ffprobe failed")
return meta
}

var probeResult struct {
Streams []struct {
Width int `json:"width"`
Height int `json:"height"`
Duration string `json:"duration"`
} `json:"streams"`
Format struct {
Duration string `json:"duration"`
} `json:"format"`
}

if err := json.Unmarshal(stdout.Bytes(), &probeResult); err != nil {
log.Warn().Err(err).Msg("getVideoMetadata: failed to parse ffprobe output")
return meta
}

var durationStr string
if len(probeResult.Streams) > 0 {
stream := probeResult.Streams[0]
meta.Width = uint32(stream.Width)
meta.Height = uint32(stream.Height)
durationStr = stream.Duration
}

// Fallback to format-level duration if stream duration was not available
if durationStr == "" && probeResult.Format.Duration != "" {
durationStr = probeResult.Format.Duration
}

if durationStr != "" {
if dur, err := strconv.ParseFloat(durationStr, 64); err == nil && dur > 0 {
meta.DurationSeconds = uint32(dur + 0.5)
}
}

log.Debug().
Uint32("duration", meta.DurationSeconds).
Uint32("width", meta.Width).
Uint32("height", meta.Height).
Msg("getVideoMetadata: extracted video metadata")

return meta
}

func convertVideoStickerToWebP(input []byte) ([]byte, error) {
return runFFmpegConversion(input, ".mp4", func(inPath, outPath string) []string {
return []string{
Expand Down