Skip to content

Commit

Permalink
ffmpegencoder: added more error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmattkc committed May 18, 2021
1 parent a561daf commit 0b1ad0a
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions app/codec/ffmpeg/ffmpegencoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,17 @@ bool FFmpegEncoder::WriteSubtitle(const SubtitleBlock *sub_block)
pkt->dts = pkt->pts;
av_packet_rescale_ts(pkt, av_get_time_base_q(), subtitle_stream_->time_base);

av_interleaved_write_frame(fmt_ctx_, pkt);
int err = av_interleaved_write_frame(fmt_ctx_, pkt);
bool ret = true;

if (err < 0) {
FFmpegError(tr("Failed to write interleaved packet"), err);
ret = false;
}

av_packet_free(&pkt);

return true;
return ret;
}

/*
Expand Down Expand Up @@ -576,7 +582,11 @@ bool FFmpegEncoder::WriteAVFrame(AVFrame *frame, AVCodecContext* codec_ctx, AVSt
av_packet_rescale_ts(pkt, codec_ctx->time_base, stream->time_base);

// Write packet to file
av_interleaved_write_frame(fmt_ctx_, pkt);
error_code = av_interleaved_write_frame(fmt_ctx_, pkt);
if (error_code < 0) {
FFmpegError(tr("Failed to write interleaved packet"), error_code);
goto fail;
}

// Unref packet in case we're getting another
av_packet_unref(pkt);
Expand Down Expand Up @@ -822,6 +832,15 @@ void FFmpegEncoder::FlushEncoders()

FlushCodecCtx(audio_codec_ctx_, audio_stream_);
}

if (fmt_ctx_) {
if (fmt_ctx_->oformat->flags & AVFMT_ALLOW_FLUSH) {
int r = av_interleaved_write_frame(fmt_ctx_, nullptr);
if (r < 0) {
FFmpegError(tr("Failed to write interleaved packet"), r);
}
}
}
}

void FFmpegEncoder::FlushCodecCtx(AVCodecContext *codec_ctx, AVStream* stream)
Expand All @@ -839,7 +858,11 @@ void FFmpegEncoder::FlushCodecCtx(AVCodecContext *codec_ctx, AVStream* stream)

pkt->stream_index = stream->index;
av_packet_rescale_ts(pkt, codec_ctx->time_base, stream->time_base);
av_interleaved_write_frame(fmt_ctx_, pkt);
int r = av_interleaved_write_frame(fmt_ctx_, pkt);
if (r < 0) {
FFmpegError(tr("Failed to write interleaved packet"), r);
break;
}
av_packet_unref(pkt);
} while (error_code >= 0);

Expand Down

0 comments on commit 0b1ad0a

Please sign in to comment.