pub fn dump(ctx: &Input, index: i32, url: Option<&str>)Examples found in repository?
examples/transcode-x264.rs (line 187)
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}