forked from quadrupleslap/x264
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfade.rs
More file actions
56 lines (43 loc) · 1.38 KB
/
fade.rs
File metadata and controls
56 lines (43 loc) · 1.38 KB
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
54
55
56
extern crate x264;
use std::io::Write;
use std::fs::File;
use x264::{Colorspace, Encoder, Image};
fn main() {
const WIDTH: usize = 480;
const HEIGHT: usize = 360;
// Initialize things.
let mut encoder =
Encoder::builder()
.fps(60, 1)
.build(Colorspace::RGB, WIDTH as _, HEIGHT as _)
.unwrap();
let mut file = File::create("fade.h264").unwrap();
let mut canvas = vec![0; WIDTH * HEIGHT * 3];
println!("Initialized!");
// Write the headers.
{
let headers = encoder.headers().unwrap();
file.write_all(headers.entirety()).unwrap();
}
// Queue each frame.
for i in 0..300 {
frame(i as f64 / 300.0, &mut canvas);
let image = Image::rgb(WIDTH as _, HEIGHT as _, &canvas);
let (data, _) = encoder.encode((60 * i) as _, image).unwrap();
file.write_all(data.entirety()).unwrap();
}
// Finally, flush any delayed frames.
{
let mut flush = encoder.flush();
while let Some(result) = flush.next() {
let (data, _) = result.unwrap();
file.write_all(data.entirety()).unwrap();
}
}
println!("Done! The output is at `fade.h264`.");
println!("Good luck finding a H.264 viewer, though! ;)");
}
fn frame(p: f64, f: &mut [u8]) {
let lum = (255.0 * p).floor().min(255.0) as u8;
for x in f { *x = lum; }
}