Skip to content

Commit

Permalink
frame-writer: try to guess format for streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
ammen99 committed Jul 12, 2019
1 parent d405083 commit 54a3fb0
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/frame-writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@ void FrameWriter::init_sws(AVPixelFormat format)
}
}

static const char* determine_output_format(const std::string& output_name)
{
if (output_name.find("rtmp") == 0)
return "flv";
if (output_name.find("udp") == 0)
return "mpegts";

return NULL;
}

FrameWriter::FrameWriter(const FrameWriterParams& _params) :
params(_params)
{
Expand All @@ -308,13 +318,10 @@ FrameWriter::FrameWriter(const FrameWriterParams& _params) :
// Preparing the data concerning the format and codec,
// in order to write properly the header, frame data and end of file.
this->outputFmt = av_guess_format(NULL, params.file.c_str(), NULL);
if (!outputFmt)
{
std::cerr << "Failed to guess output format for file " << params.file << std::endl;
std::exit(-1);
}

if (avformat_alloc_output_context2(&this->fmtCtx, NULL, NULL, params.file.c_str()) < 0)
auto streamFormat = determine_output_format(params.file);
auto context_ret = avformat_alloc_output_context2(&this->fmtCtx, NULL,
streamFormat, params.file.c_str());
if (context_ret < 0)
{
std::cerr << "Failed to allocate output context" << std::endl;
std::exit(-1);
Expand Down Expand Up @@ -540,7 +547,7 @@ FrameWriter::~FrameWriter()
av_write_trailer(fmtCtx);

// Closing the file.
if (!(outputFmt->flags & AVFMT_NOFILE))
if (outputFmt && (!(outputFmt->flags & AVFMT_NOFILE)))
avio_closep(&fmtCtx->pb);

avcodec_close(videoStream->codec);
Expand Down

0 comments on commit 54a3fb0

Please sign in to comment.