Skip to main content

ffmpeg_pipeline/encode/
params.rs

1use super::*;
2use ffmpeg_next::{channel_layout::ChannelLayout, Rational};
3
4pub enum EncodeParams {
5    Audio {
6        bitrate: usize,
7        channel_layout: ChannelLayout,
8        compression: Option<usize>,
9        global_header: bool,
10        rate: i32,
11        time_base: Rational,
12        vbr: bool,
13    },
14    Video {
15        time_base: Rational,
16        global_header: bool,
17    },
18}
19
20impl EncodeParams {
21    pub fn time_base(&self) -> Rational {
22        match self {
23            EncodeParams::Audio { time_base, .. } => *time_base,
24            EncodeParams::Video { time_base, .. } => *time_base,
25        }
26    }
27
28    pub fn with_bitrate(self, bitrate: usize) -> Self {
29        if let Self::Audio {
30            channel_layout,
31            compression,
32            global_header,
33            rate,
34            time_base,
35            vbr,
36            ..
37        } = self
38        {
39            Self::Audio {
40                bitrate,
41                channel_layout,
42                compression,
43                global_header,
44                rate,
45                time_base,
46                vbr,
47            }
48        } else {
49            self
50        }
51    }
52
53    pub fn with_compression(self, compression: Option<usize>) -> Self {
54        if let Self::Audio {
55            bitrate,
56            channel_layout,
57            global_header,
58            rate,
59            time_base,
60            vbr,
61            ..
62        } = self
63        {
64            Self::Audio {
65                bitrate,
66                channel_layout,
67                compression,
68                global_header,
69                rate,
70                time_base,
71                vbr,
72            }
73        } else {
74            self
75        }
76    }
77
78    pub fn with_vbr(self, vbr: bool) -> Self {
79        if let Self::Audio {
80            bitrate,
81            channel_layout,
82            compression,
83            global_header,
84            rate,
85            time_base,
86            ..
87        } = self
88        {
89            Self::Audio {
90                bitrate,
91                channel_layout,
92                compression,
93                global_header,
94                rate,
95                time_base,
96                vbr,
97            }
98        } else {
99            self
100        }
101    }
102}
103
104impl Default for EncodeParams {
105    fn default() -> Self {
106        EncodeParams::Audio {
107            bitrate: 128 * 1024,
108            channel_layout: ChannelLayout::STEREO,
109            compression: None,
110            global_header: false,
111            rate: 44100,
112            time_base: Rational::new(1, 44100),
113            vbr: false,
114        }
115    }
116}
117
118impl From<&Decoder<'_>> for EncodeParams {
119    fn from(decoder: &Decoder<'_>) -> Self {
120        match decoder.get_decoder() {
121            StreamDecoder::Audio(decoder) => EncodeParams::Audio {
122                bitrate: decoder.bit_rate(),
123                rate: decoder.rate() as i32,
124                channel_layout: decoder_channel_layout(decoder),
125                compression: None,
126                time_base: decoder.time_base(),
127                global_header: false,
128                vbr: false,
129            },
130            StreamDecoder::Video(decoder) => EncodeParams::Video {
131                time_base: decoder.time_base(),
132                global_header: false,
133            },
134        }
135    }
136}