pub struct StreamMut<'a> { /* private fields */ }
Implementations§
Source§impl<'a> StreamMut<'a>
impl<'a> StreamMut<'a>
Sourcepub fn set_time_base<R: Into<Rational>>(&mut self, value: R)
pub fn set_time_base<R: Into<Rational>>(&mut self, value: R)
Examples found in repository?
examples/transcode-audio.rs (line 117)
65fn transcoder<P: AsRef<Path> + ?Sized>(
66 ictx: &mut format::context::Input,
67 octx: &mut format::context::Output,
68 path: &P,
69 filter_spec: &str,
70) -> Result<Transcoder, ffmpeg::Error> {
71 let input = ictx
72 .streams()
73 .best(media::Type::Audio)
74 .expect("could not find best audio stream");
75 let context = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
76 let mut decoder = context.decoder().audio()?;
77 let codec = ffmpeg::encoder::find(octx.format().codec(path, media::Type::Audio))
78 .expect("failed to find encoder")
79 .audio()?;
80 let global = octx
81 .format()
82 .flags()
83 .contains(ffmpeg::format::flag::Flags::GLOBAL_HEADER);
84
85 decoder.set_parameters(input.parameters())?;
86
87 let mut output = octx.add_stream(codec)?;
88 let context = ffmpeg::codec::context::Context::from_parameters(output.parameters())?;
89 let mut encoder = context.encoder().audio()?;
90
91 let channel_layout = codec
92 .channel_layouts()
93 .map(|cls| cls.best(decoder.channel_layout().channels()))
94 .unwrap_or(ffmpeg::channel_layout::ChannelLayout::STEREO);
95
96 if global {
97 encoder.set_flags(ffmpeg::codec::flag::Flags::GLOBAL_HEADER);
98 }
99
100 encoder.set_rate(decoder.rate() as i32);
101 encoder.set_channel_layout(channel_layout);
102 #[cfg(not(feature = "ffmpeg_7_0"))]
103 {
104 encoder.set_channels(channel_layout.channels());
105 }
106 encoder.set_format(
107 codec
108 .formats()
109 .expect("unknown supported formats")
110 .next()
111 .unwrap(),
112 );
113 encoder.set_bit_rate(decoder.bit_rate());
114 encoder.set_max_bit_rate(decoder.max_bit_rate());
115
116 encoder.set_time_base((1, decoder.rate() as i32));
117 output.set_time_base((1, decoder.rate() as i32));
118
119 let encoder = encoder.open_as(codec)?;
120 output.set_parameters(&encoder);
121
122 let filter = filter(filter_spec, &decoder, &encoder)?;
123
124 let in_time_base = decoder.time_base();
125 let out_time_base = output.time_base();
126
127 Ok(Transcoder {
128 stream: input.index(),
129 filter,
130 decoder,
131 encoder,
132 in_time_base,
133 out_time_base,
134 })
135}
pub fn set_rate<R: Into<Rational>>(&mut self, value: R)
pub fn set_avg_frame_rate<R: Into<Rational>>(&mut self, value: R)
Sourcepub fn set_parameters<P: Into<Parameters>>(&mut self, parameters: P)
pub fn set_parameters<P: Into<Parameters>>(&mut self, parameters: P)
Examples found in repository?
examples/transcode-x264.rs (line 62)
43 fn new(
44 ist: &format::stream::Stream,
45 octx: &mut format::context::Output,
46 ost_index: usize,
47 x264_opts: Dictionary,
48 enable_logging: bool,
49 ) -> Result<Self, ffmpeg::Error> {
50 let global_header = octx.format().flags().contains(format::Flags::GLOBAL_HEADER);
51 let decoder = ffmpeg::codec::context::Context::from_parameters(ist.parameters())?
52 .decoder()
53 .video()?;
54
55 let codec = encoder::find(codec::Id::H264);
56 let mut ost = octx.add_stream(codec)?;
57
58 let mut encoder =
59 codec::context::Context::new_with_codec(codec.ok_or(ffmpeg::Error::InvalidData)?)
60 .encoder()
61 .video()?;
62 ost.set_parameters(&encoder);
63 encoder.set_height(decoder.height());
64 encoder.set_width(decoder.width());
65 encoder.set_aspect_ratio(decoder.aspect_ratio());
66 encoder.set_format(decoder.format());
67 encoder.set_frame_rate(decoder.frame_rate());
68 encoder.set_time_base(ist.time_base());
69
70 if global_header {
71 encoder.set_flags(codec::Flags::GLOBAL_HEADER);
72 }
73
74 let opened_encoder = encoder
75 .open_with(x264_opts)
76 .expect("error opening x264 with supplied settings");
77 ost.set_parameters(&opened_encoder);
78 Ok(Self {
79 ost_index,
80 decoder,
81 input_time_base: ist.time_base(),
82 encoder: opened_encoder,
83 logging_enabled: enable_logging,
84 frame_count: 0,
85 last_log_frame_count: 0,
86 starting_time: Instant::now(),
87 last_log_time: Instant::now(),
88 })
89 }
90
91 fn send_packet_to_decoder(&mut self, packet: &Packet) {
92 self.decoder.send_packet(packet).unwrap();
93 }
94
95 fn send_eof_to_decoder(&mut self) {
96 self.decoder.send_eof().unwrap();
97 }
98
99 fn receive_and_process_decoded_frames(
100 &mut self,
101 octx: &mut format::context::Output,
102 ost_time_base: Rational,
103 ) {
104 let mut frame = frame::Video::empty();
105 while self.decoder.receive_frame(&mut frame).is_ok() {
106 self.frame_count += 1;
107 let timestamp = frame.timestamp();
108 self.log_progress(f64::from(
109 Rational(timestamp.unwrap_or(0) as i32, 1) * self.input_time_base,
110 ));
111 frame.set_pts(timestamp);
112 frame.set_kind(picture::Type::None);
113 self.send_frame_to_encoder(&frame);
114 self.receive_and_process_encoded_packets(octx, ost_time_base);
115 }
116 }
117
118 fn send_frame_to_encoder(&mut self, frame: &frame::Video) {
119 self.encoder.send_frame(frame).unwrap();
120 }
121
122 fn send_eof_to_encoder(&mut self) {
123 self.encoder.send_eof().unwrap();
124 }
125
126 fn receive_and_process_encoded_packets(
127 &mut self,
128 octx: &mut format::context::Output,
129 ost_time_base: Rational,
130 ) {
131 let mut encoded = Packet::empty();
132 while self.encoder.receive_packet(&mut encoded).is_ok() {
133 encoded.set_stream(self.ost_index);
134 encoded.rescale_ts(self.input_time_base, ost_time_base);
135 encoded.write_interleaved(octx).unwrap();
136 }
137 }
138
139 fn log_progress(&mut self, timestamp: f64) {
140 if !self.logging_enabled
141 || (self.frame_count - self.last_log_frame_count < 100
142 && self.last_log_time.elapsed().as_secs_f64() < 1.0)
143 {
144 return;
145 }
146 eprintln!(
147 "time elpased: \t{:8.2}\tframe count: {:8}\ttimestamp: {:8.2}",
148 self.starting_time.elapsed().as_secs_f64(),
149 self.frame_count,
150 timestamp
151 );
152 self.last_log_frame_count = self.frame_count;
153 self.last_log_time = Instant::now();
154 }
155}
156
157fn parse_opts<'a>(s: String) -> Option<Dictionary<'a>> {
158 let mut dict = Dictionary::new();
159 for keyval in s.split_terminator(',') {
160 let tokens: Vec<&str> = keyval.split('=').collect();
161 match tokens[..] {
162 [key, val] => dict.set(key, val),
163 _ => return None,
164 }
165 }
166 Some(dict)
167}
168
169fn main() {
170 let input_file = env::args().nth(1).expect("missing input file");
171 let output_file = env::args().nth(2).expect("missing output file");
172 let x264_opts = parse_opts(
173 env::args()
174 .nth(3)
175 .unwrap_or_else(|| DEFAULT_X264_OPTS.to_string()),
176 )
177 .expect("invalid x264 options string");
178
179 eprintln!("x264 options: {:?}", x264_opts);
180
181 ffmpeg::init().unwrap();
182 log::set_level(log::Level::Info);
183
184 let mut ictx = format::input(&input_file).unwrap();
185 let mut octx = format::output(&output_file).unwrap();
186
187 format::context::input::dump(&ictx, 0, Some(&input_file));
188
189 let best_video_stream_index = ictx
190 .streams()
191 .best(media::Type::Video)
192 .map(|stream| stream.index());
193 let mut stream_mapping: Vec<isize> = vec![0; ictx.nb_streams() as _];
194 let mut ist_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
195 let mut ost_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
196 let mut transcoders = HashMap::new();
197 let mut ost_index = 0;
198 for (ist_index, ist) in ictx.streams().enumerate() {
199 let ist_medium = ist.parameters().medium();
200 if ist_medium != media::Type::Audio
201 && ist_medium != media::Type::Video
202 && ist_medium != media::Type::Subtitle
203 {
204 stream_mapping[ist_index] = -1;
205 continue;
206 }
207 stream_mapping[ist_index] = ost_index;
208 ist_time_bases[ist_index] = ist.time_base();
209 if ist_medium == media::Type::Video {
210 // Initialize transcoder for video stream.
211 transcoders.insert(
212 ist_index,
213 Transcoder::new(
214 &ist,
215 &mut octx,
216 ost_index as _,
217 x264_opts.to_owned(),
218 Some(ist_index) == best_video_stream_index,
219 )
220 .unwrap(),
221 );
222 } else {
223 // Set up for stream copy for non-video stream.
224 let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
225 ost.set_parameters(ist.parameters());
226 // We need to set codec_tag to 0 lest we run into incompatible codec tag
227 // issues when muxing into a different container format. Unfortunately
228 // there's no high level API to do this (yet).
229 unsafe {
230 (*ost.parameters().as_mut_ptr()).codec_tag = 0;
231 }
232 }
233 ost_index += 1;
234 }
235
236 octx.set_metadata(ictx.metadata().to_owned());
237 format::context::output::dump(&octx, 0, Some(&output_file));
238 octx.write_header().unwrap();
239
240 for (ost_index, _) in octx.streams().enumerate() {
241 ost_time_bases[ost_index] = octx.stream(ost_index as _).unwrap().time_base();
242 }
243
244 for (stream, mut packet) in ictx.packets() {
245 let ist_index = stream.index();
246 let ost_index = stream_mapping[ist_index];
247 if ost_index < 0 {
248 continue;
249 }
250 let ost_time_base = ost_time_bases[ost_index as usize];
251 match transcoders.get_mut(&ist_index) {
252 Some(transcoder) => {
253 transcoder.send_packet_to_decoder(&packet);
254 transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
255 }
256 None => {
257 // Do stream copy on non-video streams.
258 packet.rescale_ts(ist_time_bases[ist_index], ost_time_base);
259 packet.set_position(-1);
260 packet.set_stream(ost_index as _);
261 packet.write_interleaved(&mut octx).unwrap();
262 }
263 }
264 }
265
266 // Flush encoders and decoders.
267 for (ost_index, transcoder) in transcoders.iter_mut() {
268 let ost_time_base = ost_time_bases[*ost_index];
269 transcoder.send_eof_to_decoder();
270 transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
271 transcoder.send_eof_to_encoder();
272 transcoder.receive_and_process_encoded_packets(&mut octx, ost_time_base);
273 }
274
275 octx.write_trailer().unwrap();
276}
More examples
examples/remux.rs (line 33)
7fn main() {
8 let input_file = env::args().nth(1).expect("missing input file");
9 let output_file = env::args().nth(2).expect("missing output file");
10
11 ffmpeg::init().unwrap();
12 log::set_level(log::Level::Warning);
13
14 let mut ictx = format::input(&input_file).unwrap();
15 let mut octx = format::output(&output_file).unwrap();
16
17 let mut stream_mapping = vec![0; ictx.nb_streams() as _];
18 let mut ist_time_bases = vec![Rational(0, 1); ictx.nb_streams() as _];
19 let mut ost_index = 0;
20 for (ist_index, ist) in ictx.streams().enumerate() {
21 let ist_medium = ist.parameters().medium();
22 if ist_medium != media::Type::Audio
23 && ist_medium != media::Type::Video
24 && ist_medium != media::Type::Subtitle
25 {
26 stream_mapping[ist_index] = -1;
27 continue;
28 }
29 stream_mapping[ist_index] = ost_index;
30 ist_time_bases[ist_index] = ist.time_base();
31 ost_index += 1;
32 let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
33 ost.set_parameters(ist.parameters());
34 // We need to set codec_tag to 0 lest we run into incompatible codec tag
35 // issues when muxing into a different container format. Unfortunately
36 // there's no high level API to do this (yet).
37 unsafe {
38 (*ost.parameters().as_mut_ptr()).codec_tag = 0;
39 }
40 }
41
42 octx.set_metadata(ictx.metadata().to_owned());
43 octx.write_header().unwrap();
44
45 for (stream, mut packet) in ictx.packets() {
46 let ist_index = stream.index();
47 let ost_index = stream_mapping[ist_index];
48 if ost_index < 0 {
49 continue;
50 }
51 let ost = octx.stream(ost_index as _).unwrap();
52 packet.rescale_ts(ist_time_bases[ist_index], ost.time_base());
53 packet.set_position(-1);
54 packet.set_stream(ost_index as _);
55 packet.write_interleaved(&mut octx).unwrap();
56 }
57
58 octx.write_trailer().unwrap();
59}
examples/transcode-audio.rs (line 120)
65fn transcoder<P: AsRef<Path> + ?Sized>(
66 ictx: &mut format::context::Input,
67 octx: &mut format::context::Output,
68 path: &P,
69 filter_spec: &str,
70) -> Result<Transcoder, ffmpeg::Error> {
71 let input = ictx
72 .streams()
73 .best(media::Type::Audio)
74 .expect("could not find best audio stream");
75 let context = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
76 let mut decoder = context.decoder().audio()?;
77 let codec = ffmpeg::encoder::find(octx.format().codec(path, media::Type::Audio))
78 .expect("failed to find encoder")
79 .audio()?;
80 let global = octx
81 .format()
82 .flags()
83 .contains(ffmpeg::format::flag::Flags::GLOBAL_HEADER);
84
85 decoder.set_parameters(input.parameters())?;
86
87 let mut output = octx.add_stream(codec)?;
88 let context = ffmpeg::codec::context::Context::from_parameters(output.parameters())?;
89 let mut encoder = context.encoder().audio()?;
90
91 let channel_layout = codec
92 .channel_layouts()
93 .map(|cls| cls.best(decoder.channel_layout().channels()))
94 .unwrap_or(ffmpeg::channel_layout::ChannelLayout::STEREO);
95
96 if global {
97 encoder.set_flags(ffmpeg::codec::flag::Flags::GLOBAL_HEADER);
98 }
99
100 encoder.set_rate(decoder.rate() as i32);
101 encoder.set_channel_layout(channel_layout);
102 #[cfg(not(feature = "ffmpeg_7_0"))]
103 {
104 encoder.set_channels(channel_layout.channels());
105 }
106 encoder.set_format(
107 codec
108 .formats()
109 .expect("unknown supported formats")
110 .next()
111 .unwrap(),
112 );
113 encoder.set_bit_rate(decoder.bit_rate());
114 encoder.set_max_bit_rate(decoder.max_bit_rate());
115
116 encoder.set_time_base((1, decoder.rate() as i32));
117 output.set_time_base((1, decoder.rate() as i32));
118
119 let encoder = encoder.open_as(codec)?;
120 output.set_parameters(&encoder);
121
122 let filter = filter(filter_spec, &decoder, &encoder)?;
123
124 let in_time_base = decoder.time_base();
125 let out_time_base = output.time_base();
126
127 Ok(Transcoder {
128 stream: input.index(),
129 filter,
130 decoder,
131 encoder,
132 in_time_base,
133 out_time_base,
134 })
135}
pub fn set_metadata(&mut self, metadata: Dictionary<'_>)
Methods from Deref<Target = Stream<'a>>§
pub unsafe fn as_ptr(&self) -> *const AVStream
pub fn id(&self) -> i32
Sourcepub fn parameters(&self) -> Parameters
pub fn parameters(&self) -> Parameters
Examples found in repository?
examples/dump-frames.rs (line 21)
11fn main() -> Result<(), ffmpeg::Error> {
12 ffmpeg::init().unwrap();
13
14 if let Ok(mut ictx) = input(&env::args().nth(1).expect("Cannot open file.")) {
15 let input = ictx
16 .streams()
17 .best(Type::Video)
18 .ok_or(ffmpeg::Error::StreamNotFound)?;
19 let video_stream_index = input.index();
20
21 let context_decoder = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
22 let mut decoder = context_decoder.decoder().video()?;
23
24 let mut scaler = Context::get(
25 decoder.format(),
26 decoder.width(),
27 decoder.height(),
28 Pixel::RGB24,
29 decoder.width(),
30 decoder.height(),
31 Flags::BILINEAR,
32 )?;
33
34 let mut frame_index = 0;
35
36 let mut receive_and_process_decoded_frames =
37 |decoder: &mut ffmpeg::decoder::Video| -> Result<(), ffmpeg::Error> {
38 let mut decoded = Video::empty();
39 while decoder.receive_frame(&mut decoded).is_ok() {
40 let mut rgb_frame = Video::empty();
41 scaler.run(&decoded, &mut rgb_frame)?;
42 save_file(&rgb_frame, frame_index).unwrap();
43 frame_index += 1;
44 }
45 Ok(())
46 };
47
48 for (stream, packet) in ictx.packets() {
49 if stream.index() == video_stream_index {
50 decoder.send_packet(&packet)?;
51 receive_and_process_decoded_frames(&mut decoder)?;
52 }
53 }
54 decoder.send_eof()?;
55 receive_and_process_decoded_frames(&mut decoder)?;
56 }
57
58 Ok(())
59}
More examples
examples/transcode-x264.rs (line 51)
43 fn new(
44 ist: &format::stream::Stream,
45 octx: &mut format::context::Output,
46 ost_index: usize,
47 x264_opts: Dictionary,
48 enable_logging: bool,
49 ) -> Result<Self, ffmpeg::Error> {
50 let global_header = octx.format().flags().contains(format::Flags::GLOBAL_HEADER);
51 let decoder = ffmpeg::codec::context::Context::from_parameters(ist.parameters())?
52 .decoder()
53 .video()?;
54
55 let codec = encoder::find(codec::Id::H264);
56 let mut ost = octx.add_stream(codec)?;
57
58 let mut encoder =
59 codec::context::Context::new_with_codec(codec.ok_or(ffmpeg::Error::InvalidData)?)
60 .encoder()
61 .video()?;
62 ost.set_parameters(&encoder);
63 encoder.set_height(decoder.height());
64 encoder.set_width(decoder.width());
65 encoder.set_aspect_ratio(decoder.aspect_ratio());
66 encoder.set_format(decoder.format());
67 encoder.set_frame_rate(decoder.frame_rate());
68 encoder.set_time_base(ist.time_base());
69
70 if global_header {
71 encoder.set_flags(codec::Flags::GLOBAL_HEADER);
72 }
73
74 let opened_encoder = encoder
75 .open_with(x264_opts)
76 .expect("error opening x264 with supplied settings");
77 ost.set_parameters(&opened_encoder);
78 Ok(Self {
79 ost_index,
80 decoder,
81 input_time_base: ist.time_base(),
82 encoder: opened_encoder,
83 logging_enabled: enable_logging,
84 frame_count: 0,
85 last_log_frame_count: 0,
86 starting_time: Instant::now(),
87 last_log_time: Instant::now(),
88 })
89 }
90
91 fn send_packet_to_decoder(&mut self, packet: &Packet) {
92 self.decoder.send_packet(packet).unwrap();
93 }
94
95 fn send_eof_to_decoder(&mut self) {
96 self.decoder.send_eof().unwrap();
97 }
98
99 fn receive_and_process_decoded_frames(
100 &mut self,
101 octx: &mut format::context::Output,
102 ost_time_base: Rational,
103 ) {
104 let mut frame = frame::Video::empty();
105 while self.decoder.receive_frame(&mut frame).is_ok() {
106 self.frame_count += 1;
107 let timestamp = frame.timestamp();
108 self.log_progress(f64::from(
109 Rational(timestamp.unwrap_or(0) as i32, 1) * self.input_time_base,
110 ));
111 frame.set_pts(timestamp);
112 frame.set_kind(picture::Type::None);
113 self.send_frame_to_encoder(&frame);
114 self.receive_and_process_encoded_packets(octx, ost_time_base);
115 }
116 }
117
118 fn send_frame_to_encoder(&mut self, frame: &frame::Video) {
119 self.encoder.send_frame(frame).unwrap();
120 }
121
122 fn send_eof_to_encoder(&mut self) {
123 self.encoder.send_eof().unwrap();
124 }
125
126 fn receive_and_process_encoded_packets(
127 &mut self,
128 octx: &mut format::context::Output,
129 ost_time_base: Rational,
130 ) {
131 let mut encoded = Packet::empty();
132 while self.encoder.receive_packet(&mut encoded).is_ok() {
133 encoded.set_stream(self.ost_index);
134 encoded.rescale_ts(self.input_time_base, ost_time_base);
135 encoded.write_interleaved(octx).unwrap();
136 }
137 }
138
139 fn log_progress(&mut self, timestamp: f64) {
140 if !self.logging_enabled
141 || (self.frame_count - self.last_log_frame_count < 100
142 && self.last_log_time.elapsed().as_secs_f64() < 1.0)
143 {
144 return;
145 }
146 eprintln!(
147 "time elpased: \t{:8.2}\tframe count: {:8}\ttimestamp: {:8.2}",
148 self.starting_time.elapsed().as_secs_f64(),
149 self.frame_count,
150 timestamp
151 );
152 self.last_log_frame_count = self.frame_count;
153 self.last_log_time = Instant::now();
154 }
155}
156
157fn parse_opts<'a>(s: String) -> Option<Dictionary<'a>> {
158 let mut dict = Dictionary::new();
159 for keyval in s.split_terminator(',') {
160 let tokens: Vec<&str> = keyval.split('=').collect();
161 match tokens[..] {
162 [key, val] => dict.set(key, val),
163 _ => return None,
164 }
165 }
166 Some(dict)
167}
168
169fn main() {
170 let input_file = env::args().nth(1).expect("missing input file");
171 let output_file = env::args().nth(2).expect("missing output file");
172 let x264_opts = parse_opts(
173 env::args()
174 .nth(3)
175 .unwrap_or_else(|| DEFAULT_X264_OPTS.to_string()),
176 )
177 .expect("invalid x264 options string");
178
179 eprintln!("x264 options: {:?}", x264_opts);
180
181 ffmpeg::init().unwrap();
182 log::set_level(log::Level::Info);
183
184 let mut ictx = format::input(&input_file).unwrap();
185 let mut octx = format::output(&output_file).unwrap();
186
187 format::context::input::dump(&ictx, 0, Some(&input_file));
188
189 let best_video_stream_index = ictx
190 .streams()
191 .best(media::Type::Video)
192 .map(|stream| stream.index());
193 let mut stream_mapping: Vec<isize> = vec![0; ictx.nb_streams() as _];
194 let mut ist_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
195 let mut ost_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
196 let mut transcoders = HashMap::new();
197 let mut ost_index = 0;
198 for (ist_index, ist) in ictx.streams().enumerate() {
199 let ist_medium = ist.parameters().medium();
200 if ist_medium != media::Type::Audio
201 && ist_medium != media::Type::Video
202 && ist_medium != media::Type::Subtitle
203 {
204 stream_mapping[ist_index] = -1;
205 continue;
206 }
207 stream_mapping[ist_index] = ost_index;
208 ist_time_bases[ist_index] = ist.time_base();
209 if ist_medium == media::Type::Video {
210 // Initialize transcoder for video stream.
211 transcoders.insert(
212 ist_index,
213 Transcoder::new(
214 &ist,
215 &mut octx,
216 ost_index as _,
217 x264_opts.to_owned(),
218 Some(ist_index) == best_video_stream_index,
219 )
220 .unwrap(),
221 );
222 } else {
223 // Set up for stream copy for non-video stream.
224 let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
225 ost.set_parameters(ist.parameters());
226 // We need to set codec_tag to 0 lest we run into incompatible codec tag
227 // issues when muxing into a different container format. Unfortunately
228 // there's no high level API to do this (yet).
229 unsafe {
230 (*ost.parameters().as_mut_ptr()).codec_tag = 0;
231 }
232 }
233 ost_index += 1;
234 }
235
236 octx.set_metadata(ictx.metadata().to_owned());
237 format::context::output::dump(&octx, 0, Some(&output_file));
238 octx.write_header().unwrap();
239
240 for (ost_index, _) in octx.streams().enumerate() {
241 ost_time_bases[ost_index] = octx.stream(ost_index as _).unwrap().time_base();
242 }
243
244 for (stream, mut packet) in ictx.packets() {
245 let ist_index = stream.index();
246 let ost_index = stream_mapping[ist_index];
247 if ost_index < 0 {
248 continue;
249 }
250 let ost_time_base = ost_time_bases[ost_index as usize];
251 match transcoders.get_mut(&ist_index) {
252 Some(transcoder) => {
253 transcoder.send_packet_to_decoder(&packet);
254 transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
255 }
256 None => {
257 // Do stream copy on non-video streams.
258 packet.rescale_ts(ist_time_bases[ist_index], ost_time_base);
259 packet.set_position(-1);
260 packet.set_stream(ost_index as _);
261 packet.write_interleaved(&mut octx).unwrap();
262 }
263 }
264 }
265
266 // Flush encoders and decoders.
267 for (ost_index, transcoder) in transcoders.iter_mut() {
268 let ost_time_base = ost_time_bases[*ost_index];
269 transcoder.send_eof_to_decoder();
270 transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
271 transcoder.send_eof_to_encoder();
272 transcoder.receive_and_process_encoded_packets(&mut octx, ost_time_base);
273 }
274
275 octx.write_trailer().unwrap();
276}
examples/remux.rs (line 21)
7fn main() {
8 let input_file = env::args().nth(1).expect("missing input file");
9 let output_file = env::args().nth(2).expect("missing output file");
10
11 ffmpeg::init().unwrap();
12 log::set_level(log::Level::Warning);
13
14 let mut ictx = format::input(&input_file).unwrap();
15 let mut octx = format::output(&output_file).unwrap();
16
17 let mut stream_mapping = vec![0; ictx.nb_streams() as _];
18 let mut ist_time_bases = vec![Rational(0, 1); ictx.nb_streams() as _];
19 let mut ost_index = 0;
20 for (ist_index, ist) in ictx.streams().enumerate() {
21 let ist_medium = ist.parameters().medium();
22 if ist_medium != media::Type::Audio
23 && ist_medium != media::Type::Video
24 && ist_medium != media::Type::Subtitle
25 {
26 stream_mapping[ist_index] = -1;
27 continue;
28 }
29 stream_mapping[ist_index] = ost_index;
30 ist_time_bases[ist_index] = ist.time_base();
31 ost_index += 1;
32 let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
33 ost.set_parameters(ist.parameters());
34 // We need to set codec_tag to 0 lest we run into incompatible codec tag
35 // issues when muxing into a different container format. Unfortunately
36 // there's no high level API to do this (yet).
37 unsafe {
38 (*ost.parameters().as_mut_ptr()).codec_tag = 0;
39 }
40 }
41
42 octx.set_metadata(ictx.metadata().to_owned());
43 octx.write_header().unwrap();
44
45 for (stream, mut packet) in ictx.packets() {
46 let ist_index = stream.index();
47 let ost_index = stream_mapping[ist_index];
48 if ost_index < 0 {
49 continue;
50 }
51 let ost = octx.stream(ost_index as _).unwrap();
52 packet.rescale_ts(ist_time_bases[ist_index], ost.time_base());
53 packet.set_position(-1);
54 packet.set_stream(ost_index as _);
55 packet.write_interleaved(&mut octx).unwrap();
56 }
57
58 octx.write_trailer().unwrap();
59}
examples/transcode-audio.rs (line 75)
65fn transcoder<P: AsRef<Path> + ?Sized>(
66 ictx: &mut format::context::Input,
67 octx: &mut format::context::Output,
68 path: &P,
69 filter_spec: &str,
70) -> Result<Transcoder, ffmpeg::Error> {
71 let input = ictx
72 .streams()
73 .best(media::Type::Audio)
74 .expect("could not find best audio stream");
75 let context = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
76 let mut decoder = context.decoder().audio()?;
77 let codec = ffmpeg::encoder::find(octx.format().codec(path, media::Type::Audio))
78 .expect("failed to find encoder")
79 .audio()?;
80 let global = octx
81 .format()
82 .flags()
83 .contains(ffmpeg::format::flag::Flags::GLOBAL_HEADER);
84
85 decoder.set_parameters(input.parameters())?;
86
87 let mut output = octx.add_stream(codec)?;
88 let context = ffmpeg::codec::context::Context::from_parameters(output.parameters())?;
89 let mut encoder = context.encoder().audio()?;
90
91 let channel_layout = codec
92 .channel_layouts()
93 .map(|cls| cls.best(decoder.channel_layout().channels()))
94 .unwrap_or(ffmpeg::channel_layout::ChannelLayout::STEREO);
95
96 if global {
97 encoder.set_flags(ffmpeg::codec::flag::Flags::GLOBAL_HEADER);
98 }
99
100 encoder.set_rate(decoder.rate() as i32);
101 encoder.set_channel_layout(channel_layout);
102 #[cfg(not(feature = "ffmpeg_7_0"))]
103 {
104 encoder.set_channels(channel_layout.channels());
105 }
106 encoder.set_format(
107 codec
108 .formats()
109 .expect("unknown supported formats")
110 .next()
111 .unwrap(),
112 );
113 encoder.set_bit_rate(decoder.bit_rate());
114 encoder.set_max_bit_rate(decoder.max_bit_rate());
115
116 encoder.set_time_base((1, decoder.rate() as i32));
117 output.set_time_base((1, decoder.rate() as i32));
118
119 let encoder = encoder.open_as(codec)?;
120 output.set_parameters(&encoder);
121
122 let filter = filter(filter_spec, &decoder, &encoder)?;
123
124 let in_time_base = decoder.time_base();
125 let out_time_base = output.time_base();
126
127 Ok(Transcoder {
128 stream: input.index(),
129 filter,
130 decoder,
131 encoder,
132 in_time_base,
133 out_time_base,
134 })
135}
examples/metadata.rs (line 45)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
Sourcepub fn index(&self) -> usize
pub fn index(&self) -> usize
Examples found in repository?
examples/dump-frames.rs (line 19)
11fn main() -> Result<(), ffmpeg::Error> {
12 ffmpeg::init().unwrap();
13
14 if let Ok(mut ictx) = input(&env::args().nth(1).expect("Cannot open file.")) {
15 let input = ictx
16 .streams()
17 .best(Type::Video)
18 .ok_or(ffmpeg::Error::StreamNotFound)?;
19 let video_stream_index = input.index();
20
21 let context_decoder = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
22 let mut decoder = context_decoder.decoder().video()?;
23
24 let mut scaler = Context::get(
25 decoder.format(),
26 decoder.width(),
27 decoder.height(),
28 Pixel::RGB24,
29 decoder.width(),
30 decoder.height(),
31 Flags::BILINEAR,
32 )?;
33
34 let mut frame_index = 0;
35
36 let mut receive_and_process_decoded_frames =
37 |decoder: &mut ffmpeg::decoder::Video| -> Result<(), ffmpeg::Error> {
38 let mut decoded = Video::empty();
39 while decoder.receive_frame(&mut decoded).is_ok() {
40 let mut rgb_frame = Video::empty();
41 scaler.run(&decoded, &mut rgb_frame)?;
42 save_file(&rgb_frame, frame_index).unwrap();
43 frame_index += 1;
44 }
45 Ok(())
46 };
47
48 for (stream, packet) in ictx.packets() {
49 if stream.index() == video_stream_index {
50 decoder.send_packet(&packet)?;
51 receive_and_process_decoded_frames(&mut decoder)?;
52 }
53 }
54 decoder.send_eof()?;
55 receive_and_process_decoded_frames(&mut decoder)?;
56 }
57
58 Ok(())
59}
More examples
examples/remux.rs (line 46)
7fn main() {
8 let input_file = env::args().nth(1).expect("missing input file");
9 let output_file = env::args().nth(2).expect("missing output file");
10
11 ffmpeg::init().unwrap();
12 log::set_level(log::Level::Warning);
13
14 let mut ictx = format::input(&input_file).unwrap();
15 let mut octx = format::output(&output_file).unwrap();
16
17 let mut stream_mapping = vec![0; ictx.nb_streams() as _];
18 let mut ist_time_bases = vec![Rational(0, 1); ictx.nb_streams() as _];
19 let mut ost_index = 0;
20 for (ist_index, ist) in ictx.streams().enumerate() {
21 let ist_medium = ist.parameters().medium();
22 if ist_medium != media::Type::Audio
23 && ist_medium != media::Type::Video
24 && ist_medium != media::Type::Subtitle
25 {
26 stream_mapping[ist_index] = -1;
27 continue;
28 }
29 stream_mapping[ist_index] = ost_index;
30 ist_time_bases[ist_index] = ist.time_base();
31 ost_index += 1;
32 let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
33 ost.set_parameters(ist.parameters());
34 // We need to set codec_tag to 0 lest we run into incompatible codec tag
35 // issues when muxing into a different container format. Unfortunately
36 // there's no high level API to do this (yet).
37 unsafe {
38 (*ost.parameters().as_mut_ptr()).codec_tag = 0;
39 }
40 }
41
42 octx.set_metadata(ictx.metadata().to_owned());
43 octx.write_header().unwrap();
44
45 for (stream, mut packet) in ictx.packets() {
46 let ist_index = stream.index();
47 let ost_index = stream_mapping[ist_index];
48 if ost_index < 0 {
49 continue;
50 }
51 let ost = octx.stream(ost_index as _).unwrap();
52 packet.rescale_ts(ist_time_bases[ist_index], ost.time_base());
53 packet.set_position(-1);
54 packet.set_stream(ost_index as _);
55 packet.write_interleaved(&mut octx).unwrap();
56 }
57
58 octx.write_trailer().unwrap();
59}
examples/transcode-audio.rs (line 128)
65fn transcoder<P: AsRef<Path> + ?Sized>(
66 ictx: &mut format::context::Input,
67 octx: &mut format::context::Output,
68 path: &P,
69 filter_spec: &str,
70) -> Result<Transcoder, ffmpeg::Error> {
71 let input = ictx
72 .streams()
73 .best(media::Type::Audio)
74 .expect("could not find best audio stream");
75 let context = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
76 let mut decoder = context.decoder().audio()?;
77 let codec = ffmpeg::encoder::find(octx.format().codec(path, media::Type::Audio))
78 .expect("failed to find encoder")
79 .audio()?;
80 let global = octx
81 .format()
82 .flags()
83 .contains(ffmpeg::format::flag::Flags::GLOBAL_HEADER);
84
85 decoder.set_parameters(input.parameters())?;
86
87 let mut output = octx.add_stream(codec)?;
88 let context = ffmpeg::codec::context::Context::from_parameters(output.parameters())?;
89 let mut encoder = context.encoder().audio()?;
90
91 let channel_layout = codec
92 .channel_layouts()
93 .map(|cls| cls.best(decoder.channel_layout().channels()))
94 .unwrap_or(ffmpeg::channel_layout::ChannelLayout::STEREO);
95
96 if global {
97 encoder.set_flags(ffmpeg::codec::flag::Flags::GLOBAL_HEADER);
98 }
99
100 encoder.set_rate(decoder.rate() as i32);
101 encoder.set_channel_layout(channel_layout);
102 #[cfg(not(feature = "ffmpeg_7_0"))]
103 {
104 encoder.set_channels(channel_layout.channels());
105 }
106 encoder.set_format(
107 codec
108 .formats()
109 .expect("unknown supported formats")
110 .next()
111 .unwrap(),
112 );
113 encoder.set_bit_rate(decoder.bit_rate());
114 encoder.set_max_bit_rate(decoder.max_bit_rate());
115
116 encoder.set_time_base((1, decoder.rate() as i32));
117 output.set_time_base((1, decoder.rate() as i32));
118
119 let encoder = encoder.open_as(codec)?;
120 output.set_parameters(&encoder);
121
122 let filter = filter(filter_spec, &decoder, &encoder)?;
123
124 let in_time_base = decoder.time_base();
125 let out_time_base = output.time_base();
126
127 Ok(Transcoder {
128 stream: input.index(),
129 filter,
130 decoder,
131 encoder,
132 in_time_base,
133 out_time_base,
134 })
135}
136
137impl Transcoder {
138 fn send_frame_to_encoder(&mut self, frame: &ffmpeg::Frame) {
139 self.encoder.send_frame(frame).unwrap();
140 }
141
142 fn send_eof_to_encoder(&mut self) {
143 self.encoder.send_eof().unwrap();
144 }
145
146 fn receive_and_process_encoded_packets(&mut self, octx: &mut format::context::Output) {
147 let mut encoded = ffmpeg::Packet::empty();
148 while self.encoder.receive_packet(&mut encoded).is_ok() {
149 encoded.set_stream(0);
150 encoded.rescale_ts(self.in_time_base, self.out_time_base);
151 encoded.write_interleaved(octx).unwrap();
152 }
153 }
154
155 fn add_frame_to_filter(&mut self, frame: &ffmpeg::Frame) {
156 self.filter.get("in").unwrap().source().add(frame).unwrap();
157 }
158
159 fn flush_filter(&mut self) {
160 self.filter.get("in").unwrap().source().flush().unwrap();
161 }
162
163 fn get_and_process_filtered_frames(&mut self, octx: &mut format::context::Output) {
164 let mut filtered = frame::Audio::empty();
165 while self
166 .filter
167 .get("out")
168 .unwrap()
169 .sink()
170 .frame(&mut filtered)
171 .is_ok()
172 {
173 self.send_frame_to_encoder(&filtered);
174 self.receive_and_process_encoded_packets(octx);
175 }
176 }
177
178 fn send_packet_to_decoder(&mut self, packet: &ffmpeg::Packet) {
179 self.decoder.send_packet(packet).unwrap();
180 }
181
182 fn send_eof_to_decoder(&mut self) {
183 self.decoder.send_eof().unwrap();
184 }
185
186 fn receive_and_process_decoded_frames(&mut self, octx: &mut format::context::Output) {
187 let mut decoded = frame::Audio::empty();
188 while self.decoder.receive_frame(&mut decoded).is_ok() {
189 let timestamp = decoded.timestamp();
190 decoded.set_pts(timestamp);
191 self.add_frame_to_filter(&decoded);
192 self.get_and_process_filtered_frames(octx);
193 }
194 }
195}
196
197// Transcode the `best` audio stream of the input file into a the output file while applying a
198// given filter. If no filter was specified the stream gets copied (`anull` filter).
199//
200// Example 1: Transcode *.mp3 file to *.wmv while speeding it up
201// transcode-audio in.mp3 out.wmv "atempo=1.2"
202//
203// Example 2: Overlay an audio file
204// transcode-audio in.mp3 out.mp3 "amovie=overlay.mp3 [ov]; [in][ov] amerge [out]"
205//
206// Example 3: Seek to a specified position (in seconds)
207// transcode-audio in.mp3 out.mp3 anull 30
208fn main() {
209 ffmpeg::init().unwrap();
210
211 let input = env::args().nth(1).expect("missing input");
212 let output = env::args().nth(2).expect("missing output");
213 let filter = env::args().nth(3).unwrap_or_else(|| "anull".to_owned());
214 let seek = env::args().nth(4).and_then(|s| s.parse::<i64>().ok());
215
216 let mut ictx = format::input(&input).unwrap();
217 let mut octx = format::output(&output).unwrap();
218 let mut transcoder = transcoder(&mut ictx, &mut octx, &output, &filter).unwrap();
219
220 if let Some(position) = seek {
221 // If the position was given in seconds, rescale it to ffmpegs base timebase.
222 let position = position.rescale((1, 1), rescale::TIME_BASE);
223 // If this seek was embedded in the transcoding loop, a call of `flush()`
224 // for every opened buffer after the successful seek would be advisable.
225 ictx.seek(position, ..position).unwrap();
226 }
227
228 octx.set_metadata(ictx.metadata().to_owned());
229 octx.write_header().unwrap();
230
231 for (stream, mut packet) in ictx.packets() {
232 if stream.index() == transcoder.stream {
233 packet.rescale_ts(stream.time_base(), transcoder.in_time_base);
234 transcoder.send_packet_to_decoder(&packet);
235 transcoder.receive_and_process_decoded_frames(&mut octx);
236 }
237 }
238
239 transcoder.send_eof_to_decoder();
240 transcoder.receive_and_process_decoded_frames(&mut octx);
241
242 transcoder.flush_filter();
243 transcoder.get_and_process_filtered_frames(&mut octx);
244
245 transcoder.send_eof_to_encoder();
246 transcoder.receive_and_process_encoded_packets(&mut octx);
247
248 octx.write_trailer().unwrap();
249}
examples/transcode-x264.rs (line 192)
169fn main() {
170 let input_file = env::args().nth(1).expect("missing input file");
171 let output_file = env::args().nth(2).expect("missing output file");
172 let x264_opts = parse_opts(
173 env::args()
174 .nth(3)
175 .unwrap_or_else(|| DEFAULT_X264_OPTS.to_string()),
176 )
177 .expect("invalid x264 options string");
178
179 eprintln!("x264 options: {:?}", x264_opts);
180
181 ffmpeg::init().unwrap();
182 log::set_level(log::Level::Info);
183
184 let mut ictx = format::input(&input_file).unwrap();
185 let mut octx = format::output(&output_file).unwrap();
186
187 format::context::input::dump(&ictx, 0, Some(&input_file));
188
189 let best_video_stream_index = ictx
190 .streams()
191 .best(media::Type::Video)
192 .map(|stream| stream.index());
193 let mut stream_mapping: Vec<isize> = vec![0; ictx.nb_streams() as _];
194 let mut ist_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
195 let mut ost_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
196 let mut transcoders = HashMap::new();
197 let mut ost_index = 0;
198 for (ist_index, ist) in ictx.streams().enumerate() {
199 let ist_medium = ist.parameters().medium();
200 if ist_medium != media::Type::Audio
201 && ist_medium != media::Type::Video
202 && ist_medium != media::Type::Subtitle
203 {
204 stream_mapping[ist_index] = -1;
205 continue;
206 }
207 stream_mapping[ist_index] = ost_index;
208 ist_time_bases[ist_index] = ist.time_base();
209 if ist_medium == media::Type::Video {
210 // Initialize transcoder for video stream.
211 transcoders.insert(
212 ist_index,
213 Transcoder::new(
214 &ist,
215 &mut octx,
216 ost_index as _,
217 x264_opts.to_owned(),
218 Some(ist_index) == best_video_stream_index,
219 )
220 .unwrap(),
221 );
222 } else {
223 // Set up for stream copy for non-video stream.
224 let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
225 ost.set_parameters(ist.parameters());
226 // We need to set codec_tag to 0 lest we run into incompatible codec tag
227 // issues when muxing into a different container format. Unfortunately
228 // there's no high level API to do this (yet).
229 unsafe {
230 (*ost.parameters().as_mut_ptr()).codec_tag = 0;
231 }
232 }
233 ost_index += 1;
234 }
235
236 octx.set_metadata(ictx.metadata().to_owned());
237 format::context::output::dump(&octx, 0, Some(&output_file));
238 octx.write_header().unwrap();
239
240 for (ost_index, _) in octx.streams().enumerate() {
241 ost_time_bases[ost_index] = octx.stream(ost_index as _).unwrap().time_base();
242 }
243
244 for (stream, mut packet) in ictx.packets() {
245 let ist_index = stream.index();
246 let ost_index = stream_mapping[ist_index];
247 if ost_index < 0 {
248 continue;
249 }
250 let ost_time_base = ost_time_bases[ost_index as usize];
251 match transcoders.get_mut(&ist_index) {
252 Some(transcoder) => {
253 transcoder.send_packet_to_decoder(&packet);
254 transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
255 }
256 None => {
257 // Do stream copy on non-video streams.
258 packet.rescale_ts(ist_time_bases[ist_index], ost_time_base);
259 packet.set_position(-1);
260 packet.set_stream(ost_index as _);
261 packet.write_interleaved(&mut octx).unwrap();
262 }
263 }
264 }
265
266 // Flush encoders and decoders.
267 for (ost_index, transcoder) in transcoders.iter_mut() {
268 let ost_time_base = ost_time_bases[*ost_index];
269 transcoder.send_eof_to_decoder();
270 transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
271 transcoder.send_eof_to_encoder();
272 transcoder.receive_and_process_encoded_packets(&mut octx, ost_time_base);
273 }
274
275 octx.write_trailer().unwrap();
276}
examples/metadata.rs (line 15)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
Sourcepub fn time_base(&self) -> Rational
pub fn time_base(&self) -> Rational
Examples found in repository?
examples/transcode-x264.rs (line 68)
43 fn new(
44 ist: &format::stream::Stream,
45 octx: &mut format::context::Output,
46 ost_index: usize,
47 x264_opts: Dictionary,
48 enable_logging: bool,
49 ) -> Result<Self, ffmpeg::Error> {
50 let global_header = octx.format().flags().contains(format::Flags::GLOBAL_HEADER);
51 let decoder = ffmpeg::codec::context::Context::from_parameters(ist.parameters())?
52 .decoder()
53 .video()?;
54
55 let codec = encoder::find(codec::Id::H264);
56 let mut ost = octx.add_stream(codec)?;
57
58 let mut encoder =
59 codec::context::Context::new_with_codec(codec.ok_or(ffmpeg::Error::InvalidData)?)
60 .encoder()
61 .video()?;
62 ost.set_parameters(&encoder);
63 encoder.set_height(decoder.height());
64 encoder.set_width(decoder.width());
65 encoder.set_aspect_ratio(decoder.aspect_ratio());
66 encoder.set_format(decoder.format());
67 encoder.set_frame_rate(decoder.frame_rate());
68 encoder.set_time_base(ist.time_base());
69
70 if global_header {
71 encoder.set_flags(codec::Flags::GLOBAL_HEADER);
72 }
73
74 let opened_encoder = encoder
75 .open_with(x264_opts)
76 .expect("error opening x264 with supplied settings");
77 ost.set_parameters(&opened_encoder);
78 Ok(Self {
79 ost_index,
80 decoder,
81 input_time_base: ist.time_base(),
82 encoder: opened_encoder,
83 logging_enabled: enable_logging,
84 frame_count: 0,
85 last_log_frame_count: 0,
86 starting_time: Instant::now(),
87 last_log_time: Instant::now(),
88 })
89 }
90
91 fn send_packet_to_decoder(&mut self, packet: &Packet) {
92 self.decoder.send_packet(packet).unwrap();
93 }
94
95 fn send_eof_to_decoder(&mut self) {
96 self.decoder.send_eof().unwrap();
97 }
98
99 fn receive_and_process_decoded_frames(
100 &mut self,
101 octx: &mut format::context::Output,
102 ost_time_base: Rational,
103 ) {
104 let mut frame = frame::Video::empty();
105 while self.decoder.receive_frame(&mut frame).is_ok() {
106 self.frame_count += 1;
107 let timestamp = frame.timestamp();
108 self.log_progress(f64::from(
109 Rational(timestamp.unwrap_or(0) as i32, 1) * self.input_time_base,
110 ));
111 frame.set_pts(timestamp);
112 frame.set_kind(picture::Type::None);
113 self.send_frame_to_encoder(&frame);
114 self.receive_and_process_encoded_packets(octx, ost_time_base);
115 }
116 }
117
118 fn send_frame_to_encoder(&mut self, frame: &frame::Video) {
119 self.encoder.send_frame(frame).unwrap();
120 }
121
122 fn send_eof_to_encoder(&mut self) {
123 self.encoder.send_eof().unwrap();
124 }
125
126 fn receive_and_process_encoded_packets(
127 &mut self,
128 octx: &mut format::context::Output,
129 ost_time_base: Rational,
130 ) {
131 let mut encoded = Packet::empty();
132 while self.encoder.receive_packet(&mut encoded).is_ok() {
133 encoded.set_stream(self.ost_index);
134 encoded.rescale_ts(self.input_time_base, ost_time_base);
135 encoded.write_interleaved(octx).unwrap();
136 }
137 }
138
139 fn log_progress(&mut self, timestamp: f64) {
140 if !self.logging_enabled
141 || (self.frame_count - self.last_log_frame_count < 100
142 && self.last_log_time.elapsed().as_secs_f64() < 1.0)
143 {
144 return;
145 }
146 eprintln!(
147 "time elpased: \t{:8.2}\tframe count: {:8}\ttimestamp: {:8.2}",
148 self.starting_time.elapsed().as_secs_f64(),
149 self.frame_count,
150 timestamp
151 );
152 self.last_log_frame_count = self.frame_count;
153 self.last_log_time = Instant::now();
154 }
155}
156
157fn parse_opts<'a>(s: String) -> Option<Dictionary<'a>> {
158 let mut dict = Dictionary::new();
159 for keyval in s.split_terminator(',') {
160 let tokens: Vec<&str> = keyval.split('=').collect();
161 match tokens[..] {
162 [key, val] => dict.set(key, val),
163 _ => return None,
164 }
165 }
166 Some(dict)
167}
168
169fn main() {
170 let input_file = env::args().nth(1).expect("missing input file");
171 let output_file = env::args().nth(2).expect("missing output file");
172 let x264_opts = parse_opts(
173 env::args()
174 .nth(3)
175 .unwrap_or_else(|| DEFAULT_X264_OPTS.to_string()),
176 )
177 .expect("invalid x264 options string");
178
179 eprintln!("x264 options: {:?}", x264_opts);
180
181 ffmpeg::init().unwrap();
182 log::set_level(log::Level::Info);
183
184 let mut ictx = format::input(&input_file).unwrap();
185 let mut octx = format::output(&output_file).unwrap();
186
187 format::context::input::dump(&ictx, 0, Some(&input_file));
188
189 let best_video_stream_index = ictx
190 .streams()
191 .best(media::Type::Video)
192 .map(|stream| stream.index());
193 let mut stream_mapping: Vec<isize> = vec![0; ictx.nb_streams() as _];
194 let mut ist_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
195 let mut ost_time_bases = vec![Rational(0, 0); ictx.nb_streams() as _];
196 let mut transcoders = HashMap::new();
197 let mut ost_index = 0;
198 for (ist_index, ist) in ictx.streams().enumerate() {
199 let ist_medium = ist.parameters().medium();
200 if ist_medium != media::Type::Audio
201 && ist_medium != media::Type::Video
202 && ist_medium != media::Type::Subtitle
203 {
204 stream_mapping[ist_index] = -1;
205 continue;
206 }
207 stream_mapping[ist_index] = ost_index;
208 ist_time_bases[ist_index] = ist.time_base();
209 if ist_medium == media::Type::Video {
210 // Initialize transcoder for video stream.
211 transcoders.insert(
212 ist_index,
213 Transcoder::new(
214 &ist,
215 &mut octx,
216 ost_index as _,
217 x264_opts.to_owned(),
218 Some(ist_index) == best_video_stream_index,
219 )
220 .unwrap(),
221 );
222 } else {
223 // Set up for stream copy for non-video stream.
224 let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
225 ost.set_parameters(ist.parameters());
226 // We need to set codec_tag to 0 lest we run into incompatible codec tag
227 // issues when muxing into a different container format. Unfortunately
228 // there's no high level API to do this (yet).
229 unsafe {
230 (*ost.parameters().as_mut_ptr()).codec_tag = 0;
231 }
232 }
233 ost_index += 1;
234 }
235
236 octx.set_metadata(ictx.metadata().to_owned());
237 format::context::output::dump(&octx, 0, Some(&output_file));
238 octx.write_header().unwrap();
239
240 for (ost_index, _) in octx.streams().enumerate() {
241 ost_time_bases[ost_index] = octx.stream(ost_index as _).unwrap().time_base();
242 }
243
244 for (stream, mut packet) in ictx.packets() {
245 let ist_index = stream.index();
246 let ost_index = stream_mapping[ist_index];
247 if ost_index < 0 {
248 continue;
249 }
250 let ost_time_base = ost_time_bases[ost_index as usize];
251 match transcoders.get_mut(&ist_index) {
252 Some(transcoder) => {
253 transcoder.send_packet_to_decoder(&packet);
254 transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
255 }
256 None => {
257 // Do stream copy on non-video streams.
258 packet.rescale_ts(ist_time_bases[ist_index], ost_time_base);
259 packet.set_position(-1);
260 packet.set_stream(ost_index as _);
261 packet.write_interleaved(&mut octx).unwrap();
262 }
263 }
264 }
265
266 // Flush encoders and decoders.
267 for (ost_index, transcoder) in transcoders.iter_mut() {
268 let ost_time_base = ost_time_bases[*ost_index];
269 transcoder.send_eof_to_decoder();
270 transcoder.receive_and_process_decoded_frames(&mut octx, ost_time_base);
271 transcoder.send_eof_to_encoder();
272 transcoder.receive_and_process_encoded_packets(&mut octx, ost_time_base);
273 }
274
275 octx.write_trailer().unwrap();
276}
More examples
examples/remux.rs (line 30)
7fn main() {
8 let input_file = env::args().nth(1).expect("missing input file");
9 let output_file = env::args().nth(2).expect("missing output file");
10
11 ffmpeg::init().unwrap();
12 log::set_level(log::Level::Warning);
13
14 let mut ictx = format::input(&input_file).unwrap();
15 let mut octx = format::output(&output_file).unwrap();
16
17 let mut stream_mapping = vec![0; ictx.nb_streams() as _];
18 let mut ist_time_bases = vec![Rational(0, 1); ictx.nb_streams() as _];
19 let mut ost_index = 0;
20 for (ist_index, ist) in ictx.streams().enumerate() {
21 let ist_medium = ist.parameters().medium();
22 if ist_medium != media::Type::Audio
23 && ist_medium != media::Type::Video
24 && ist_medium != media::Type::Subtitle
25 {
26 stream_mapping[ist_index] = -1;
27 continue;
28 }
29 stream_mapping[ist_index] = ost_index;
30 ist_time_bases[ist_index] = ist.time_base();
31 ost_index += 1;
32 let mut ost = octx.add_stream(encoder::find(codec::Id::None)).unwrap();
33 ost.set_parameters(ist.parameters());
34 // We need to set codec_tag to 0 lest we run into incompatible codec tag
35 // issues when muxing into a different container format. Unfortunately
36 // there's no high level API to do this (yet).
37 unsafe {
38 (*ost.parameters().as_mut_ptr()).codec_tag = 0;
39 }
40 }
41
42 octx.set_metadata(ictx.metadata().to_owned());
43 octx.write_header().unwrap();
44
45 for (stream, mut packet) in ictx.packets() {
46 let ist_index = stream.index();
47 let ost_index = stream_mapping[ist_index];
48 if ost_index < 0 {
49 continue;
50 }
51 let ost = octx.stream(ost_index as _).unwrap();
52 packet.rescale_ts(ist_time_bases[ist_index], ost.time_base());
53 packet.set_position(-1);
54 packet.set_stream(ost_index as _);
55 packet.write_interleaved(&mut octx).unwrap();
56 }
57
58 octx.write_trailer().unwrap();
59}
examples/transcode-audio.rs (line 125)
65fn transcoder<P: AsRef<Path> + ?Sized>(
66 ictx: &mut format::context::Input,
67 octx: &mut format::context::Output,
68 path: &P,
69 filter_spec: &str,
70) -> Result<Transcoder, ffmpeg::Error> {
71 let input = ictx
72 .streams()
73 .best(media::Type::Audio)
74 .expect("could not find best audio stream");
75 let context = ffmpeg::codec::context::Context::from_parameters(input.parameters())?;
76 let mut decoder = context.decoder().audio()?;
77 let codec = ffmpeg::encoder::find(octx.format().codec(path, media::Type::Audio))
78 .expect("failed to find encoder")
79 .audio()?;
80 let global = octx
81 .format()
82 .flags()
83 .contains(ffmpeg::format::flag::Flags::GLOBAL_HEADER);
84
85 decoder.set_parameters(input.parameters())?;
86
87 let mut output = octx.add_stream(codec)?;
88 let context = ffmpeg::codec::context::Context::from_parameters(output.parameters())?;
89 let mut encoder = context.encoder().audio()?;
90
91 let channel_layout = codec
92 .channel_layouts()
93 .map(|cls| cls.best(decoder.channel_layout().channels()))
94 .unwrap_or(ffmpeg::channel_layout::ChannelLayout::STEREO);
95
96 if global {
97 encoder.set_flags(ffmpeg::codec::flag::Flags::GLOBAL_HEADER);
98 }
99
100 encoder.set_rate(decoder.rate() as i32);
101 encoder.set_channel_layout(channel_layout);
102 #[cfg(not(feature = "ffmpeg_7_0"))]
103 {
104 encoder.set_channels(channel_layout.channels());
105 }
106 encoder.set_format(
107 codec
108 .formats()
109 .expect("unknown supported formats")
110 .next()
111 .unwrap(),
112 );
113 encoder.set_bit_rate(decoder.bit_rate());
114 encoder.set_max_bit_rate(decoder.max_bit_rate());
115
116 encoder.set_time_base((1, decoder.rate() as i32));
117 output.set_time_base((1, decoder.rate() as i32));
118
119 let encoder = encoder.open_as(codec)?;
120 output.set_parameters(&encoder);
121
122 let filter = filter(filter_spec, &decoder, &encoder)?;
123
124 let in_time_base = decoder.time_base();
125 let out_time_base = output.time_base();
126
127 Ok(Transcoder {
128 stream: input.index(),
129 filter,
130 decoder,
131 encoder,
132 in_time_base,
133 out_time_base,
134 })
135}
136
137impl Transcoder {
138 fn send_frame_to_encoder(&mut self, frame: &ffmpeg::Frame) {
139 self.encoder.send_frame(frame).unwrap();
140 }
141
142 fn send_eof_to_encoder(&mut self) {
143 self.encoder.send_eof().unwrap();
144 }
145
146 fn receive_and_process_encoded_packets(&mut self, octx: &mut format::context::Output) {
147 let mut encoded = ffmpeg::Packet::empty();
148 while self.encoder.receive_packet(&mut encoded).is_ok() {
149 encoded.set_stream(0);
150 encoded.rescale_ts(self.in_time_base, self.out_time_base);
151 encoded.write_interleaved(octx).unwrap();
152 }
153 }
154
155 fn add_frame_to_filter(&mut self, frame: &ffmpeg::Frame) {
156 self.filter.get("in").unwrap().source().add(frame).unwrap();
157 }
158
159 fn flush_filter(&mut self) {
160 self.filter.get("in").unwrap().source().flush().unwrap();
161 }
162
163 fn get_and_process_filtered_frames(&mut self, octx: &mut format::context::Output) {
164 let mut filtered = frame::Audio::empty();
165 while self
166 .filter
167 .get("out")
168 .unwrap()
169 .sink()
170 .frame(&mut filtered)
171 .is_ok()
172 {
173 self.send_frame_to_encoder(&filtered);
174 self.receive_and_process_encoded_packets(octx);
175 }
176 }
177
178 fn send_packet_to_decoder(&mut self, packet: &ffmpeg::Packet) {
179 self.decoder.send_packet(packet).unwrap();
180 }
181
182 fn send_eof_to_decoder(&mut self) {
183 self.decoder.send_eof().unwrap();
184 }
185
186 fn receive_and_process_decoded_frames(&mut self, octx: &mut format::context::Output) {
187 let mut decoded = frame::Audio::empty();
188 while self.decoder.receive_frame(&mut decoded).is_ok() {
189 let timestamp = decoded.timestamp();
190 decoded.set_pts(timestamp);
191 self.add_frame_to_filter(&decoded);
192 self.get_and_process_filtered_frames(octx);
193 }
194 }
195}
196
197// Transcode the `best` audio stream of the input file into a the output file while applying a
198// given filter. If no filter was specified the stream gets copied (`anull` filter).
199//
200// Example 1: Transcode *.mp3 file to *.wmv while speeding it up
201// transcode-audio in.mp3 out.wmv "atempo=1.2"
202//
203// Example 2: Overlay an audio file
204// transcode-audio in.mp3 out.mp3 "amovie=overlay.mp3 [ov]; [in][ov] amerge [out]"
205//
206// Example 3: Seek to a specified position (in seconds)
207// transcode-audio in.mp3 out.mp3 anull 30
208fn main() {
209 ffmpeg::init().unwrap();
210
211 let input = env::args().nth(1).expect("missing input");
212 let output = env::args().nth(2).expect("missing output");
213 let filter = env::args().nth(3).unwrap_or_else(|| "anull".to_owned());
214 let seek = env::args().nth(4).and_then(|s| s.parse::<i64>().ok());
215
216 let mut ictx = format::input(&input).unwrap();
217 let mut octx = format::output(&output).unwrap();
218 let mut transcoder = transcoder(&mut ictx, &mut octx, &output, &filter).unwrap();
219
220 if let Some(position) = seek {
221 // If the position was given in seconds, rescale it to ffmpegs base timebase.
222 let position = position.rescale((1, 1), rescale::TIME_BASE);
223 // If this seek was embedded in the transcoding loop, a call of `flush()`
224 // for every opened buffer after the successful seek would be advisable.
225 ictx.seek(position, ..position).unwrap();
226 }
227
228 octx.set_metadata(ictx.metadata().to_owned());
229 octx.write_header().unwrap();
230
231 for (stream, mut packet) in ictx.packets() {
232 if stream.index() == transcoder.stream {
233 packet.rescale_ts(stream.time_base(), transcoder.in_time_base);
234 transcoder.send_packet_to_decoder(&packet);
235 transcoder.receive_and_process_decoded_frames(&mut octx);
236 }
237 }
238
239 transcoder.send_eof_to_decoder();
240 transcoder.receive_and_process_decoded_frames(&mut octx);
241
242 transcoder.flush_filter();
243 transcoder.get_and_process_filtered_frames(&mut octx);
244
245 transcoder.send_eof_to_encoder();
246 transcoder.receive_and_process_encoded_packets(&mut octx);
247
248 octx.write_trailer().unwrap();
249}
examples/metadata.rs (line 33)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
Sourcepub fn start_time(&self) -> i64
pub fn start_time(&self) -> i64
Examples found in repository?
examples/metadata.rs (line 34)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
Sourcepub fn duration(&self) -> i64
pub fn duration(&self) -> i64
Examples found in repository?
examples/metadata.rs (line 35)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
Sourcepub fn frames(&self) -> i64
pub fn frames(&self) -> i64
Examples found in repository?
examples/metadata.rs (line 40)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
Sourcepub fn disposition(&self) -> Disposition
pub fn disposition(&self) -> Disposition
Examples found in repository?
examples/metadata.rs (line 41)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
Sourcepub fn discard(&self) -> Discard
pub fn discard(&self) -> Discard
Examples found in repository?
examples/metadata.rs (line 42)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
pub fn side_data(&self) -> SideDataIter<'_>
Sourcepub fn rate(&self) -> Rational
pub fn rate(&self) -> Rational
Examples found in repository?
examples/metadata.rs (line 43)
5fn main() -> Result<(), ffmpeg::Error> {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing file")) {
9 Ok(context) => {
10 for (k, v) in context.metadata().iter() {
11 println!("{}: {}", k, v);
12 }
13
14 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Video) {
15 println!("Best video stream index: {}", stream.index());
16 }
17
18 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Audio) {
19 println!("Best audio stream index: {}", stream.index());
20 }
21
22 if let Some(stream) = context.streams().best(ffmpeg::media::Type::Subtitle) {
23 println!("Best subtitle stream index: {}", stream.index());
24 }
25
26 println!(
27 "duration (seconds): {:.2}",
28 context.duration() as f64 / f64::from(ffmpeg::ffi::AV_TIME_BASE)
29 );
30
31 for stream in context.streams() {
32 println!("stream index {}:", stream.index());
33 println!("\ttime_base: {}", stream.time_base());
34 println!("\tstart_time: {}", stream.start_time());
35 println!("\tduration (stream timebase): {}", stream.duration());
36 println!(
37 "\tduration (seconds): {:.2}",
38 stream.duration() as f64 * f64::from(stream.time_base())
39 );
40 println!("\tframes: {}", stream.frames());
41 println!("\tdisposition: {:?}", stream.disposition());
42 println!("\tdiscard: {:?}", stream.discard());
43 println!("\trate: {}", stream.rate());
44
45 let codec = ffmpeg::codec::context::Context::from_parameters(stream.parameters())?;
46 println!("\tmedium: {:?}", codec.medium());
47 println!("\tid: {:?}", codec.id());
48
49 if codec.medium() == ffmpeg::media::Type::Video {
50 if let Ok(video) = codec.decoder().video() {
51 println!("\tbit_rate: {}", video.bit_rate());
52 println!("\tmax_rate: {}", video.max_bit_rate());
53 println!("\tdelay: {}", video.delay());
54 println!("\tvideo.width: {}", video.width());
55 println!("\tvideo.height: {}", video.height());
56 println!("\tvideo.format: {:?}", video.format());
57 println!("\tvideo.has_b_frames: {}", video.has_b_frames());
58 println!("\tvideo.aspect_ratio: {}", video.aspect_ratio());
59 println!("\tvideo.color_space: {:?}", video.color_space());
60 println!("\tvideo.color_range: {:?}", video.color_range());
61 println!("\tvideo.color_primaries: {:?}", video.color_primaries());
62 println!(
63 "\tvideo.color_transfer_characteristic: {:?}",
64 video.color_transfer_characteristic()
65 );
66 println!("\tvideo.chroma_location: {:?}", video.chroma_location());
67 println!("\tvideo.references: {}", video.references());
68 println!("\tvideo.intra_dc_precision: {}", video.intra_dc_precision());
69 }
70 } else if codec.medium() == ffmpeg::media::Type::Audio {
71 if let Ok(audio) = codec.decoder().audio() {
72 println!("\tbit_rate: {}", audio.bit_rate());
73 println!("\tmax_rate: {}", audio.max_bit_rate());
74 println!("\tdelay: {}", audio.delay());
75 println!("\taudio.rate: {}", audio.rate());
76 println!("\taudio.channels: {}", audio.channels());
77 println!("\taudio.format: {:?}", audio.format());
78 println!("\taudio.frames: {}", audio.frames());
79 println!("\taudio.align: {}", audio.align());
80 println!("\taudio.channel_layout: {:?}", audio.channel_layout());
81 }
82 }
83 }
84 }
85
86 Err(error) => println!("error: {}", error),
87 }
88 Ok(())
89}
pub fn avg_frame_rate(&self) -> Rational
pub fn metadata(&self) -> DictionaryRef<'_>
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for StreamMut<'a>
impl<'a> RefUnwindSafe for StreamMut<'a>
impl<'a> !Send for StreamMut<'a>
impl<'a> !Sync for StreamMut<'a>
impl<'a> Unpin for StreamMut<'a>
impl<'a> !UnwindSafe for StreamMut<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more