ffmpeg如何将任意一种格式数据都转化成 vp9格式啊?
下面是我根据网上的例子,改的程序,总报错,请高手给看看,这样写对不对?
void video_encode_example(const char *filename, AVFrame *picture, int width, int height)
{
AVCodec *codec;
AVCodecContext *c= NULL;
int i, out_size, size, outbuf_size;
FILE *f;
uint8_t *outbuf, *picture_buf;
printf("Video encoding\n");
av_register_all();
codec = avcodec_find_encoder(AVCodecID::AV_CODEC_ID_VP9);
avcodec_register(codec);//这个不知道该放在哪儿?
if (!codec) {
fprintf(stderr, "codec not found\n");
exit(1);
}
c= avcodec_alloc_context3(codec);
//picture= avcodec_alloc_frame();
/* put sample parameters */
c->bit_rate = 400000;//设置采样参数,即比特率。
/* resolution must be a multiple of two */
c->width = width;//设置分辨率,必须是2的倍数。
c->height = height;//设置分辨率,必须是2的倍数。
/* frames per second */
c->time_base.num = 1;
c->time_base.den = 25;
c->gop_size = 10; /* emit one intra frame every ten frames *///该值表示每10帧会插入一个I帧(intra frame)。
c->max_b_frames=1;
c->pix_fmt = PIX_FMT_YUV420P;
/* open it */
AVDictionary *pAVDictionary=NULL;//
if (avcodec_open2(c, codec,&pAVDictionary) < 0) {
fprintf(stderr, "could not open codec\n");
exit(1);
}
/* the codec gives us the frame size, in samples */
fopen_s(&f,filename, "ab");//接着是打开文件,f = fopen( filename, “wb” );
if (!f) {
fprintf(stderr, "could not open %s\n", filename);
exit(1);
}
/* alloc image and output buffer */
outbuf_size = 1000000;//
outbuf = (uint8_t *)malloc(outbuf_size);
size = c->width * c->height;
picture_buf = (uint8_t *)malloc((size * 3) / 2); /* size for YUV 420 */
/* encode the image */
out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture); // 这儿不对
fwrite(outbuf, 1, out_size, f);
/* get the delayed frames */
i=0;
for(; out_size; i++) {
fflush(stdout);
printf("Ln:%d ;;out_size = %d \n",__LINE__,out_size);
out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
printf("write frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}
printf("Ln:%d \n",__LINE__);
/* add sequence end code to have a real mpeg file */
fclose(f);
free(picture_buf);
free(outbuf);
avcodec_close(c);
av_free(c);
av_free(picture);
printf("\n");
}