QT開發(fā) FFmpeg錄制視頻YUV格式


    // 獲取輸入格式對(duì)象
    AVInputFormat *fmt = av_find_input_format("avfoundation");
    if (!fmt) {
        qDebug() << "av_find_input_format error" <<"avfoundation";
        return;
    }

    // 格式上下文(將來可以利用上下文操作設(shè)備)
    AVFormatContext *ctx = nullptr;

    // 設(shè)備參數(shù)
    AVDictionary *options = nullptr;
    av_dict_set(&options, "video_size", "640x480", 0);
    av_dict_set(&options, "pixel_format", "yuyv422", 0);
    av_dict_set(&options, "framerate", "30", 0);

//    av_dict_set(&options, "video_size", "1280x720", 0);
//    av_dict_set(&options, "pixel_format", "yuyv422", 0);
//    av_dict_set(&options, "framerate", "10", 0);

    // 打開設(shè)備
    int ret = avformat_open_input(&ctx,"0", fmt, &options);
    if (ret < 0) {
        ERROR_BUF(ret);
        qDebug() << "avformat_open_input error" << errbuf;
        return;
    }

    // 文件名
    QFile file("/Users/apple/Desktop/out.yuv");

    // 打開文件
    if (!file.open(QFile::WriteOnly)) {
        qDebug() << "file open error" << FILENAME;

        // 關(guān)閉設(shè)備
        avformat_close_input(&ctx);
        return;
    }

    // 計(jì)算一幀的大小
    AVCodecParameters *params = ctx->streams[0]->codecpar;
    AVPixelFormat pixFmt = (AVPixelFormat) params->format;

    int imageSize = av_image_get_buffer_size(
                        pixFmt,
                        params->width,
                        params->height,
                        1);

//    qDebug() << imageSize;
//    qDebug() << pixFmt << params->width << params->height;
//    qDebug() << av_pix_fmt_desc_get(pixFmt)->name;
//    int pixSize = av_get_bits_per_pixel(av_pix_fmt_desc_get(pixFmt)) >> 3;
//    int imageSize = params->width * params->height * pixSize;

    // 數(shù)據(jù)包
    AVPacket *pkt = av_packet_alloc();
    while (!isInterruptionRequested()) {
        // 不斷采集數(shù)據(jù)
        ret = av_read_frame(ctx, pkt);

        if (ret == 0) { // 讀取成功
            // 將數(shù)據(jù)寫入文件
            file.write((const char *) pkt->data, imageSize);
          // 釋放資源
            av_packet_unref(pkt);
        } else if (ret == AVERROR(EAGAIN)) { // 資源臨時(shí)不可用
            continue;
        } else { // 其他錯(cuò)誤
            ERROR_BUF(ret);
            qDebug() << "av_read_frame error" << errbuf << ret;
            break;
        }
    }

    // 釋放資源
    av_packet_free(&pkt);

    // 關(guān)閉文件
    file.close();

    // 關(guān)閉設(shè)備
    avformat_close_input(&ctx);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容