From c88dbe1ecfedff3aa234657ca41aeb7cf0e56b24 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Tue, 9 Jul 2024 23:26:26 +0800 Subject: [PATCH] Mux mp4 ok. --- utils/MediaBase/CMakeLists.txt | 5 ++- utils/MediaBase/src/FfmpegDecoder.cpp | 31 +++++++++---------- utils/MediaBase/src/FfmpegEncoder.cpp | 44 +++++++++++++-------------- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/utils/MediaBase/CMakeLists.txt b/utils/MediaBase/CMakeLists.txt index d9cf06c..4965cdd 100644 --- a/utils/MediaBase/CMakeLists.txt +++ b/utils/MediaBase/CMakeLists.txt @@ -20,10 +20,9 @@ aux_source_directory(./src SRC_FILES) set(TARGET_NAME MediaBase) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} avformat avcodec avutil swresample avdevice avfilter swscale postproc fdk-aac StatusCode Log) if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) - target_link_libraries(${TARGET_NAME} avformat avcodec avutil swresample avdevice avfilter swscale postproc fdk-aac z StatusCode Log) -else() - target_link_libraries(${TARGET_NAME} avformat avcodec avutil swresample avdevice avfilter swscale postproc fdk-aac StatusCode Log) + target_link_libraries(${TARGET_NAME} z) endif() add_custom_target( diff --git a/utils/MediaBase/src/FfmpegDecoder.cpp b/utils/MediaBase/src/FfmpegDecoder.cpp index 840dfc2..5e2a97c 100644 --- a/utils/MediaBase/src/FfmpegDecoder.cpp +++ b/utils/MediaBase/src/FfmpegDecoder.cpp @@ -167,24 +167,23 @@ void inline FfmpegDecoder::AVParseData(const void *data, const size_t &size, } } } +// static void save_code_stream_file(const void *data, const size_t &size) +// { +// char OutPath[16]; +// const void *pData = data; +// FILE *file = NULL; -static void save_code_stream_file(const void *data, const size_t &size) -{ - char OutPath[16]; - const void *pData = data; - FILE *file = NULL; +// sprintf(OutPath, "./test.pcm"); +// file = fopen(OutPath, "a+"); - sprintf(OutPath, "./test.pcm"); - file = fopen(OutPath, "a+"); +// if (file) { // TODO: Don't open very time. +// fwrite(pData, 1, size, file); +// fflush(file); +// } - if (file) { // TODO: Don't open very time. - fwrite(pData, 1, size, file); - fflush(file); - } - - if (file) - fclose(file); -} +// if (file) +// fclose(file); +// } void inline FfmpegDecoder::AVDecodeData(AVPacket *pkt, std::function callback) { int ret = avcodec_send_packet(mCodecCtx, pkt); @@ -209,7 +208,7 @@ void inline FfmpegDecoder::AVDecodeData(AVPacket *pkt, std::functionch_layout.nb_channels; ch++) // // fwrite(frame->data[ch] + data_size * i, 1, data_size, outfile); // save_code_stream_file(mFrame->data[ch] + data_size * i, data_size); - save_code_stream_file(mFrame->data[0], mFrame->linesize[0]); + // save_code_stream_file(mFrame->data[0], mFrame->linesize[0]); callback(mFrame); } break; diff --git a/utils/MediaBase/src/FfmpegEncoder.cpp b/utils/MediaBase/src/FfmpegEncoder.cpp index 0e6ec5c..0ff0e91 100644 --- a/utils/MediaBase/src/FfmpegEncoder.cpp +++ b/utils/MediaBase/src/FfmpegEncoder.cpp @@ -22,7 +22,7 @@ extern "C" { #include #include #include -#include +// #include #include #include #include @@ -82,11 +82,11 @@ bool FfmpegEncoder::Init(int &outputFlags) mCodecCtx->sample_rate = 44100; } } - mCodecCtx->sample_rate = SOURCE_AUDIO_SAMPEL_RATE; + mCodecCtx->sample_rate = 16000; // mCodecCtx->time_base = (AVRational){1, mCodecCtx->sample_rate}; - mCodecCtx->ch_layout.nb_channels = 1; - av_channel_layout_default(&mCodecCtx->ch_layout, 1); - // av_channel_layout_copy(&mCodecCtx->ch_layout, &src); + // mCodecCtx->ch_layout.nb_channels = 1; + // av_channel_layout_default(&mCodecCtx->ch_layout, 1); + av_channel_layout_copy(&mCodecCtx->ch_layout, &src); break; case AVMEDIA_TYPE_VIDEO: @@ -170,23 +170,23 @@ bool FfmpegEncoder::OpenEncoder(AVDictionary *optArg, AVStream *stream) return false; } } -static void save_code_stream_file(const void *data, const size_t &size) -{ - char OutPath[16]; - const void *pData = data; - FILE *file = NULL; - LogInfo("save_code_stream_file: %d\n", size); - sprintf(OutPath, "./test.aac"); - file = fopen(OutPath, "a+"); +// static void save_code_stream_file(const void *data, const size_t &size) +// { +// char OutPath[16]; +// const void *pData = data; +// FILE *file = NULL; +// LogInfo("save_code_stream_file: %d\n", size); +// sprintf(OutPath, "./test.aac"); +// file = fopen(OutPath, "a+"); - if (file) { // TODO: Don't open very time. - fwrite(pData, 1, size, file); - fflush(file); - } +// if (file) { // TODO: Don't open very time. +// fwrite(pData, 1, size, file); +// fflush(file); +// } - if (file) - fclose(file); -} +// if (file) +// fclose(file); +// } int FfmpegEncoder::EncodeData(AVFrame *frame, AVStream *stream, std::function callback) { int ret = 0; @@ -300,8 +300,8 @@ bool FfmpegEncoder::OpenAudio(AVDictionary *optArg, AVStream *stream) AVChannelLayout ch_layout; av_channel_layout_copy(&ch_layout, &src); /* set options */ - // av_opt_set_chlayout(mSwrCtx, "in_chlayout", &ch_layout, 0); - av_opt_set_chlayout(mSwrCtx, "in_chlayout", &mCodecCtx->ch_layout, 0); + av_opt_set_chlayout(mSwrCtx, "in_chlayout", &ch_layout, 0); + // av_opt_set_chlayout(mSwrCtx, "in_chlayout", &mCodecCtx->ch_layout, 0); av_opt_set_int(mSwrCtx, "in_sample_rate", SOURCE_AUDIO_SAMPEL_RATE, 0); av_opt_set_sample_fmt(mSwrCtx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0); av_opt_set_chlayout(mSwrCtx, "out_chlayout", &mCodecCtx->ch_layout, 0);