ffmpeg mux h264 to mp4.

This commit is contained in:
Fancy code 2024-06-21 14:17:55 +08:00
parent 86a441f877
commit fe0b956592

View File

@ -72,8 +72,9 @@ StatusCode MediaBaseImpl::StartReadFile(const std::string &path)
AVFormatContext *pFormatCtx = nullptr;
if ((result = avformat_open_input(&pFormatCtx, path.c_str(), iformat, nullptr)) < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, result);
LogError("Couldn't open file: %s, result=%s\n", path.c_str(), error_str);
LogError("Couldn't open file: %s, result=%s\n",
path.c_str(),
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, result));
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
if (avformat_find_stream_info(pFormatCtx, nullptr) < 0) {
@ -162,8 +163,9 @@ StatusCode MediaBaseImpl::OpenOutputFile(const std::string &fileName)
ret = avio_open(&oc->pb, fileName.c_str(), AVIO_FLAG_WRITE);
if (ret < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
LogInfo("Could not open '%s': %s\n", fileName.c_str(), error_str);
LogInfo("Could not open '%s': %s\n",
fileName.c_str(),
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
}
@ -171,8 +173,8 @@ StatusCode MediaBaseImpl::OpenOutputFile(const std::string &fileName)
ret = avformat_write_header(oc, &opt);
if (ret < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
LogInfo("Error occurred when opening output file: %s\n", error_str);
LogInfo("Error occurred when opening output file: %s\n",
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return CreateStatusCode(STATUS_CODE_OK);
@ -411,8 +413,7 @@ bool MediaBaseImpl::open_video(AVFormatContext *oc, const AVCodec *codec, Output
av_dict_free(&opt);
if (ret < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
LogInfo("Could not open video codec: %s\n", error_str);
LogInfo("Could not open video codec: %s\n", av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
return false;
}
@ -458,8 +459,7 @@ bool MediaBaseImpl::open_audio(AVFormatContext *oc, const AVCodec *codec, Output
av_dict_free(&opt);
if (ret < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
LogInfo("Could not open audio codec: %s\n", error_str);
LogInfo("Could not open audio codec: %s\n", av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
return false;
}
@ -553,23 +553,25 @@ AVFrame *MediaBaseImpl::alloc_frame(enum AVPixelFormat pix_fmt, int width, int h
}
void MediaBaseImpl::InitCodec(AVCodec **codec, AVCodecContext **codec_ctx, AVFrame **frame)
{
int ret = 0;
*codec = (AVCodec *)avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
LogInfo("Codec not found\n");
if (!(*codec)) {
LogError("Codec not found\n");
return;
}
*codec_ctx = avcodec_alloc_context3((const AVCodec *)codec);
if (!codec_ctx) {
LogInfo("Could not allocate codec context\n");
*codec_ctx = avcodec_alloc_context3((const AVCodec *)(*codec));
if (!(*codec_ctx)) {
LogError("Could not allocate codec context\n");
return;
}
if (avcodec_open2(*codec_ctx, *codec, nullptr) < 0) {
LogInfo("Could not open codec\n");
if ((ret = avcodec_open2(*codec_ctx, *codec, nullptr)) < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
LogError("Could not open codec:%s\n", av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
return;
}
*frame = av_frame_alloc();
if (!frame) {
LogInfo("Could not allocate video frame\n");
LogError("Could not allocate video frame\n");
return;
}
}
@ -581,8 +583,8 @@ int MediaBaseImpl::write_frame(AVFormatContext *fmt_ctx, AVCodecContext *c, AVSt
ret = avcodec_send_frame(c, frame);
if (ret < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
LogInfo("Error sending a frame to the encoder: %s\n", error_str);
LogInfo("Error sending a frame to the encoder: %s\n",
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
return -1;
}
@ -593,8 +595,7 @@ int MediaBaseImpl::write_frame(AVFormatContext *fmt_ctx, AVCodecContext *c, AVSt
}
if (ret < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
LogInfo("Error encoding a frame: %s\n", error_str);
LogInfo("Error encoding a frame: %s\n", av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
return -1;
}
@ -610,8 +611,8 @@ int MediaBaseImpl::write_frame(AVFormatContext *fmt_ctx, AVCodecContext *c, AVSt
* This would be different if one used av_write_frame(). */
if (ret < 0) {
char error_str[AV_ERROR_MAX_STRING_SIZE] = {0};
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret);
LogInfo("Error while writing output packet: %s\n", error_str);
LogInfo("Error while writing output packet: %s\n",
av_make_error_string(error_str, AV_ERROR_MAX_STRING_SIZE, ret));
return -1;
}
}