ffmpeg解碼後輸出yuv視頻文件

使用新版的ffmpeg:關鍵部分代碼如下

ret = avcodec_send_packet(st->ctx, &avpkt);
.....
ret = avcodec_receive_frame(st->ctx, st->pict);
.....

for (i=0; i<4; i++) {
	frame->data[i]     = st->pict->data[i];
	frame->linesize[i] = st->pict->linesize[i];
}
//把解碼後的yuv數據寫入文件
char* buf = (char *)malloc(st->ctx->height * st->ctx->width * 3 / 2);
memset(buf, 0, st->ctx->height * st->ctx->width * 3 / 2);
int height = st->ctx->height;
int width = st->ctx->width;
printf("decode video ok\n");
int a = 0, i;
for (i = 0; i<height; i++)
{
	memcpy(buf + a, st->pict->data[0] + i * st->pict->linesize[0], width);
	a += width;
}
for (i = 0; i<height / 2; i++)
{
	memcpy(buf + a, st->pict->data[1] + i * st->pict->linesize[1], width / 2);
	a += width / 2;
}
for (i = 0; i<height / 2; i++)
{
	memcpy(buf + a, st->pict->data[2] + i * st->pict->linesize[2], width / 2);
	a += width / 2;
}
fwrite(buf, 1, st->ctx->height * st->ctx->width * 3 / 2, fp);//解碼後的yuv數據,二進制寫
free(buf);
buf = NULL;


使用vlc播放yuv視頻
vlc --demux rawvideo --rawvid-fps 30 --rawvid-width 1280 --rawvid-height 720 --rawvid-chroma I420 test.yuv

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章