Hi,
I trying to remux H264 rtsp based stream to mp4 file on disk and I have
problem. Its converted without any warnings or errors but stream doesn't
displayed in player(for example vlc).
My code:
//open part
int errnum = 0;
AVOutputFormat *av_out_fmt = av_guess_format(nullptr, dst.c_str(), nullptr);
if (!av_out_fmt)
{
throw std::runtime_error("Can't guess format by passed dest: " + dst);
}
av_out_fmt_ctx = avformat_alloc_context();
av_out_fmt_ctx->oformat = av_out_fmt;
if (!av_out_fmt_ctx)
{
throw std::runtime_error("Could not create output context");
}
for (uint32_t i = 0; i < av_in_fmt_ctx->nb_streams; ++i)
{
if (av_in_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
{
AVStream *in_stream = av_in_fmt_ctx->streams[i];
AVStream *out_stream = avformat_new_stream(av_out_fmt_ctx,
in_stream->codec->codec);
if (!out_stream)
{
throw std::runtime_error("Failed allocating output stream");
}
if ((errnum = avcodec_copy_context(out_stream->codec,
in_stream->codec)) < 0)
{
throw std::runtime_error("Failed to copy context from
input to output stream codec context. Error: " +
AVHelper::error_to_string(errnum));
}
out_stream->time_base = in_stream->time_base;
out_stream->codec->codec_id = in_stream->codec->codec_id;
out_stream->sample_aspect_ratio = in_stream->sample_aspect_ratio;
if (av_out_fmt->flags & AVFMT_GLOBALHEADER)
{
out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
}
}
}
if (!(av_out_fmt->flags & AVFMT_NOFILE))
{
if (avio_open(&av_out_fmt_ctx->pb, dst.c_str(), AVIO_FLAG_WRITE) < 0)
{
throw std::runtime_error("Could not open output: " + dst);
}
}
avformat_write_header(av_out_fmt_ctx, nullptr);
//write part
int av_stream_idx = av_in_pkt->stream_index;
const AVCodecContext *in_codec_ctx =
pimpl->av_in_fmt_ctx->streams[av_stream_idx]->codec;
const AVCodecContext *out_codec_ctx =
pimpl->av_out_fmt_ctx->streams[av_stream_idx]->codec;
assert(in_codec_ctx->codec_id == out_codec_ctx->codec_id);
AVPacket av_out_pkt;
av_init_packet(&av_out_pkt);
AVStream *in_stream = pimpl->av_in_fmt_ctx->streams[av_stream_idx];
AVStream *out_stream = pimpl->av_out_fmt_ctx->streams[av_stream_idx];
av_out_pkt.stream_index = av_in_pkt->stream_index;
av_out_pkt.size = av_in_pkt->size;
av_out_pkt.data = av_in_pkt->data;
av_out_pkt.pts = av_in_pkt->pts == int64_t(AV_NOPTS_VALUE) ? AV_NOPTS_VALUE
: av_rescale_q_rnd(av_in_pkt->pts, in_stream->time_base,
out_stream->time_base, AV_ROUND_NEAR_INF);
av_out_pkt.dts = av_rescale_q_rnd(av_in_pkt->dts,
in_stream->time_base, out_stream->time_base, AV_ROUND_NEAR_INF);
av_out_pkt.duration = av_rescale_q(av_in_pkt->duration,
in_stream->time_base, out_stream->time_base);
av_out_pkt.pos = -1;
int ret = av_interleaved_write_frame(pimpl->av_out_fmt_ctx, av_out_pkt);
//close part
if (av_out_fmt_ctx)
{
if (!av_out_fmt_ctx->pb->error)
{
av_write_trailer(av_out_fmt_ctx);
}
if ((av_out_fmt_ctx->pb && !(av_out_fmt_ctx->oformat->flags &
AVFMT_NOFILE)))
{
avio_closep(&av_out_fmt_ctx->pb);
}
avformat_free_context(av_out_fmt_ctx);
}
Can someone help me with that?
p.s. If i chage mp4 to flv all is ok.
_______________________________________________
libav-api mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-api