1use std::{
2 fs::{remove_file, File},
3 io::ErrorKind,
4 path::Path,
5};
6
7use image::ImageError;
8use rav1e::{color::ChromaSampling, config::RateControlConfig, data::Rational, EncoderConfig};
9
10use crate::utils::io_error;
11
12mod encoding;
13
14pub struct Av1Encoder {
15 output: File,
16 config: EncoderConfig,
17 rate_control: RateControlConfig,
18}
19
20impl Av1Encoder {
21 pub fn new<P: AsRef<Path>>(output: P, overwrite: bool) -> Result<Self, ImageError> {
38 let path = output.as_ref();
39 if path.exists() {
40 if overwrite {
41 remove_file(path)?;
42 }
43 else {
44 io_error(ErrorKind::AlreadyExists, format!("File already exists: {}", path.display()))?;
45 }
46 }
47 let mut encoder = EncoderConfig::default();
48 encoder.chroma_sampling = ChromaSampling::Cs444;
49 Ok(Self { output: File::create(path)?, config: encoder, rate_control: Default::default() })
50 }
51 pub fn mut_config(&mut self) -> &mut EncoderConfig {
52 &mut self.config
53 }
54 pub fn with_config(mut self, config: EncoderConfig) -> Self {
55 self.config = config;
56 self
57 }
58 pub fn with_size(mut self, width: usize, height: usize) -> Self {
59 self.config.width = width;
60 self.config.height = height;
61 self
62 }
63 pub fn with_fps(mut self, fps: usize) -> Self {
64 self.config.time_base = Rational { num: 1, den: fps as u64 };
65 self
66 }
67 pub fn with_rate_control(mut self, rate_control: RateControlConfig) -> Self {
68 self.rate_control = rate_control;
69 self
70 }
71 }