Skip to content

Commit 322a6c2

Browse files
authored
transcoder_ffmpeg: add audio or video extraction function (#85)
1 parent 06f5600 commit 322a6c2

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/transcoder/src/transcoder_ffmpeg.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ bool TranscoderFFmpeg::transcode(std::string input_path,
3333
copyAudio = false;
3434
}
3535

36-
open_Media(decoder, encoder);
36+
if(!open_Media(decoder, encoder)){
37+
flag = false;
38+
goto end;
39+
}
3740

3841
if (!prepare_Decoder(decoder)) {
3942
flag = false;
@@ -43,6 +46,10 @@ bool TranscoderFFmpeg::transcode(std::string input_path,
4346
for (int i = 0; i < decoder->fmtCtx->nb_streams; i++) {
4447
if (decoder->fmtCtx->streams[i]->codecpar->codec_type ==
4548
AVMEDIA_TYPE_VIDEO) {
49+
// skip video streams
50+
if (encoder->fmtCtx->oformat->video_codec == AV_CODEC_ID_NONE) {
51+
continue;
52+
}
4653
if (!copyVideo) {
4754
ret = prepare_Encoder_Video(decoder, encoder);
4855
if (ret < 0) {
@@ -54,6 +61,10 @@ bool TranscoderFFmpeg::transcode(std::string input_path,
5461
}
5562
} else if (decoder->fmtCtx->streams[i]->codecpar->codec_type ==
5663
AVMEDIA_TYPE_AUDIO) {
64+
// skip audio streams
65+
if (encoder->fmtCtx->oformat->audio_codec == AV_CODEC_ID_NONE) {
66+
continue;
67+
}
5768
if (!copyAudio) {
5869
ret = prepare_Encoder_Audio(decoder, encoder);
5970
if (ret < 0) {
@@ -86,6 +97,9 @@ bool TranscoderFFmpeg::transcode(std::string input_path,
8697
// read video data from multimedia files to write into destination file
8798
while (av_read_frame(decoder->fmtCtx, decoder->pkt) >= 0) {
8899
if (decoder->pkt->stream_index == decoder->videoIdx) {
100+
if(encoder->fmtCtx->oformat->video_codec == AV_CODEC_ID_NONE) {
101+
continue;
102+
}
89103
if (!copyVideo) {
90104
transcode_Video(decoder, encoder);
91105
} else {
@@ -96,6 +110,9 @@ bool TranscoderFFmpeg::transcode(std::string input_path,
96110
// encode(oFmtCtx, outCodecCtx, outFrame, outPkt, inStream,
97111
// outStream);
98112
} else if (decoder->pkt->stream_index == decoder->audioIdx) {
113+
if(encoder->fmtCtx->oformat->audio_codec == AV_CODEC_ID_NONE) {
114+
continue;
115+
}
99116
if (!copyAudio) {
100117
transcode_Audio(decoder, encoder);
101118
} else {
@@ -580,6 +597,8 @@ bool TranscoderFFmpeg::prepare_Copy(AVFormatContext *avCtx, AVStream **stream,
580597

581598
bool TranscoderFFmpeg::remux(AVPacket *pkt, AVFormatContext *avCtx,
582599
AVStream *inStream, AVStream *outStream) {
600+
// associate the avpacket with the target output avstream
601+
pkt->stream_index = outStream->index;
583602
av_packet_rescale_ts(pkt, inStream->time_base, outStream->time_base);
584603
if (av_interleaved_write_frame(avCtx, pkt) < 0) {
585604
av_log(NULL, AV_LOG_ERROR, "write frame error!\n");

0 commit comments

Comments
 (0)