pub struct Output { /* private fields */ }Implementations§
Source§impl Output
impl Output
pub unsafe fn wrap(ptr: *mut AVFormatContext) -> Self
pub unsafe fn as_ptr(&self) -> *const AVFormatContext
pub unsafe fn as_mut_ptr(&mut self) -> *mut AVFormatContext
Source§impl Output
impl Output
Sourcepub fn format(&self) -> Output
pub fn format(&self) -> Output
Examples found in repository?
examples/transcode-x264.rs (line 50)
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 }More examples
examples/transcode-audio.rs (line 77)
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}Sourcepub fn write_header(&mut self) -> Result<(), Error>
pub fn write_header(&mut self) -> Result<(), Error>
Examples found in repository?
examples/transcode-audio.rs (line 229)
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}More examples
examples/remux.rs (line 43)
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-x264.rs (line 238)
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}pub fn write_header_with( &mut self, options: Dictionary<'_>, ) -> Result<Dictionary<'_>, Error>
Sourcepub fn write_trailer(&mut self) -> Result<(), Error>
pub fn write_trailer(&mut self) -> Result<(), Error>
Examples found in repository?
examples/transcode-audio.rs (line 248)
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}More examples
examples/remux.rs (line 58)
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-x264.rs (line 275)
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}Sourcepub fn add_stream<E: Encoder>(
&mut self,
codec: E,
) -> Result<StreamMut<'_>, Error>
pub fn add_stream<E: Encoder>( &mut self, codec: E, ) -> Result<StreamMut<'_>, Error>
Examples found in repository?
examples/transcode-x264.rs (line 56)
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 32)
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 87)
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 add_stream_with( &mut self, context: &Context, ) -> Result<StreamMut<'_>, Error>
Sourcepub fn add_chapter<R: Into<Rational>, S: AsRef<str>>(
&mut self,
id: i64,
time_base: R,
start: i64,
end: i64,
title: S,
) -> Result<ChapterMut<'_>, Error>
pub fn add_chapter<R: Into<Rational>, S: AsRef<str>>( &mut self, id: i64, time_base: R, start: i64, end: i64, title: S, ) -> Result<ChapterMut<'_>, Error>
Examples found in repository?
examples/chapters.rs (lines 31-37)
5fn main() {
6 ffmpeg::init().unwrap();
7
8 match ffmpeg::format::input(&env::args().nth(1).expect("missing input file name")) {
9 Ok(ictx) => {
10 println!("Nb chapters: {}", ictx.nb_chapters());
11
12 for chapter in ictx.chapters() {
13 println!("chapter id {}:", chapter.id());
14 println!("\ttime_base: {}", chapter.time_base());
15 println!("\tstart: {}", chapter.start());
16 println!("\tend: {}", chapter.end());
17
18 for (k, v) in chapter.metadata().iter() {
19 println!("\t{}: {}", k, v);
20 }
21 }
22
23 let mut octx = ffmpeg::format::output(&"test.mkv").expect("Couldn't open test file");
24
25 for chapter in ictx.chapters() {
26 let title = match chapter.metadata().get("title") {
27 Some(title) => String::from(title),
28 None => String::new(),
29 };
30
31 match octx.add_chapter(
32 chapter.id(),
33 chapter.time_base(),
34 chapter.start(),
35 chapter.end(),
36 &title,
37 ) {
38 Ok(chapter) => println!("Added chapter with id {} to output", chapter.id()),
39 Err(error) => {
40 println!("Error adding chapter with id: {} - {}", chapter.id(), error)
41 }
42 }
43 }
44
45 println!("\nOuput: nb chapters: {}", octx.nb_chapters());
46 for chapter in octx.chapters() {
47 println!("chapter id {}:", chapter.id());
48 println!("\ttime_base: {}", chapter.time_base());
49 println!("\tstart: {}", chapter.start());
50 println!("\tend: {}", chapter.end());
51 for (k, v) in chapter.metadata().iter() {
52 println!("\t{}: {}", k, v);
53 }
54 }
55 }
56
57 Err(error) => println!("error: {}", error),
58 }
59}Sourcepub fn set_metadata(&mut self, dictionary: Dictionary<'_>)
pub fn set_metadata(&mut self, dictionary: Dictionary<'_>)
Examples found in repository?
examples/transcode-audio.rs (line 228)
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}More examples
examples/remux.rs (line 42)
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-x264.rs (line 236)
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}Trait Implementations§
Auto Trait Implementations§
impl Freeze for Output
impl RefUnwindSafe for Output
impl !Sync for Output
impl Unpin for Output
impl UnsafeUnpin for Output
impl UnwindSafe for Output
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