Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frame-writer: try to guess format for streaming #42

Merged
merged 1 commit into from
Aug 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/frame-writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,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 @@ -309,13 +319,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 @@ -568,7 +575,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