在學習和使用FFmpeg的時候,我們經常會去查找很多資料並加以實踐,但是目前存在一個問題困擾着不少剛接觸音視頻的同學,那就是FFmpeg的棄用API如何調整。
我們知道FFmpeg中所謂的“被聲明為已否決”就是因為函數或者結構體屬性被標示為attribute_deprecated,很有可能在未來的版本中就刪除了。所以我們最好的解決方案就是使用新的被推薦使用的函數、結構體等。
下面是相關的API的匯總:
1、AVStream::codec: 被聲明為已否決
舊版本:
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
...
pCodecCtx = pFormatCtx->streams[videoIndex]->codec;
新版本:
if(pFormatCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){
...
pCodecCtx = avcodec_alloc_context3(NULL);
if (pCodecCtx == NULL)
{
printf("Could not allocate AVCodecContext \n");
return -1;
}
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
printf("Couldn't find audio stream information \n");
return -1;
}
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoIndex]->codecpar);
2、avcodec_encode_audio2:被聲明為已否決
舊版本:
if (avcodec_encode_audio2(tmpAvCodecCtx, &pkt_out, frame, &got_picture) < 0)
新版本:
if(avcodec_send_frame(tmpAvCodecCtx, frame)<0 || (got_picture=avcodec_receive_packet(tmpAvCodecCtx, &pkt_out))<0)
3、'avpicture_get_size': 被聲明為已否決
舊版本:
avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)
新版本:
#include "libavutil/imgutils.h"
av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1)
4、'avpicture_fill': 被聲明為已否決
舊版本:
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
新版本:
av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);
5、'avcodec_decode_video2': 被聲明為已否決
舊版本:
ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);
新版本:
if(avcodec_send_packet(pCodecCtx, packet)<0 || (got_picture =avcodec_receive_frame(pCodecCtx, pFrame))<0) {return -1}
6、' avcodec_alloc_frame': 被聲明為已否決
舊版本:
pFrame = avcodec_alloc_frame();
新版本:
pFrame = av_frame_alloc();
7、'av_free_packet': 被聲明為已否決
舊版本:
av_free_packet(packet);
新版本:
av_packet_unref(packet);
8、avcodec_decode_audio4:被聲明為已否決
舊版本:
int avcodec_decode_audio4(AVCodecContext *avctx, AVFrame *frame, int *got_frame, const AVPacket *avpkt);
新版本:
if(avcodec_send_packet(pCodecCtxOut_Video, &pkt)<0 || (got_frame=avcodec_receive_frame(pCodecCtxOut_Video,picture))<0) {return -1}
9、avcodec_encode_video2:被聲明為已否決
舊版本:
if(avcodec_encode_video2(tmpAvCodecCtx, &pkt, picture, &got_picture)<0)
新版本:
if(avcodec_send_frame(tmpAvCodecCtx, picture)<0 || (got_picture=avcodec_receive_packet(tmpAvCodecCtx, &pkt))<0)