ffmpeg4 读取图片编码

举报
风吹稻花香 发表于 2021/06/05 00:08:25 2021/06/05
【摘要】  ffmpeg4.1 压缩图片为视频 int save_jpeg(char* filepath, void* vp, void* service) {    Rtmp_tool *rtmp_tool = (Rtmp_tool *)vp;     DemoDevice *pDev = (DemoDevi...

 ffmpeg4.1 压缩图片为视频


  
  1. int save_jpeg(char* filepath, void* vp, void* service) {
  2.     Rtmp_tool *rtmp_tool = (Rtmp_tool *)vp;
  3.     DemoDevice *pDev = (DemoDevice *)service;
  4.     av_log_set_level(AV_LOG_WARNING);
  5.     
  6.     AVCodecContext *c = rtmp_tool->c;
  7.     AVPacket avpkt;
  8.     //AVFrame *m_pRGBFrame = rtmp_tool->m_pRGBFrame;
  9.     //AVFrame *m_pYUVFrame = rtmp_tool->m_pYUVFrame;
  10.     AVPixelFormat pixFormat = AV_PIX_FMT_YUV420P;
  11.     AVFormatContext *avFormatCtx = NULL;
  12.     AVCodecContext *avCodecCtx = NULL;
  13.     AVFrame *pFrameYUV = NULL;
  14.     AVCodec *avCodec = NULL;
  15.     AVPacket packet;
  16.     if (avformat_open_input(&avFormatCtx, filepath, NULL, NULL) != 0)
  17.     {
  18.         printf("phase_1,couldn't open input file\n");
  19.         return -1;
  20.     }
  21.     if (avformat_find_stream_info(avFormatCtx, NULL) < 0)
  22.     {
  23.         printf("phase_1,stream_info error\n");
  24.         return -1;
  25.     }
  26.     int videoStream =0;
  27.     avCodecCtx = avcodec_alloc_context3(NULL);
  28.     avcodec_parameters_to_context(avCodecCtx, avFormatCtx->streams[videoStream]->codecpar);
  29.     avCodec = avcodec_find_decoder(avCodecCtx->codec_id);
  30.     if (avCodec == NULL)
  31.     {
  32.         printf("phase_1,cant find decoder\n");
  33.         return -1;
  34.     }
  35.     if (avcodec_open2(avCodecCtx, avCodec, NULL) < 0)
  36.     {
  37.         printf("phase_1,cant open decoder!\n");
  38.         return -1;
  39.     }
  40.     pFrameYUV = av_frame_alloc();
  41.     if ( pFrameYUV == NULL)
  42.     {
  43.         printf("phase_1,cant open a frame to store data\n");
  44.         return -1;
  45.     }
  46.     int frameFinish = 0;
  47.     int decLen = 0;
  48.     while (av_read_frame(avFormatCtx, &packet) >= 0)
  49.     {
  50.         if (packet.stream_index == videoStream)
  51.         {
  52.             if (avcodec_send_packet(avCodecCtx, &packet) != 0) {
  53.                 cout << "avcodec_send_packet错误" << endl;
  54.                 break;
  55.             }
  56.             while (avcodec_receive_frame(avCodecCtx, pFrameYUV) == 0) {
  57.                 printf("avcodec_receive_frame for encoding\n");
  58.                 avcodec_flush_buffers(avCodecCtx);
  59.                     av_init_packet(&avpkt);// av_packet_alloc();
  60.                     int ret = avcodec_send_frame(c, pFrameYUV);
  61.                     pFrameYUV->pts++;
  62.                     if (ret < 0)
  63.                     {
  64.                         printf("Error sending a frame for encoding\n");
  65.                         exit(1);
  66.                     }
  67.                     while (ret >= 0)
  68.                     {
  69.                         ret = avcodec_receive_packet(c, &avpkt);
  70.                         if (ret != 0) {
  71.                             if (ret == -11 || ret == AVERROR(EAGAIN)) {//-11
  72.                                printf("avcodec_receive_packet AVERROR(EAGAIN) %d\n",AVERROR(EAGAIN));
  73.                             }
  74.                             else if (ret == AVERROR(EINVAL)) {//-22
  75.                                 printf("avcodec_receive_packet AVERROR(EINVAL)\n");
  76.                             }
  77.                             else {
  78.                                 printf("avcodec_receive_packet error %d\n", ret);
  79.                             }
  80.                             av_packet_unref(&avpkt);
  81.                             break;
  82.                         }
  83.                         //printf("Write packet %3(size=%5d)\n", avpkt->pts, avpkt->size);
  84.                         if (!pDev->m_token.empty()) {
  85.                             InfoFrame data_send;
  86.                             data_send.video = CODEC_H264;
  87.                             struct timeval tv;
  88.                             gettimeofday(&tv, NULL);
  89.                             if (pDev->stream_open) {
  90.                                 pDev->send_data(avpkt.data, avpkt.size, tv.tv_sec, tv.tv_usec / 1000, data_send);
  91.                             }
  92.                             //fwrite(avpkt->data, 1, avpkt->size, rtmp_tool->f);
  93.                         }
  94.                         av_packet_unref(&avpkt);
  95.                     }
  96.                 
  97.                 //packet.data += decLen;
  98.                 //packet.size -= decLen;
  99.                     av_packet_unref(&packet);
  100.             }
  101.             
  102.         }
  103.         
  104.     }
  105.     
  106.     avCodec->close(avCodecCtx);
  107.     printf("2222\n");
  108.     avcodec_close(avCodecCtx);
  109.     printf("555\n");
  110.     avcodec_free_context(&avCodecCtx);
  111.     printf("66666\n");
  112.     
  113.     avformat_close_input(&avFormatCtx);
  114.     printf("7777\n");
  115.     
  116.     av_frame_free(&pFrameYUV);
  117.     printf("222\n");
  118.     av_packet_unref(&packet);
  119.     printf("send ok\n");
  120.     return 0;
  121. }

 

文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/87364450

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。