forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

enc.rs72 lines, 2.8 KiBHistoryRawDownload
1//! Encode raw planar video to a ProRes `.mov`.
2//! `enc in.yuv WxH i210|i422|i410|i412|i444 proxy|lt|422|hq|4444|4444xq out.mov [fps_num]`
3//! with env `THREADS`, `QSCALE`, `QUIET`; prints bytes per frame and frames per second.
4use std::time::Instant;
5
6use ec_core::frame::{ColorInfo, PixelFormat, Plane, VideoFrame};
7use ec_core::registry::{Muxer, StreamInfo};
8use ec_core::timebase::TimeBase;
9use ec_core::Packet;
10use ec_mp4::Mp4Muxer;
11use ec_prores::{EncoderConfig, Profile, ProResEncoder};
12use ec_core::registry::Encoder;
13
14fn main() {
15 let a: Vec<String> = std::env::args().collect();
16 let (w, h) = a[2].split_once('x').map(|(w, h)| (w.parse::<u32>().unwrap(), h.parse::<u32>().unwrap())).unwrap();
17 let fmt = match a[3].as_str() {
18 "i210" => PixelFormat::I210,
19 "i422" => PixelFormat::I422,
20 "i410" => PixelFormat::I410,
21 "i412" => PixelFormat::I412,
22 _ => PixelFormat::I444,
23 };
24 let profile = match a[4].as_str() {
25 "proxy" => Profile::Proxy,
26 "lt" => Profile::Lt,
27 "422" => Profile::Standard,
28 "hq" => Profile::Hq,
29 "4444" => Profile::P4444,
30 _ => Profile::P4444Xq,
31 };
32 let data = std::fs::read(&a[1]).unwrap();
33 let geo = fmt.plane_geometry(w, h);
34 let fsize: usize = geo.iter().map(|g| g.0 * g.1).sum();
35 let mut cfg = EncoderConfig::new(profile, w, h);
36 cfg.color = ColorInfo { primaries: 1, transfer: 1, matrix: 1, full_range: false };
37 if let Some(t) = std::env::var("THREADS").ok().and_then(|v| v.parse().ok()) {
38 cfg.threads = t;
39 }
40 cfg.qscale = std::env::var("QSCALE").ok().and_then(|v| v.parse().ok());
41 let enc = ProResEncoder::new(cfg).unwrap();
42 let tb = TimeBase::try_new(1001, 30000).unwrap();
43 let mut mux = Mp4Muxer::with_brands(std::fs::File::create(&a[5]).unwrap(), *b"qt ", &[*b"qt "]).unwrap();
44 let mut info = StreamInfo::new(0, tb, enc.codec_parameters().clone());
45 info.default = true;
46 let s = mux.add_stream(info).unwrap();
47 let t0 = Instant::now();
48 let mut total = 0usize;
49 let n = data.len() / fsize;
50 for i in 0..n {
51 let mut off = i * fsize;
52 let mut planes = Vec::new();
53 for (rb, rows) in &geo {
54 planes.push(Plane::new(data[off..off + rb * rows].to_vec(), *rb));
55 off += rb * rows;
56 }
57 let v = VideoFrame::try_new(fmt, w, h, planes).unwrap();
58 let bytes = enc.encode(&v).unwrap();
59 total += bytes.len();
60 let mut p = Packet::new(s, tb, bytes);
61 p.pts = Some(i as i64);
62 p.dts = Some(i as i64);
63 p.duration = Some(1);
64 p.flags.keyframe = true;
65 mux.write_packet(&p).unwrap();
66 }
67 let el = t0.elapsed().as_secs_f64();
68 mux.finish().unwrap();
69 if std::env::var_os("QUIET").is_none() {
70 println!("frames {n} avg_bytes {} fps {:.1}", total / n.max(1), n as f64 / el);
71 }
72}