quality_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package transcoder
  2. import (
  3. "os/exec"
  4. "path/filepath"
  5. "strings"
  6. "testing"
  7. )
  8. // TestParseTranscodeResolution covers the request parameter mapping, including
  9. // the fall back to the source resolution for anything unknown.
  10. func TestParseTranscodeResolution(t *testing.T) {
  11. cases := []struct {
  12. value string
  13. want TranscodeOutputResolution
  14. }{
  15. {"360p", TranscodeResolution_360p},
  16. {"480p", TranscodeResolution_480p},
  17. {"720p", TranscodeResolution_720p},
  18. {"1080p", TranscodeResolution_1080p},
  19. {" 720P ", TranscodeResolution_720p},
  20. {"", TranscodeResolution_original},
  21. {"original", TranscodeResolution_original},
  22. {"1280p", TranscodeResolution_original},
  23. {"240p", TranscodeResolution_original},
  24. {"banana", TranscodeResolution_original},
  25. }
  26. for _, tc := range cases {
  27. t.Run(tc.value, func(t *testing.T) {
  28. if got := ParseTranscodeResolution(tc.value); got != tc.want {
  29. t.Errorf("ParseTranscodeResolution(%q) = %q, want %q", tc.value, got, tc.want)
  30. }
  31. })
  32. }
  33. }
  34. // TestResolutionScaleFilter covers the shared filter used by both the MP4 and
  35. // the HLS path, including the rejection that guards the endpoint.
  36. func TestResolutionScaleFilter(t *testing.T) {
  37. cases := []struct {
  38. resolution TranscodeOutputResolution
  39. want string
  40. wantErr bool
  41. }{
  42. {TranscodeResolution_original, "", false},
  43. {TranscodeResolution_360p, "min(1,min(640/max(iw,ih),360/min(iw,ih)))", false},
  44. {TranscodeResolution_1080p, "min(1,min(1920/max(iw,ih),1080/min(iw,ih)))", false},
  45. {"banana", "", true},
  46. }
  47. for _, tc := range cases {
  48. t.Run(string(tc.resolution), func(t *testing.T) {
  49. got, err := resolutionScaleFilter(tc.resolution)
  50. if tc.wantErr {
  51. if err == nil {
  52. t.Fatalf("resolutionScaleFilter(%q) returned no error, want one", tc.resolution)
  53. }
  54. return
  55. }
  56. if err != nil {
  57. t.Fatalf("resolutionScaleFilter(%q) returned error: %v", tc.resolution, err)
  58. }
  59. if tc.want == "" {
  60. if got != "" {
  61. t.Errorf("resolutionScaleFilter(%q) = %q, want no filter", tc.resolution, got)
  62. }
  63. return
  64. }
  65. if !strings.HasPrefix(got, "scale=") || !strings.Contains(got, tc.want) {
  66. t.Errorf("resolutionScaleFilter(%q) = %q, want a scale filter using %q", tc.resolution, got, tc.want)
  67. }
  68. })
  69. }
  70. }
  71. // TestResolutionScaleFilterOutput runs the filter through ffmpeg to check the
  72. // sizes it produces: fitted to the box, turned for portrait sources, never
  73. // enlarged and always even. Skipped where ffmpeg is not installed.
  74. func TestResolutionScaleFilterOutput(t *testing.T) {
  75. if _, err := exec.LookPath("ffmpeg"); err != nil {
  76. t.Skip("ffmpeg not installed")
  77. }
  78. if _, err := exec.LookPath("ffprobe"); err != nil {
  79. t.Skip("ffprobe not installed")
  80. }
  81. cases := []struct {
  82. name string
  83. source string
  84. resolution TranscodeOutputResolution
  85. want string
  86. }{
  87. {"widescreen down", "1920x1080", TranscodeResolution_720p, "1280,720"},
  88. {"scope film keeps width", "1920x800", TranscodeResolution_1080p, "1920,800"},
  89. {"scope film down", "1920x800", TranscodeResolution_720p, "1280,532"},
  90. {"portrait", "1080x1920", TranscodeResolution_720p, "720,1280"},
  91. {"four by three", "1440x1080", TranscodeResolution_480p, "640,480"},
  92. {"never upscales", "720x480", TranscodeResolution_1080p, "720,480"},
  93. {"odd width made even", "853x480", TranscodeResolution_480p, "852,480"},
  94. }
  95. for _, tc := range cases {
  96. t.Run(tc.name, func(t *testing.T) {
  97. scale, err := resolutionScaleFilter(tc.resolution)
  98. if err != nil {
  99. t.Fatalf("resolutionScaleFilter(%q) returned error: %v", tc.resolution, err)
  100. }
  101. out := filepath.Join(t.TempDir(), "out.mp4")
  102. encode := exec.Command("ffmpeg", "-v", "error",
  103. "-f", "lavfi", "-i", "color=c=black:s="+tc.source+":d=0.1",
  104. "-vf", scale, "-frames:v", "1", "-vcodec", "libx264", "-pix_fmt", "yuv420p", "-y", out)
  105. if output, err := encode.CombinedOutput(); err != nil {
  106. t.Fatalf("ffmpeg failed: %v\n%s", err, output)
  107. }
  108. probe, err := exec.Command("ffprobe", "-v", "quiet", "-select_streams", "v:0",
  109. "-show_entries", "stream=width,height", "-of", "csv=p=0", out).Output()
  110. if err != nil {
  111. t.Fatalf("ffprobe failed: %v", err)
  112. }
  113. if got := strings.TrimSpace(string(probe)); got != tc.want {
  114. t.Errorf("%s at %s = %s, want %s", tc.source, tc.resolution, got, tc.want)
  115. }
  116. })
  117. }
  118. }
  119. // TestRateControlArgs checks the bitrate ceiling each path gets.
  120. func TestRateControlArgs(t *testing.T) {
  121. cases := []struct {
  122. name string
  123. resolution TranscodeOutputResolution
  124. hardware bool
  125. want string
  126. }{
  127. {"original software", TranscodeResolution_original, false, ""},
  128. {"original hardware", TranscodeResolution_original, true, ""},
  129. {"480p software", TranscodeResolution_480p, false, "-crf 23 -maxrate 1400k -bufsize 2800k"},
  130. {"720p hardware", TranscodeResolution_720p, true, "-b:v 2100k -maxrate 2800k -bufsize 5600k"},
  131. }
  132. for _, tc := range cases {
  133. t.Run(tc.name, func(t *testing.T) {
  134. got := strings.Join(rateControlArgs(tc.resolution, tc.hardware), " ")
  135. if got != tc.want {
  136. t.Errorf("rateControlArgs(%q, %v) = %q, want %q", tc.resolution, tc.hardware, got, tc.want)
  137. }
  138. })
  139. }
  140. }