1. 程式人生 > >FFmpeg解碼MP4檔案為h264和YUV檔案

FFmpeg解碼MP4檔案為h264和YUV檔案

#include <iostream>
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
};
#endif

using namespace std;

int main(int argc, char* argv[])
{
    char filepath[] = "test.mp4"
; FILE *fp_yuv = fopen("test_out.yuv", "wb+"); FILE *fp_h264 = fopen("test_out.h264", "wb+"); //註冊所有元件 av_register_all(); //初始化一個AVFormatContext AVFormatContext *pFormatCtx = avformat_alloc_context(); //開啟輸入的視訊檔案 if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0
) { cout << "Couldn't open input stream." << endl; return -1; } //獲取視訊檔案資訊 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { cout << "Couldn't find stream information." << endl; return -1; } int videoindex = -1; for
(int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { videoindex = i; break; } } if (videoindex == -1) { cout << "Didn't find a video stream." << endl; return -1; } AVCodecContext *pCodecCtx = pFormatCtx->streams[videoindex]->codec; //查詢解碼器 AVCodec *pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL) { cout << "Codec not found." << endl; return -1; } //開啟解碼器 if (avcodec_open2(pCodecCtx, pCodec, NULL)<0) { cout << "Could not open codec." << endl; return -1; } AVFrame *pFrame = av_frame_alloc(); AVFrame *pFrameYUV = av_frame_alloc(); uint8_t *out_buffer = (uint8_t *)av_malloc(avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)); avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height); AVPacket *packet = (AVPacket *)av_malloc(sizeof(AVPacket)); //除錯輸出檔案資訊 cout << "--------------- File Information ----------------" << endl;; av_dump_format(pFormatCtx, 0, filepath, 0); cout << "-------------------------------------------------" << endl; //對圖形進行寬度上方的裁剪,以便於顯示得更好 struct SwsContext *img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); int got_picture; while (av_read_frame(pFormatCtx, packet) >= 0) { if (packet->stream_index == videoindex)//讀取一幀壓縮資料 { fwrite(packet->data, 1, packet->size, fp_h264); //把H264資料寫入fp_h264檔案 int ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet);//解碼一幀壓縮資料 if (ret < 0) { cout << "Decode Error." << endl; return -1; } if (got_picture) { sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); int y_size = pCodecCtx->width*pCodecCtx->height; fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Y fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //U fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //V cout << "Succeed to decode frame " << pCodecCtx->frame_number << endl; } } av_free_packet(packet); } //flush decoder while (1) { int ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); if (ret < 0) break; if (!got_picture) break; sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); int y_size = pCodecCtx->width*pCodecCtx->height; fwrite(pFrameYUV->data[0], 1, y_size, fp_yuv); //Y fwrite(pFrameYUV->data[1], 1, y_size / 4, fp_yuv); //U fwrite(pFrameYUV->data[2], 1, y_size / 4, fp_yuv); //V cout << "Succeed to decode frame " << pCodecCtx->frame_number << endl; } sws_freeContext(img_convert_ctx); //關閉檔案以及釋放記憶體 fclose(fp_yuv); fclose(fp_h264); av_frame_free(&pFrameYUV); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); return getchar(); }