| 1 | package ffprobe |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "os" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | testPath = "assets/test.mp4" |
| 15 | testPathError = "assets/test.avi" |
| 16 | ) |
| 17 | |
| 18 | func Test_ProbeURL(t *testing.T) { |
| 19 | ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second) |
| 20 | defer cancelFn() |
| 21 | |
| 22 | data, err := ProbeURL(ctx, testPath) |
| 23 | if err != nil { |
| 24 | t.Errorf("Error getting data: %v", err) |
| 25 | } |
| 26 | |
| 27 | validateData(t, data) |
| 28 | } |
| 29 | |
| 30 | func Test_ProbeURL_Error(t *testing.T) { |
| 31 | ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second) |
| 32 | defer cancelFn() |
| 33 | |
| 34 | _, err := ProbeURL(ctx, testPathError, "-loglevel", "error") |
| 35 | if err == nil { |
| 36 | t.Errorf("No error reading bad asset") |
| 37 | } |
| 38 | |
| 39 | if strings.Contains(err.Error(), "[]") { |
| 40 | t.Errorf("No stderr included in error message") |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func Test_ProbeURL_HTTP(t *testing.T) { |
| 45 | const testPort = 20811 |
| 46 | |
| 47 | ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second) |
| 48 | defer cancelFn() |
| 49 | |
| 50 | // Serve all files in assets |
| 51 | go func() { |
| 52 | http.Handle("/", http.FileServer(http.Dir("./assets"))) |
| 53 | err := http.ListenAndServe(fmt.Sprintf(":%d", testPort), nil) //nolint:gosec |
| 54 | t.Log(err) |
| 55 | }() |
| 56 | |
| 57 | // Make sure HTTP is up |
| 58 | time.Sleep(time.Second) |
| 59 | |
| 60 | data, err := ProbeURL(ctx, fmt.Sprintf("http://127.0.0.1:%d/test.mp4", testPort)) |
| 61 | if err != nil { |
| 62 | t.Errorf("Error getting data: %v", err) |
| 63 | } |
| 64 | |
| 65 | validateData(t, data) |
| 66 | |
| 67 | _, err = ProbeURL(ctx, fmt.Sprintf("http://127.0.0.1:%d/test.avi", testPort), "-loglevel", "error") |
| 68 | if err == nil { |
| 69 | t.Errorf("No error reading bad asset") |
| 70 | } |
| 71 | |
| 72 | if strings.Contains(err.Error(), "[]") { |
| 73 | t.Errorf("No stderr included in error message") |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | func Test_ProbeReader(t *testing.T) { |
| 78 | ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second) |
| 79 | defer cancelFn() |
| 80 | |
| 81 | fileReader, err := os.Open(testPath) |
| 82 | if err != nil { |
| 83 | t.Errorf("Error opening test file: %v", err) |
| 84 | } |
| 85 | |
| 86 | data, err := ProbeReader(ctx, fileReader) |
| 87 | if err != nil { |
| 88 | t.Errorf("Error getting data: %v", err) |
| 89 | } |
| 90 | |
| 91 | validateData(t, data) |
| 92 | } |
| 93 | |
| 94 | func Test_ProbeReader_Error(t *testing.T) { |
| 95 | ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second) |
| 96 | defer cancelFn() |
| 97 | |
| 98 | fileReader, err := os.Open(testPathError) |
| 99 | if err != nil { |
| 100 | t.Errorf("Error opening test file: %v", err) |
| 101 | } |
| 102 | |
| 103 | _, err = ProbeReader(ctx, fileReader, "-loglevel", "error") |
| 104 | if err == nil { |
| 105 | t.Errorf("No error reading bad asset") |
| 106 | } |
| 107 | |
| 108 | if strings.Contains(err.Error(), "[]") { |
| 109 | t.Errorf("No stderr included in error message") |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func validateData(t *testing.T, data *ProbeData) { |
| 114 | validateStreams(t, data) |
| 115 | // Check some Tags |
| 116 | const testMajorBrand = "isom" |
| 117 | if data.Format.Tags.MajorBrand != testMajorBrand { |
| 118 | t.Errorf("MajorBrand format tag is not %s", testMajorBrand) |
| 119 | } |
| 120 | |
| 121 | if val, err := data.Format.TagList.GetString("major_brand"); err != nil { |
| 122 | t.Errorf("retrieving major_brand tag errors: %v", err) |
| 123 | } else if val != testMajorBrand { |
| 124 | t.Errorf("MajorBrand format tag is not %s", testMajorBrand) |
| 125 | } |
| 126 | |
| 127 | // test Format.Duration |
| 128 | duration := data.Format.Duration() |
| 129 | if duration.Seconds() != 5.312 { |
| 130 | t.Errorf("this video is 5.312s.") |
| 131 | } |
| 132 | // test Format.StartTime |
| 133 | startTime := data.Format.StartTime() |
| 134 | if startTime != time.Duration(0) { |
| 135 | t.Errorf("this video starts at 0s.") |
| 136 | } |
| 137 | validateChapters(t, data) |
| 138 | } |
| 139 | |
| 140 | func validateStreams(t *testing.T, data *ProbeData) { |
| 141 | // test ProbeData.GetStream |
| 142 | stream := data.StreamType(StreamVideo) |
| 143 | if len(stream) != 1 { |
| 144 | t.Errorf("It just has one video stream.") |
| 145 | } |
| 146 | |
| 147 | // Check some Tags |
| 148 | const testLanguage = "und" |
| 149 | if stream[0].Tags.Rotate != 0 { |
| 150 | t.Errorf("Video stream rotate tag is not 0") |
| 151 | } |
| 152 | if stream[0].Tags.Language != testLanguage { |
| 153 | t.Errorf("Video stream language tag is not %s", testLanguage) |
| 154 | } |
| 155 | |
| 156 | if val, err := stream[0].TagList.GetString("language"); err != nil { |
| 157 | t.Errorf("retrieving language tag errors: %v", err) |
| 158 | } else if val != testLanguage { |
| 159 | t.Errorf("Video stream language tag is not %s", testLanguage) |
| 160 | } |
| 161 | |
| 162 | stream = data.StreamType(StreamAudio) |
| 163 | if len(stream) != 1 { |
| 164 | t.Errorf("It just has one audio stream.") |
| 165 | } |
| 166 | |
| 167 | // this stream is [] |
| 168 | stream = data.StreamType(StreamSubtitle) |
| 169 | if len(stream) != 0 { |
| 170 | t.Errorf("It does not have a subtitle stream.") |
| 171 | } |
| 172 | |
| 173 | stream = data.StreamType(StreamData) |
| 174 | // We expect at least one data stream, since there are chapters |
| 175 | if len(stream) == 0 { |
| 176 | t.Errorf("It does not have a data stream.") |
| 177 | } |
| 178 | |
| 179 | stream = data.StreamType(StreamAttachment) |
| 180 | if len(stream) != 0 { |
| 181 | t.Errorf("It does not have an attachment stream.") |
| 182 | } |
| 183 | |
| 184 | stream = data.StreamType(StreamAny) |
| 185 | if len(stream) != 3 { |
| 186 | t.Errorf("It should have three streams.") |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | func validateChapters(t *testing.T, data *ProbeData) { |
| 191 | chapters := data.Chapters |
| 192 | if chapters == nil { |
| 193 | t.Error("Chapters List was nil") |
| 194 | return |
| 195 | } |
| 196 | if len(chapters) != 3 { |
| 197 | t.Errorf("Expected 3 chapters. Got %d", len(chapters)) |
| 198 | return |
| 199 | } |
| 200 | chapterToTest := chapters[1] |
| 201 | if chapterToTest.Title() != "Middle" { |
| 202 | t.Errorf("Bad Chapter Name. Got %s", chapterToTest.Title()) |
| 203 | } |
| 204 | if chapterToTest.StartTimeSeconds != 2.0 { |
| 205 | t.Errorf("Bad Chapter Start Time. Got %f", chapterToTest.StartTimeSeconds) |
| 206 | } |
| 207 | if chapterToTest.EndTimeSeconds != 4.0 { |
| 208 | t.Errorf("Bad Chapter End Time. Got %f", chapterToTest.EndTimeSeconds) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func Test_ProbeSideData(t *testing.T) { |
| 213 | ctx, cancelFn := context.WithTimeout(context.Background(), 3*time.Second) |
| 214 | defer cancelFn() |
| 215 | |
| 216 | fileReader, err := os.Open("assets/test.mov") |
| 217 | if err != nil { |
| 218 | t.Errorf("Error opening test file: %v", err) |
| 219 | } |
| 220 | |
| 221 | data, err := ProbeReader(ctx, fileReader) |
| 222 | if err != nil { |
| 223 | t.Errorf("Error getting data: %v", err) |
| 224 | } |
| 225 | |
| 226 | videoStream := data.FirstVideoStream() |
| 227 | if videoStream == nil { |
| 228 | t.Error("Video Stream was nil") |
| 229 | return |
| 230 | } |
| 231 | |
| 232 | sideData, err := videoStream.SideDataList.GetDisplayMatrix() |
| 233 | if err != nil { |
| 234 | t.Errorf("Error getting display matrix: %v", err) |
| 235 | } |
| 236 | |
| 237 | if sideData.Rotation != -180 { |
| 238 | t.Errorf("Expected rotation to be -180, got %d", sideData.Rotation) |
| 239 | } |
| 240 | } |