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