Qt/C++音视频开发69-保存监控pcm音频数据到mp4文件/监控录像/录像存储和回放/264/265/aac/pcm等

一、前言

用ffmpeg做音视频保存到mp4文件,都会遇到一个问题,尤其是在视频监控行业,就是监控摄像头设置的音频是PCM/G711A/G711U,解码后对应的格式是pcm_s16be/pcm_alaw/pcm_mulaw,将这个原始的音频流保存到mp4文件是会报错的,在调用avformat_write_header写文件头的时候提示(-22) Invalid argument,非法的参数,翻阅源码得知,ffmpeg中的mp4封装并不支持pcma和pcmu,除非手动更改源码加入。mp4封装格式默认支持的音频格式是aac和mp3,其实mp4文件本身是可以支持pcm音频数据的,不知道为何ffmpeg中不加入。通过个更改源码的形式尽管可以支持,个人还是推荐用另外一种方法,那就是在调用avformat_alloc_output_context2的时候传入format的时候填mov,而不是填mp4,mov的格式兼容性更强,文件拓展名依然是mp4一点问题没有。对应avformat_alloc_output_context2函数的说明,format格式参数可以为空,为空的话默认从保存的文件名拓展名取,而如果指定了则以指定的为准。

既然以mov格式存储到mp4文件,那么问题来了,会不会导致文件体积或者格式不兼容呢?一开始我也是有这个担心的,特意找了多个厂家的摄像头专门测试,发现根本没有体积变化,所以个人猜测,填mov只是为了方便跳过检测,MOV文件可以使用多种编码格式,包括MPEG-4、H.264、MJPEG等;而MP4文件主要使用H.264编码。

上面的不仅支持264,同时也支持265,也就是mov格式同时支持264+aac/264+mp3/264+pcm/264+pcma/264+pcmu/265+aac/265+mp3/265+pcm/265+pcma/265+pcmu,这样原始数据保存到文件最好,不用转码重新编码,可以省下不少的CPU,写文件基本上不占CPU,基本上都是磁盘操作,所以性能瓶颈在磁盘读写能力和网络带宽。

二、效果图

五、相关代码

bool FFmpegSaveHelper::rtmp_pcm = false;
QStringList FFmpegSaveHelper::vnames_file = QStringList() << "h264" << "hevc";
QStringList FFmpegSaveHelper::anames_pcm = QStringList() << "pcm_mulaw" << "pcm_alaw" << "pcm_s16be";
QStringList FFmpegSaveHelper::anames_file = QStringList() << "aac" << "mp2" << "mp3" << "ac3" << anames_pcm;
QStringList FFmpegSaveHelper::anames_rtmp = QStringList() << "aac" << "mp3";
QStringList FFmpegSaveHelper::anames_rtsp = QStringList() << "aac" << "mp3" << anames_pcm;

void FFmpegSaveHelper::checkEncode(FFmpegSave *thread, const QString &videoCodecName, const QString &audioCodecName, bool &videoEncode, bool &audioEncode, EncodeAudio &encodeAudio, bool &needAudio)
{
    //推流和录制要区分判断(推流更严格/主要限定在流媒体服务器端)
    bool notSupportVideo = false;
    bool notSupportAudio = false;
    SaveMode saveMode = thread->getSaveMode();
    QString mediaUrl = thread->property("mediaUrl").toString();
    if (saveMode == SaveMode_File) {
        notSupportVideo = !vnames_file.contains(videoCodecName);
        notSupportAudio = !anames_file.contains(audioCodecName);
    } else {
        //具体需要根据实际需求进行调整
        if (saveMode == SaveMode_Rtmp) {
            notSupportVideo = (videoCodecName != "h264");
            notSupportAudio = !anames_rtmp.contains(audioCodecName);
        } else if (saveMode == SaveMode_Rtsp) {
            notSupportVideo = !vnames_file.contains(videoCodecName);
            notSupportAudio = !anames_rtsp.contains(audioCodecName);
        }

        //特定格式过滤
        if (mediaUrl.endsWith(".m3u8")) {
            notSupportAudio = true;
        }
    }

    if (notSupportVideo) {
        thread->debug(0, "视频格式", QString("警告: %1").arg(videoCodecName));
        videoEncode = true;
    }

    if (notSupportAudio) {
        thread->debug(0, "音频格式", QString("警告: %1").arg(audioCodecName));
        audioEncode = true;
    }

    //0. 因为还没有搞定万能转换/所以暂时做下面的限制
    //1. 保存文件模式下纯音频统一编码成pcma
    //2. 保存文件模式下视音频且启用了转码则禁用音频
    //3. 推流RTMP模式下启用了转码则禁用音频
    //4. 推流RTSP模式下纯音频且启用了转码则编码成pcma
    //5. 推流RTSP模式下启用了转码则禁用音频
    //6. 纯音频aac格式在推流的时候可选转码/有些流媒体程序必须要求转码才能用
    bool encodeAac = false;
    bool onlySaveAudio = thread->getOnlySaveAudio();
    bool onlyAac = (onlySaveAudio && audioCodecName == "aac");
    if (encodeAudio == EncodeAudio_Auto) {
        if (saveMode == SaveMode_File) {
            if (onlySaveAudio || audioCodecName == "pcm_s16le") {
                encodeAudio = EncodeAudio_Pcma;
            } else if (audioEncode) {
                needAudio = false;
            }
        } else if (saveMode == SaveMode_Rtmp) {
            if (audioEncode) {
                needAudio = false;
            } else if (onlyAac && encodeAac) {
                encodeAudio = EncodeAudio_Aac;
            }
        } else if (saveMode == SaveMode_Rtsp) {
            if (audioEncode) {
                encodeAudio = EncodeAudio_Pcma;
            } else if (onlyAac && encodeAac) {
                encodeAudio = EncodeAudio_Pcma;
            }
        }
    }

    //如果设置过需要检查B帧/有B帧推流需要转码/否则一卡卡
    if (!videoEncode && !onlySaveAudio && saveMode != SaveMode_File) {
        bool checkB = thread->property("checkB").toBool();
        bool isFile = thread->property("isFile").toBool();
        if (checkB && isFile && FFmpegUtil::hasB(mediaUrl)) {
            videoEncode = true;
        }
    }

    //部分流媒体服务支持推pcma和pcmu
    if (rtmp_pcm && saveMode == SaveMode_Rtmp && anames_pcm.contains(audioCodecName)) {
        needAudio = true;
        encodeAudio = EncodeAudio_Pcma;
    }

    //音频需要强转则必须设置启用音频编码
    if (encodeAudio != EncodeAudio_Auto) {
        audioEncode = true;
    }
}

const char *FFmpegSaveHelper::getFormat(AVDictionary **options, QString &fileName, bool mov, const QString &flag)
{
    //默认是mp4/mov更具兼容性比如音频支持pcma等
    const char *format = mov ? "mov" : "mp4";
    if (fileName.startsWith("rtmp://")) {
        format = "flv";
    } else if (fileName.startsWith("rtsp://")) {
        format = "rtsp";
        av_dict_set(options, "stimeout", "3000000", 0);
        av_dict_set(options, "rtsp_transport", "tcp", 0);
    } else if (fileName.startsWith("udp://")) {
        format = "mpegts";
    } else {
        QByteArray temp;
        if (!flag.isEmpty()) {
            temp = flag.toUtf8();
            format = temp.constData();
            QString suffix = fileName.split(".").last();
            fileName.replace(suffix, flag);
        }
    }

    return format;
}

bool FFmpegSave::initStream()
{
    //如果存在秘钥则启用加密
    AVDictionary *options = NULL;
    FFmpegHelper::initEncryption(&options, this->property("cryptoKey").toByteArray());

    QString flag;
    if (getOnlySaveAudio() && encodeAudio != EncodeAudio_Aac) {
        flag = "wav";
    }

    //既可以是保存到文件也可以是推流(对应格式要区分)
    bool mov = audioCodecName.startsWith("pcm_");
    const char *format = FFmpegSaveHelper::getFormat(&options, fileName, mov, flag);

    //开辟一个格式上下文用来处理视频流输出(末尾url不填则rtsp推流失败)
    QByteArray fileData = fileName.toUtf8();
    const char *url = fileData.data();
    int result = avformat_alloc_output_context2(&formatCtx, NULL, format, url);
    if (result < 0) {
        debug(result, "创建格式", "");
        return false;
    }

    //创建输出视频流
    if (!this->initVideoStream()) {
        goto end;
    }

    //创建输出音频流
    if (!this->initAudioStream()) {
        goto end;
    }

    //打开输出文件
    if (!(formatCtx->oformat->flags & AVFMT_NOFILE)) {
        //记录开始时间并设置回调用于超时判断
        startTime = av_gettime();
        formatCtx->interrupt_callback.callback = FFmpegSaveHelper::openAndWriteCallBack;
        formatCtx->interrupt_callback.opaque = this;

        tryOpen = true;
        result = avio_open2(&formatCtx->pb, url, AVIO_FLAG_WRITE, &formatCtx->interrupt_callback, NULL);
        tryOpen = false;
        if (result < 0) {
            debug(result, "打开输出", "");
            goto end;
        }
    }

    //写入文件开始符
    result = avformat_write_header(formatCtx, &options);
    if (result < 0) {
        debug(result, "写文件头", "");
        goto end;
    }

    writeHeader = true;
    debug(0, "打开输出", QString("格式: %1").arg(format));
    return true;

end:
    //关闭释放并清理文件
    this->close();
    this->deleteFile(fileName);
    return false;
}

bool FFmpegSave::initVideoStream()
{
    if (needVideo) {
        videoIndexOut = 0;
        AVStream *stream = avformat_new_stream(formatCtx, NULL);
        if (!stream) {
            return false;
        }

        //设置旋转角度(没有编码的数据是源头带有旋转角度的/编码后的是正常旋转好的)
        if (!videoEncode) {
            FFmpegHelper::setRotate(stream, rotate);
        }

        //复制解码器上下文参数(不编码从源头流拷贝/编码从设置的编码器拷贝)
        int result = -1;
        if (videoEncode) {
            stream->r_frame_rate = videoCodecCtx->framerate;
            result = FFmpegHelper::copyContext(videoCodecCtx, stream, true);
        } else {
            result = FFmpegHelper::copyContext(videoStreamIn, stream);
        }

        if (result < 0) {
            debug(result, "复制参数", "");
            return false;
        }
    }

    return true;
}

bool FFmpegSave::initAudioStream()
{
    if (needAudio) {
        audioIndexOut = (videoIndexOut == 0 ? 1 : 0);
        AVStream *stream = avformat_new_stream(formatCtx, NULL);
        if (!stream) {
            return false;
        }

        //复制解码器上下文参数(不编码从源头流拷贝/编码从设置的编码器拷贝)
        int result = -1;
        if (audioEncode) {
            result = FFmpegHelper::copyContext(audioCodecCtx, stream, true);
        } else {
            result = FFmpegHelper::copyContext(audioStreamIn, stream);
        }

        if (result < 0) {
            debug(result, "复制参数", "");
            return false;
        }
    }

    return true;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章