Skip to content

Commit 2f35bcc

Browse files
authored
* ensure correct multimedia stream mapping
* flush audio encoder
1 parent f9df28a commit 2f35bcc

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

3_transcoding.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,25 @@ int main(int argc, char *argv[])
328328
avformat_alloc_output_context2(&encoder->avfc, NULL, NULL, encoder->filename);
329329
if (!encoder->avfc) {logging("could not allocate memory for output format");return -1;}
330330

331-
if (!sp.copy_video) {
332-
AVRational input_framerate = av_guess_frame_rate(decoder->avfc, decoder->video_avs, NULL);
333-
prepare_video_encoder(encoder, decoder->video_avcc, input_framerate, sp);
334-
} else {
335-
if (prepare_copy(encoder->avfc, &encoder->video_avs, decoder->video_avs->codecpar)) {return -1;}
336-
}
331+
for (int i = 0; i < decoder->avfc->nb_streams; i++) {
332+
if (decoder->avfc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
333+
{
334+
if (!sp.copy_video) {
335+
AVRational input_framerate = av_guess_frame_rate(decoder->avfc, decoder->video_avs, NULL);
336+
prepare_video_encoder(encoder, decoder->video_avcc, input_framerate, sp);
337+
} else {
338+
if (prepare_copy(encoder->avfc, &encoder->video_avs, decoder->video_avs->codecpar)) {return -1;}
339+
}
340+
}
337341

338-
if (!sp.copy_audio) {
339-
if (prepare_audio_encoder(encoder, decoder->audio_avcc->sample_rate, sp)) {return -1;}
340-
} else {
341-
if (prepare_copy(encoder->avfc, &encoder->audio_avs, decoder->audio_avs->codecpar)) {return -1;}
342+
if (decoder->avfc->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
343+
{
344+
if (!sp.copy_audio) {
345+
if (prepare_audio_encoder(encoder, decoder->audio_avcc->sample_rate, sp)) {return -1;}
346+
} else {
347+
if (prepare_copy(encoder->avfc, &encoder->audio_avs, decoder->audio_avs->codecpar)) {return -1;}
348+
}
349+
}
342350
}
343351

344352
if (encoder->avfc->oformat->flags & AVFMT_GLOBALHEADER)
@@ -386,8 +394,11 @@ int main(int argc, char *argv[])
386394
logging("ignoring all non video or audio packets");
387395
}
388396
}
389-
// TODO: should I also flush the audio encoder?
390-
if (encode_video(decoder, encoder, NULL)) return -1;
397+
398+
if (!sp.copy_video)
399+
if (encode_video(decoder, encoder, NULL)) return -1;
400+
if (!sp.copy_audio)
401+
if (encode_audio(decoder, encoder, NULL)) return -1;
391402

392403
av_write_trailer(encoder->avfc);
393404

0 commit comments

Comments
 (0)