mirror of
https://github.com/u2takey/ffmpeg-go.git
synced 2025-09-26 20:11:17 +08:00

Discover CommandOption type and ffmpeg.GlobalCommandOptions array, that is processed before running ffmpeg or ffprobe commands. Add tests.
46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
package ffmpeg_go
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"os/exec"
|
|
"time"
|
|
)
|
|
|
|
// Probe Run ffprobe on the specified file and return a JSON representation of the output.
|
|
func Probe(fileName string, kwargs ...KwArgs) (string, error) {
|
|
return ProbeWithTimeout(fileName, 0, MergeKwArgs(kwargs))
|
|
}
|
|
|
|
func ProbeWithTimeout(fileName string, timeOut time.Duration, kwargs KwArgs) (string, error) {
|
|
args := KwArgs{
|
|
"show_format": "",
|
|
"show_streams": "",
|
|
"of": "json",
|
|
}
|
|
|
|
return ProbeWithTimeoutExec(fileName, timeOut, MergeKwArgs([]KwArgs{args, kwargs}))
|
|
}
|
|
|
|
func ProbeWithTimeoutExec(fileName string, timeOut time.Duration, kwargs KwArgs) (string, error) {
|
|
args := ConvertKwargsToCmdLineArgs(kwargs)
|
|
args = append(args, fileName)
|
|
ctx := context.Background()
|
|
if timeOut > 0 {
|
|
var cancel func()
|
|
ctx, cancel = context.WithTimeout(context.Background(), timeOut)
|
|
defer cancel()
|
|
}
|
|
cmd := exec.CommandContext(ctx, "ffprobe", args...)
|
|
buf := bytes.NewBuffer(nil)
|
|
cmd.Stdout = buf
|
|
for _, option := range GlobalCommandOptions {
|
|
option(cmd)
|
|
}
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(buf.Bytes()), nil
|
|
}
|