audio_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package audio_test
  2. import (
  3. "testing"
  4. "github.com/labring/aiproxy/core/common/audio"
  5. "github.com/smartystreets/goconvey/convey"
  6. )
  7. func TestParseTimeFromFfmpegOutput(t *testing.T) {
  8. convey.Convey("parseTimeFromFfmpegOutput", t, func() {
  9. convey.Convey("should parse valid duration", func() {
  10. output := "size=N/A time=00:00:05.52 bitrate=N/A speed= 785x"
  11. duration, err := audio.ParseTimeFromFfmpegOutput(output)
  12. convey.So(err, convey.ShouldBeNil)
  13. convey.So(duration, convey.ShouldAlmostEqual, 5.52)
  14. })
  15. convey.Convey("should parse longer duration", func() {
  16. output := "frame= 100 fps=0.0 q=-0.0 size= 123kB time=01:02:03.45 bitrate= 10.0kbits/s speed= 10x"
  17. duration, err := audio.ParseTimeFromFfmpegOutput(output)
  18. convey.So(err, convey.ShouldBeNil)
  19. // 1*3600 + 2*60 + 3.45 = 3600 + 120 + 3.45 = 3723.45
  20. convey.So(duration, convey.ShouldAlmostEqual, 3723.45)
  21. })
  22. convey.Convey("should use last match", func() {
  23. output := "time=00:00:01.00\n... time=00:00:02.00"
  24. duration, err := audio.ParseTimeFromFfmpegOutput(output)
  25. convey.So(err, convey.ShouldBeNil)
  26. convey.So(duration, convey.ShouldAlmostEqual, 2.0)
  27. })
  28. convey.Convey("should return error for no time match", func() {
  29. output := "invalid output"
  30. _, err := audio.ParseTimeFromFfmpegOutput(output)
  31. convey.So(err, convey.ShouldNotBeNil)
  32. convey.So(err, convey.ShouldEqual, audio.ErrAudioDurationNAN)
  33. })
  34. })
  35. }