diff --git a/examples/ffmpeg_recorder.rs b/examples/ffmpeg_recorder.rs index f95936e..03237ae 100644 --- a/examples/ffmpeg_recorder.rs +++ b/examples/ffmpeg_recorder.rs @@ -107,6 +107,7 @@ fn audio_filter( .capabilities() .contains(ffmpeg::codec::capabilities::Capabilities::VARIABLE_FRAME_SIZE) { + eprintln!("setting constant frame size {}", audio_encoder.frame_size()); afilter .get("out") .unwrap() @@ -136,7 +137,9 @@ impl MyEmulator { let fps_int = if fps_int == 0 { 60 } else { fps_int }; let detected_vcodec = octx.format().codec(&video_path, media::Type::Video); - let detected_acodec = octx.format().codec(&video_path, media::Type::Audio); + //let detected_acodec = octx.format().codec(&video_path, media::Type::Audio); +let wavname = Path::new("out.wav"); + let detected_acodec = octx.format().codec(&wavname, media::Type::Audio); let vcodec = ffmpeg::encoder::find(detected_vcodec).unwrap().video().unwrap(); let acodec = ffmpeg::encoder::find(detected_acodec).unwrap().audio().unwrap(); @@ -186,8 +189,9 @@ static bool ffmpeg_init_config(struct ff_config_param *params, */ - audio_encoder.set_bit_rate(64000); - audio_encoder.set_rate(44100); + audio_encoder.set_bit_rate(640000); + //audio_encoder.set_rate(44100); + audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32); audio_encoder.set_format(audio_encoder.codec().unwrap().audio().unwrap().formats().unwrap().nth(0).unwrap()); audio_encoder.set_channels(2); audio_encoder.set_channel_layout(ChannelLayout::STEREO); @@ -197,10 +201,11 @@ static bool ffmpeg_init_config(struct ff_config_param *params, let mut audio_encoder = audio_encoder.open_as(acodec).unwrap(); let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap(); - audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32); + //audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32); audio_output.set_parameters(&audio_encoder); octx.write_header().unwrap(); + ffmpeg::format::context::output::dump(&octx, 0, None); let emu = MyEmulator { retro, @@ -225,6 +230,25 @@ static bool ffmpeg_init_config(struct ff_config_param *params, pin_emu.set_system_av_info(av_info); pin_emu } + + fn receive_and_write_packets(&mut self, encoder: EncoderToWriteFrom) + { + let stream_index = match encoder { + EncoderToWriteFrom::Video => 0, + EncoderToWriteFrom::Audio => 1, + }; + let mut encoded_packet = ffmpeg::Packet::empty(); + while let Ok(..) = match encoder { + EncoderToWriteFrom::Video => self.video_encoder.receive_packet(&mut encoded_packet), + EncoderToWriteFrom::Audio => self.audio_encoder.receive_packet(&mut encoded_packet), + } { + if encoded_packet.size() > 0 { + encoded_packet.set_stream(stream_index); + eprintln!("pk pts {:?} dts {:?}", encoded_packet.pts(), encoded_packet.dts()); + encoded_packet.write_interleaved(&mut self.octx).unwrap(); // AAA + } + } + } pub fn run(&mut self, frame: i64) { self.frame += 1; @@ -241,18 +265,9 @@ static bool ffmpeg_init_config(struct ff_config_param *params, filtered_vframe.set_pts(Some(frame)); self.video_encoder.send_frame(&filtered_vframe).unwrap(); - let mut encoded_packet = ffmpeg::Packet::empty(); - while let Ok(..) = self.video_encoder.receive_packet(&mut encoded_packet) { - if encoded_packet.size() > 0 { - encoded_packet.set_stream(0); // use stream index... - //encoded_packet.rescale_ts(Rational(1, 1), Rational(1, 60)); - encoded_packet.write_interleaved(&mut self.octx).unwrap(); // AAA - } - //encoded_packet.write_interleaved(&mut self.octx).unwrap(); // AAA - } + self.receive_and_write_packets(EncoderToWriteFrom::Video); } - eprintln!("Audio buffer length {}", self.audio_buf.len()); let mut aframe = frame::Audio::new( format::Sample::I16(format::sample::Type::Packed), @@ -261,8 +276,8 @@ static bool ffmpeg_init_config(struct ff_config_param *params, ); aframe.set_channels(2); aframe.set_rate(32040); - aframe.set_pts(Some(frame)); let aplane: &mut [(i16, i16)] = aframe.plane_mut(0); + eprintln!("Audio buffer length {} -> {}", self.audio_buf.len(), aplane.len()); aplane.copy_from_slice(self.audio_buf.as_ref()); //eprintln!("src: {:?}, dest: {:?}", self.audio_buf, aplane); self.audio_buf.clear(); @@ -274,17 +289,15 @@ static bool ffmpeg_init_config(struct ff_config_param *params, if self.audio_filter.get("in").unwrap().source().failed_requests() > 0 { println!("🎥 failed to put filter input frame"); } + //let faplane: &[f32] = filtered_aframe.plane(0); + //eprintln!("filtered: {:?}", faplane); + filtered_aframe.set_pts(Some(frame)); + eprintln!("f pts {:?}", filtered_aframe.pts()); filtered_aframe.set_pts(Some(frame)); self.audio_encoder.send_frame(&aframe).unwrap(); let mut encoded_packet = ffmpeg::Packet::empty(); - while let Ok(..) = self.audio_encoder.receive_packet(&mut encoded_packet) { - if encoded_packet.size() > 0 { - encoded_packet.set_stream(1); - //encoded_packet.rescale_ts(Rational(1, 1), Rational(1, 60)); - encoded_packet.write_interleaved(&mut self.octx).unwrap(); // AAA - } - } + self.receive_and_write_packets(EncoderToWriteFrom::Audio); } } @@ -306,6 +319,10 @@ static bool ffmpeg_init_config(struct ff_config_param *params, } pub fn end(&mut self) { + self.video_encoder.send_eof().unwrap(); + self.receive_and_write_packets(EncoderToWriteFrom::Video); + self.audio_encoder.send_eof().unwrap(); + self.receive_and_write_packets(EncoderToWriteFrom::Audio); self.octx.write_trailer().unwrap(); } } @@ -458,3 +475,8 @@ fn main() -> Fallible<()> { //octx.write_trailer().unwrap(); Ok(()) } + +enum EncoderToWriteFrom { + Video, + Audio, +}