• FFmpeg将视频包AVPacket通过视频流方式写入本地文件


    1.写视频头

    1. void writeVideoHeader(const char* videoFileName){
    2. int r = avformat_alloc_output_context2(&pFormatCtx, nullptr, nullptr,videoFileName);
    3. if(r < 0){
    4. qDebug()<<"Error: avformat_alloc_output_context2: "<<av_err2str(r);
    5. return;
    6. }
    7. pStream = avformat_new_stream(pFormatCtx,nullptr);
    8. qDebug()<<"创建视频流成功";
    9. pFormatCtx->streams[0]->codecpar->width = 1280;
    10. pFormatCtx->streams[0]->codecpar->height = 720;
    11. AVCodecContext *c= pStream->codec;
    12. c->codec_type = AVMEDIA_TYPE_VIDEO;
    13. c->codec_id = AV_CODEC_ID_H264;
    14. c->bit_rate = 2000000;
    15. c->width = 1080;
    16. c->height = 720;
    17. c->time_base.num = 1;
    18. c->time_base.den = 30;
    19. qDebug()<<"配置解码器选项成功";
    20. r = avio_open(&pFormatCtx->pb, videoFileName, AVIO_FLAG_WRITE);
    21. if(r < 0){
    22. qDebug()<<"Error: avio_open: "<<av_err2str(r);
    23. return;
    24. }
    25. qDebug()<<"打开视频流成功";
    26. r = avformat_write_header(pFormatCtx, nullptr);
    27. if(r < 0){
    28. qDebug()<<"Error: avformat_write_header error"<<av_err2str(r)<
    29. return;
    30. }
    31. qDebug()<<"写入视频头部信息成功";
    32. }

    2.写视频帧

    1. void writeAVFrame(AVPacket *packet){
    2. qDebug()<<"原始视频帧信息:"<pts<dts<size;
    3. int64_t video_max_pts = 0;
    4. av_packet_rescale_ts(packet, pStream->time_base, pStream->time_base);
    5. packet->stream_index = 0;
    6. video_max_pts = qMax(packet->pts, video_max_pts);
    7. qDebug("视频:PTS-> %d %d %d",packet->pts,packet->dts, &pStream->time_base);
    8. qDebug()<<"计算时间基成功";
    9. int r=av_write_frame(pFormatCtx, packet);
    10. if (r<0) {
    11. qDebug()<<"Error: av_write_frame: "<<av_err2str(r);
    12. return;
    13. }
    14. qDebug()<<"写入视频帧成功";
    15. }

    3.关闭视频流

    1. if(pFormatCtx){
    2. av_write_trailer(pFormatCtx);
    3. avio_close(pFormatCtx->pb);
    4. avformat_free_context(pFormatCtx);
    5. }

  • 相关阅读:
    企业公众号怎么做内容?这四个阶段要做好
    vmtouch——Linux下的文件缓存管理神器
    缓存选型:Redis or MemCache
    jzoj1212 重建道路
    算法—6、Z字形变换
    Arm 全面计算解决方案重新定义视觉体验强力赋能移动游戏
    led台灯如何挑选?2022双十一什么样的台灯对眼睛好
    JQuery对Dom的支持
    有趣的opencv-记录图片二值化和相似度实现
    Elasticsearch语法知多少之Match query
  • 原文地址:https://blog.csdn.net/fittec/article/details/136615015