Skip to content

Commit

Permalink
release: updated probe by adding the format with the actual duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Paxx committed May 30, 2023
1 parent a414de7 commit acc48dc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
16 changes: 2 additions & 14 deletions ffprobe/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,8 @@ func (p *Probe) GetResolution() (width int, height int) {
return videoStream.Width, videoStream.Height
}

func (p *Probe) GetStreamDuration(codecType ...string) (float64, error) {
selectedCodec := "video"
if len(codecType) > 0 {
selectedCodec = codecType[0]
}
var selected Stream
for _, stream := range p.Streams {
if stream.CodecType == selectedCodec {
selected = stream
break
}
}

duration, err := strconv.ParseFloat(selected.Duration, 64)
func (p *Probe) GetStreamDuration() (float64, error) {
duration, err := strconv.ParseFloat(p.Format.Duration, 64)
if err != nil {
return -1, err
}
Expand Down
1 change: 1 addition & 0 deletions ffprobe/ffprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (p *ffprobe) GetProbe(args ...string) (*Probe, error) {
cmd := exec.Command(p.configuration.FfprobePath)
cArgs := []string{
"-show_streams",
"-show_format",
"-print_format", "json",
}

Expand Down
13 changes: 13 additions & 0 deletions ffprobe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,21 @@ import (

type Probe struct {
Streams []Stream `json:"streams"`
Format Format `json:"format"`
}

type Format struct {
Filename string `json:"filename"`
NbStreams int `json:"nb_streams"`
NbPrograms int `json:"nb_programs"`
FormatName string `json:"format_name"`
FormatLongName string `json:"format_long_name"`
StartTime string `json:"start_time"`
Duration string `json:"duration"`
Size string `json:"size"`
BitRate string `json:"bit_rate"`
ProbeScore int `json:"probe_score"`
}
type Stream struct {
Index int `json:"index"`
CodecName string `json:"codec_name"`
Expand Down
17 changes: 17 additions & 0 deletions test/ffprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ func Test_FfprobeVideoStreams(t *testing.T) {
}
}

func Test_FfprobeVideoStreamsGetDuration(t *testing.T) {
ffprobe := GetFfprobe()
probe, err := ffprobe.GetProbe(input)
if err != nil {
panic(fmt.Sprintf("could not probe %s", err.Error()))
}

d, err := probe.GetStreamDuration()
if err != nil {
panic(fmt.Sprintf("could not get duration %s", err.Error()))
}

if d < 0 {
panic(fmt.Sprintf("could not get duration -1 %s", err.Error()))
}
}

func Test_FfprobeAudioStreams(t *testing.T) {
expected := 1
expectedAudio := "audio"
Expand Down

0 comments on commit acc48dc

Please sign in to comment.