actually write real audio (was sending aframe instead of filtered_aframe to encoder)

and a few other fixes / shufflings
This commit is contained in:
Vivian Lim 2021-04-25 19:31:06 -07:00
parent 7920d944d4
commit 41da935d42
1 changed files with 67 additions and 31 deletions

View File

@ -170,7 +170,7 @@ let wavname = Path::new("out.wav");
let video_filter = video_filter(&video_encoder, &av_info, pix_fmt).unwrap();
let video_encoder = video_encoder.open_as(vcodec).unwrap();
video_output.set_parameters(&video_encoder);
//video_output.set_parameters(&video_encoder);
let mut audio_output = octx.add_stream(acodec).unwrap();
let mut audio_encoder = audio_output.codec().encoder().audio().unwrap();
@ -190,19 +190,22 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
audio_encoder.set_bit_rate(640000);
audio_encoder.set_max_bit_rate(990000);
//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);
audio_encoder.set_format(audio_encoder.codec().unwrap().audio().unwrap().formats().unwrap().nth(0).unwrap());
audio_encoder.set_time_base(Rational::new(1, fps_int));
audio_output.set_time_base(Rational::new(1, fps_int));
let mut audio_encoder = audio_encoder.open_as(acodec).unwrap();
//audio_output.set_parameters(&audio_encoder);
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_output.set_parameters(&audio_encoder);
octx.write_header().unwrap();
ffmpeg::format::context::output::dump(&octx, 0, None);
@ -238,14 +241,30 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
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
loop
{
match match encoder {
EncoderToWriteFrom::Video => self.video_encoder.receive_packet(&mut encoded_packet),
EncoderToWriteFrom::Audio => self.audio_encoder.receive_packet(&mut encoded_packet),
} {
Ok(..) => {
//if encoded_packet.size() > 0 {
encoded_packet.set_stream(stream_index);
eprintln!("Writing packet, pts {:?} dts {:?} size {}", encoded_packet.pts(), encoded_packet.dts(), encoded_packet.size());
match encoded_packet.write(&mut self.octx) {
Ok(..) => eprintln!("Write OK"),
Err(e) => eprintln!("Error writing: {}", e),
}
//encoded_packet.write_interleaved(&mut self.octx).unwrap(); // AAA
//}
//else {
//eprintln!("Did not try to write 0-length packet");
//}
},
Err(e) => {
eprintln!("Error writing packet: {:?}", e);
break;
}
}
}
}
@ -258,14 +277,23 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
self.video_filter.get("in").unwrap().source().add(&vframe).unwrap();
let mut filtered_vframe = frame::Video::empty();
while let Ok(..) = self.video_filter.get("out").unwrap().sink().frame(&mut filtered_vframe) {
if self.video_filter.get("in").unwrap().source().failed_requests() > 0 {
println!("🎥 failed to put filter input frame");
}
filtered_vframe.set_pts(Some(frame));
self.video_encoder.send_frame(&filtered_vframe).unwrap();
loop {
match self.video_filter.get("out").unwrap().sink().frame(&mut filtered_vframe) {
Ok(..) => {
eprintln!("Got filtered video frame");
if self.video_filter.get("in").unwrap().source().failed_requests() > 0 {
println!("🎥 failed to put filter input frame");
}
filtered_vframe.set_pts(Some(frame));
self.video_encoder.send_frame(&filtered_vframe).unwrap();
self.receive_and_write_packets(EncoderToWriteFrom::Video);
self.receive_and_write_packets(EncoderToWriteFrom::Video);
},
Err(e) => {
eprintln!("Error getting filtered video frame: {:?}", e);
break;
}
}
}
@ -282,22 +310,30 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
//eprintln!("src: {:?}, dest: {:?}", self.audio_buf, aplane);
self.audio_buf.clear();
eprintln!("frame audio: {:?}", aframe);
self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap();
let mut filtered_aframe = frame::Audio::empty();
while let Ok(..) = self.audio_filter.get("out").unwrap().sink().frame(&mut filtered_aframe) {
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());
loop {
match self.audio_filter.get("out").unwrap().sink().frame(&mut filtered_aframe) {
Ok(..) => {
eprintln!("Got filtered audio frame {:?}", filtered_aframe);
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);
//filtered_aframe.set_pts(Some(frame));
filtered_aframe.set_pts(Some(frame));
self.audio_encoder.send_frame(&aframe).unwrap();
let mut encoded_packet = ffmpeg::Packet::empty();
self.receive_and_write_packets(EncoderToWriteFrom::Audio);
//filtered_aframe.set_pts(Some(frame));
self.audio_encoder.send_frame(&filtered_aframe).unwrap();
self.receive_and_write_packets(EncoderToWriteFrom::Audio);
},
Err(e) => {
eprintln!("Error getting filtered audio frame: {:?}", e);
break;
}
}
}
}
@ -463,7 +499,7 @@ fn main() -> Fallible<()> {
emu.frame_properties_locked = true;
//for frame in 0..60*10 {
for frame in 0..600 {
for frame in 0..1300 {
eprintln!("🖼️ frame: {}", frame);
emu.run(frame);