FFmpeg命令行CPU使用率与代码CPU使用率

时间:2018-04-10 00:03:25

标签: ffmpeg rtsp

在我现有的PC / IP Camera组合中,命令行功能

ffmpeg -i rtsp://abcd:123456@1.2.3.4/rtspvideostream /home/pc/video.avi

正确地将视频流写入文件并使用大约30%的CPU。

命令行功能

ffmpeg -i rtsp://abcd:123456@1.2.3.4/rtspvideostream -vcodec copy /home/pc/video.avi

使用大约3%的CPU来获得看似相同的结果。我假设删除与编解码器相关的一些功能有助于节省CPU。

使用以下标准ffmpeg初始化:

AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVDictionary *opts = NULL;
av_dict_set(&opts, "rtsp_transport", "tcp", 0);
avformat_open_input(&pFormatCtx,"rtsp://abcd:123456@1.2.3.4/rtspvideostream", NULL, &opts);
avformat_find_stream_info(pFormatCtx,NULL);
int videoStream = -1;
for(int i=0; i<(int)pFormatCtx->nb_streams; i++)
{
  if(pFormatCtx->streams[i]->codec->codec_type== AVMEDIA_TYPE_VIDEO)
  {
    videoStream=i;
    break;
  }
}
pCodecCtx=pFormatCtx->streams[videoStream]->codec;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
avcodec_open2(pCodecCtx, pCodec, NULL)
AVFrame *pFrame = av_frame_alloc();
int numBytes = avpicture_get_size(AV_PIX_FMT_YUV420P, IMAGEWIDTH, IMAGEHEIGHT);
uint8_t *buffer12 = (uint8_t*)av_malloc(numBytes*sizeof(uint8_t));
avpicture_fill((AVPicture *)pFrame, buffer12, AV_PIX_FMT_YUV420P, IMAGEWIDTH, IMAGEHEIGHT);

标准阅读实施:

int frameFinished = 0;
while(frameFinished == 0)
{
  av_read_frame(pFormatCtx, &packet);
  if(packet.stream_index==videoStream)
  {
    avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
  }
  if( packet.duration ) av_free_packet(&packet);
  packet.duration = 0;
}  

正确获取视频流并使用大约30%的CPU。

在命令行ffmpeg实现中,添加参数'-vcodec copy'会大大降低CPU使用率。对于上面的编码实现,我无法重现CPU使用率的类似下降。

假设有可能,我该怎么办?

1 个答案:

答案 0 :(得分:0)

-vcodec copy意味着视频流按原样复制(它没有被解码/加密),因此CPU使用率是纯I / O.要做同样的事情,你应该从你的代码中删除所有编解码器打开的东西和解码(avcodec_decode_video2),然后直接将数据包写入输出流。