-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
avclj_test.clj
53 lines (46 loc) · 1.81 KB
/
avclj_test.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(ns avclj-test
(:require [clojure.test :refer [deftest is]]
[avclj :as avclj]
[avclj.av-codec-ids :as codec-ids]
[tech.v3.tensor :as dtt]
[tech.v3.datatype :as dtype]
[tech.v3.libs.buffered-image :as bufimg]))
(defn img-tensor
[shape ^long offset]
(dtt/compute-tensor shape
(fn [^long y ^long x ^long _c]
(let [ymod (-> (quot (+ y offset) 32)
(mod 2))
xmod (-> (quot (+ x offset) 32)
(mod 2))]
(if (and (== 0 xmod)
(== 0 ymod))
255
0)))
:uint8))
(defn save-tensor
[tens fname]
(let [[h w _c] (dtype/shape tens)
bufimg (bufimg/new-image h w :byte-bgr)]
(-> (dtype/copy! tens bufimg)
(bufimg/save! fname))))
(avclj/initialize!)
(deftest encode-demo
(let [encoder-name codec-ids/AV_CODEC_ID_H264
output-fname "test/data/test-video.mp4"]
(.delete (java.io.File. output-fname))
(with-open [encoder (avclj/make-video-encoder
256 256 output-fname
{:encoder-name encoder-name
:bit-rate 600000})]
(dotimes [iter 600]
(avclj/encode-frame! encoder (img-tensor [256 256 3] iter))))
(is (.exists (java.io.File. output-fname)))
(let [frame-count
(with-open [decoder (avclj/make-video-decoder "test/data/test-video.mp4")]
(loop [idx 0
frame (avclj/decode-frame! decoder)]
(if frame
(recur (inc idx) (avclj/decode-frame! decoder))
idx)))]
(is (= 600 frame-count)))))