|
| 1 | +package vidio |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func assertEquals(expected, actual interface{}) { |
| 10 | + if expected != actual { |
| 11 | + panic(fmt.Sprintf("Expected %v, got %v", expected, actual)) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +func TestVideoMetaData(t *testing.T) { |
| 16 | + video := NewVideo("test/koala.mp4") |
| 17 | + defer video.Close() |
| 18 | + |
| 19 | + assertEquals(video.filename, "test/koala.mp4") |
| 20 | + assertEquals(video.width, 480) |
| 21 | + assertEquals(video.height, 270) |
| 22 | + assertEquals(video.depth, 3) |
| 23 | + assertEquals(video.bitrate, 170549) |
| 24 | + assertEquals(video.frames, 101) |
| 25 | + assertEquals(video.duration, 3.366667) |
| 26 | + assertEquals(video.fps, float64(30)) |
| 27 | + assertEquals(video.codec, "h264") |
| 28 | + assertEquals(video.audio_codec, "aac") |
| 29 | + assertEquals(video.pix_fmt, "yuv420p") |
| 30 | + assertEquals(len(video.framebuffer), 0) |
| 31 | + |
| 32 | + if video.pipe != nil { |
| 33 | + panic("Expected video.pipe to be nil") |
| 34 | + } |
| 35 | + if video.cmd != nil { |
| 36 | + panic("Expected video.cmd to be nil") |
| 37 | + } |
| 38 | + |
| 39 | + fmt.Println("Video Meta Data Test Passed") |
| 40 | +} |
| 41 | + |
| 42 | +func TestVideoFrame(t *testing.T) { |
| 43 | + video := NewVideo("test/koala.mp4") |
| 44 | + defer video.Close() |
| 45 | + |
| 46 | + video.Read() |
| 47 | + // [203 222 134 203 222 134 203 222 134 203] |
| 48 | + assertEquals(video.framebuffer[0], uint8(203)) |
| 49 | + assertEquals(video.framebuffer[1], uint8(222)) |
| 50 | + assertEquals(video.framebuffer[2], uint8(134)) |
| 51 | + assertEquals(video.framebuffer[3], uint8(203)) |
| 52 | + assertEquals(video.framebuffer[4], uint8(222)) |
| 53 | + assertEquals(video.framebuffer[5], uint8(134)) |
| 54 | + assertEquals(video.framebuffer[6], uint8(203)) |
| 55 | + assertEquals(video.framebuffer[7], uint8(222)) |
| 56 | + assertEquals(video.framebuffer[8], uint8(134)) |
| 57 | + assertEquals(video.framebuffer[9], uint8(203)) |
| 58 | + |
| 59 | + fmt.Println("Video Frame Test Passed") |
| 60 | +} |
| 61 | + |
| 62 | +func TestVideoWriting(t *testing.T) { |
| 63 | + testWriting := func(input, output string, audio bool) { |
| 64 | + video := NewVideo(input) |
| 65 | + options := Options{ |
| 66 | + fps: video.fps, |
| 67 | + bitrate: video.bitrate, |
| 68 | + codec: video.codec, |
| 69 | + } |
| 70 | + if audio { |
| 71 | + options.audio = input |
| 72 | + } |
| 73 | + |
| 74 | + writer := NewVideoWriter(output, video.width, video.height, &options) |
| 75 | + for video.Read() { |
| 76 | + writer.Write(video.framebuffer) |
| 77 | + } |
| 78 | + writer.Close() |
| 79 | + |
| 80 | + os.Remove(output) |
| 81 | + } |
| 82 | + |
| 83 | + testWriting("test/koala.mp4", "test/koala-out.mp4", true) |
| 84 | + fmt.Println("Video Writing (with Audio) Test Passed") |
| 85 | + testWriting("test/koala-noaudio.mp4", "test/koala-noaudio-out.mp4", false) |
| 86 | + fmt.Println("Video Writing (without Audio) Test Passed") |
| 87 | +} |
| 88 | + |
| 89 | +func TestFFprobe(t *testing.T) { |
| 90 | + koalaVideo := ffprobe("test/koala.mp4", "v") |
| 91 | + assertEquals(koalaVideo["width"], "480") |
| 92 | + assertEquals(koalaVideo["height"], "270") |
| 93 | + assertEquals(koalaVideo["duration"], "3.366667") |
| 94 | + assertEquals(koalaVideo["bit_rate"], "170549") |
| 95 | + assertEquals(koalaVideo["codec_name"], "h264") |
| 96 | + koalaAudio := ffprobe("test/koala.mp4", "a") |
| 97 | + assertEquals(koalaAudio["codec_name"], "aac") |
| 98 | + |
| 99 | + koalaVideo = ffprobe("test/koala-noaudio.mp4", "v") |
| 100 | + assertEquals(koalaVideo["width"], "480") |
| 101 | + assertEquals(koalaVideo["height"], "270") |
| 102 | + assertEquals(koalaVideo["duration"], "3.366667") |
| 103 | + assertEquals(koalaVideo["bit_rate"], "170549") |
| 104 | + assertEquals(koalaVideo["codec_name"], "h264") |
| 105 | + koalaAudio = ffprobe("test/koala-noaudio.mp4", "a") |
| 106 | + assertEquals(len(koalaAudio), 0) |
| 107 | + |
| 108 | + fmt.Println("FFprobe Test Passed") |
| 109 | +} |
| 110 | + |
| 111 | +// Linux and MacOS allow the user to directly choose a camera stream by index. |
| 112 | +// Windows requires the user to give the device name. |
| 113 | +func TestDeviceParsingWindows(t *testing.T) { |
| 114 | + // Sample string taken from FFmpeg wiki: |
| 115 | + data := parseDevices( |
| 116 | + []byte(`ffmpeg version N-45279-g6b86dd5... --enable-runtime-cpudetect |
| 117 | + libavutil 51. 74.100 / 51. 74.100 |
| 118 | + libavcodec 54. 65.100 / 54. 65.100 |
| 119 | + libavformat 54. 31.100 / 54. 31.100 |
| 120 | + libavdevice 54. 3.100 / 54. 3.100 |
| 121 | + libavfilter 3. 19.102 / 3. 19.102 |
| 122 | + libswscale 2. 1.101 / 2. 1.101 |
| 123 | + libswresample 0. 16.100 / 0. 16.100 |
| 124 | +[dshow @ 03ACF580] DirectShow video devices |
| 125 | +[dshow @ 03ACF580] "Integrated Camera" |
| 126 | +[dshow @ 03ACF580] "screen-capture-recorder" |
| 127 | +[dshow @ 03ACF580] DirectShow audio devices |
| 128 | +[dshow @ 03ACF580] "Internal Microphone (Conexant 2" |
| 129 | +[dshow @ 03ACF580] "virtual-audio-capturer" |
| 130 | +dummy: Immediate exit requested`, |
| 131 | + ), |
| 132 | + ) |
| 133 | + |
| 134 | + assertEquals(data[0], "Integrated Camera") |
| 135 | + assertEquals(data[1], "screen-capture-recorder") |
| 136 | + |
| 137 | + fmt.Println("Device Parsing for Windows Test Passed") |
| 138 | +} |
| 139 | + |
| 140 | +func TestWebcamParsing(t *testing.T) { |
| 141 | + camera := &Camera{} |
| 142 | + getCameraData( |
| 143 | + `Input #0, dshow, from 'video=Integrated Camera': |
| 144 | + Duration: N/A, start: 1367309.442000, bitrate: N/A |
| 145 | + Stream #0:0: Video: mjpeg (Baseline) (MJPG / 0x47504A4D), yuvj422p(pc, bt470bg/unknown/unknown), 1280x720, 30 fps, 30 tbr, 10000k tbn |
| 146 | +At least one output file must be specified`, |
| 147 | + camera, |
| 148 | + ) |
| 149 | + |
| 150 | + assertEquals(camera.width, 1280) |
| 151 | + assertEquals(camera.height, 720) |
| 152 | + assertEquals(camera.fps, float64(30)) |
| 153 | + assertEquals(camera.codec, "mjpeg") |
| 154 | + |
| 155 | + fmt.Println("Webcam Parsing Test Passed") |
| 156 | +} |
0 commit comments