(轉載)QT+FFMPEG+SDL2.0實現視訊播放QT+FFMPEG+SDL2.0實現視訊播放(沒有聲音)
阿新 • • 發佈:2021-12-02
開發環境:MinGW+QT5.9+FFMPEG20190212+SDL2.0.9
一、開發環境搭建
(1)下載工具
在https://ffmpeg.zeranoe.com/builds/下載對應版本。連結方式有三種,
Static:這個版本只包含了ffmpeg.exe、ffplay.exe、ffprobe.exe三個可執行程式,沒有標頭檔案和庫檔案。
Shared:這個版本包含了ffmpeg.exe、ffplay.exe、ffprobe.exe三個可執行程式和相關動態庫檔案。
Dev:開發版,這個包含了標頭檔案和庫檔案。
我們需要下載Shared和Dev兩個版本,Dev有我們程式開發需要的標頭檔案和庫檔案,這裡麵包含的庫是動態呼叫的,所依賴的動態庫在Shared這個版本里面,所以兩個版本都要下載。
在http://www.libsdl.org/download-2.0.php下載SDL庫,選擇
(2)新增庫
將下載的檔案解壓縮,然後新建一個QT工程,在pro新增lib目錄和include目錄的路徑。
INCLUDEPATH +="E:\\Lib\\ffmpeg\\include" INCLUDEPATH +="E:\\Lib\\SDL2-2.0.9\\include" LIBS += -LE:\Lib\ffmpeg\lib -lavutil -lavformat -lavcodec -lavdevice -lavfilter -lpostproc -lswresample -lswscale LIBS += -LE:\Lib\SDL2-2.0.9\lib\x86 -lSDL2
然後將ffmpeg的dll和SDL2.dll放到exe目錄下。
二、程式碼實現:
在QT介面上放置一個widget和一個按鈕,點選按鈕時實現下面功能:
extern "C"{ #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #include <libavutil/imgutils.h> #include <SDL.h> } void MainWindow::on_btnPlay_clicked() { AVFormatContext *pFormatCtx; int i, videoindex; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame, *pFrameYUV; unsigned char *out_buffer; AVPacket *packet; int ret, got_picture; struct SwsContext *img_convert_ctx; char filepath[] = "E:\\media\\1.avi"; //初始化編解碼庫 av_register_all(); //已經無需使用的函式? //avformat_network_init(); //建立AVFormatContext物件,與碼流相關的結構。 pFormatCtx = avformat_alloc_context(); //初始化pFormatCtx結構 if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0){ printf("Couldn't open input stream.\n"); return ; } //獲取音視訊流資料資訊 if (avformat_find_stream_info(pFormatCtx, NULL) < 0){ printf("Couldn't find stream information.\n"); return ; } videoindex = -1; //nb_streams視音訊流的個數,這裡當查詢到視訊流時就中斷了。 for (i = 0; i < pFormatCtx->nb_streams; i++) if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){ videoindex = i; break; } if (videoindex == -1){ printf("Didn't find a video stream.\n"); return ; } //獲取視訊流編碼結構 pCodecCtx = pFormatCtx->streams[videoindex]->codec; //查詢解碼器 pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL){ printf("Codec not found.\n"); return ; } //用於初始化pCodecCtx結構 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){ printf("Could not open codec.\n"); return ; } //建立幀結構,此函式僅分配基本結構空間,影象資料空間需通過av_malloc分配 pFrame = av_frame_alloc(); pFrameYUV = av_frame_alloc(); //建立動態記憶體,建立儲存影象資料的空間 //av_image_get_buffer_size獲取一幀影象需要的大小 out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, ui->widget->width(), ui->widget->height(), 1)); av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, ui->widget->width(), ui->widget->height(), 1); packet = (AVPacket *)av_malloc(sizeof(AVPacket)); //Output Info----------------------------- printf("--------------- File Information ----------------\n"); //此函式列印輸入或輸出的詳細資訊 av_dump_format(pFormatCtx, 0, filepath, 0); printf("-------------------------------------------------\n"); //初始化img_convert_ctx結構 img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, ui->widget->width(), ui->widget->height(), AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); //SDL--------------------------- SDL_Window *screen; SDL_Renderer* sdlRenderer; SDL_Texture* sdlTexture; SDL_Rect sdlRect; if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) { printf("Could not initialize SDL - %s\n", SDL_GetError()); return; } screen = SDL_CreateWindowFrom((void *)ui->widget->winId()); if(screen==NULL) { printf("Could not create window - %s\n", SDL_GetError()); return; } sdlRenderer = SDL_CreateRenderer(screen, -1, 0); sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height); sdlRect.x = 0; sdlRect.y = 0; sdlRect.w = pCodecCtx->width; sdlRect.h = pCodecCtx->height; //end SDL----------------------- //av_read_frame讀取一幀未解碼的資料 while (av_read_frame(pFormatCtx, packet) >= 0){ //如果是視訊資料 if (packet->stream_index == videoindex){ //解碼一幀視訊資料 ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); if (ret < 0){ printf("Decode Error.\n"); return ; } if (got_picture){ sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); SDL_UpdateYUVTexture(sdlTexture, &sdlRect, pFrameYUV->data[0], pFrameYUV->linesize[0], pFrameYUV->data[1], pFrameYUV->linesize[1], pFrameYUV->data[2], pFrameYUV->linesize[2]); SDL_RenderClear(sdlRenderer); SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect); SDL_RenderPresent(sdlRenderer); SDL_Delay(40); } } av_free_packet(packet); } sws_freeContext(img_convert_ctx); av_frame_free(&pFrameYUV); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); }
extern "C" { #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libavutil/avutil.h> #include "libavutil/log.h" #include <libswscale/swscale.h> #include <libavutil/avutil.h> #include <libavdevice/avdevice.h> #include <libavutil/pixfmt.h> #include <libavutil/imgutils.h> #include <SDL.h> #include <SDL_audio.h> #include <SDL_types.h> #include <SDL_name.h> #include <SDL_main.h> #include <SDL_config.h> }; void FFmpegDemo::on_playerSdlBtn1_clicked() { AVFormatContext *pFormatCtx; int i, videoindex; AVCodecContext *pCodecCtx; AVCodec *pCodec; AVFrame *pFrame, *pFrameYUV; unsigned char *out_buffer; AVPacket *packet; int ret, got_picture; struct SwsContext *img_convert_ctx; //char filepath[] = "E:\\media\\1.avi"; char filepath[] = ".\\1.mp4"; //初始化編解碼庫 av_register_all(); //已經無需使用的函式? //avformat_network_init(); //建立AVFormatContext物件,與碼流相關的結構。 pFormatCtx = avformat_alloc_context(); //初始化pFormatCtx結構 if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0){ printf("Couldn't open input stream.\n"); return; } //獲取音視訊流資料資訊 if (avformat_find_stream_info(pFormatCtx, NULL) < 0){ printf("Couldn't find stream information.\n"); return; } videoindex = -1; //nb_streams視音訊流的個數,這裡當查詢到視訊流時就中斷了。 for (i = 0; i < pFormatCtx->nb_streams; i++) if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){ videoindex = i; break; } if (videoindex == -1){ printf("Didn't find a video stream.\n"); return; } //獲取視訊流編碼結構 pCodecCtx = pFormatCtx->streams[videoindex]->codec; //查詢解碼器 pCodec = avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL){ printf("Codec not found.\n"); return; } //用於初始化pCodecCtx結構 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0){ printf("Could not open codec.\n"); return; } //建立幀結構,此函式僅分配基本結構空間,影象資料空間需通過av_malloc分配 pFrame = av_frame_alloc(); pFrameYUV = av_frame_alloc(); //建立動態記憶體,建立儲存影象資料的空間 //av_image_get_buffer_size獲取一幀影象需要的大小 //==================================== 分配空間 ==================================// //一幀影象資料大小 //int numBytes = avpicture_get_size(AV_PIX_FMT_YUVJ420P, pCodecCtx->width, pCodecCtx->height); //out_buffer = (unsigned char *)av_malloc(numBytes * sizeof(unsigned char)); out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_YUV420P, ui.label->width(), ui.label->height(), 1)); av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer, AV_PIX_FMT_YUV420P, ui.label->width(), ui.label->height(), 1); packet = (AVPacket *)av_malloc(sizeof(AVPacket)); //Output Info----------------------------- printf("--------------- File Information ----------------\n"); //此函式列印輸入或輸出的詳細資訊 av_dump_format(pFormatCtx, 0, filepath, 0); printf("-------------------------------------------------\n"); //初始化img_convert_ctx結構 img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, ui.label->width(), ui.label->height(), AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL); //img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, //源地址長寬以及資料格式 // pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_YUVJ420P, //目的地址長寬以及資料格式 // SWS_BICUBIC, NULL, NULL, NULL);//演算法型別 AV_PIX_FMT_YUVJ420P AV_PIX_FMT_BGR24 //SDL--------------------------- SDL_Window *screen; SDL_Renderer* sdlRenderer; SDL_Texture* sdlTexture; SDL_Rect sdlRect; if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) { printf("Could not initialize SDL - %s\n", SDL_GetError()); return; } screen = SDL_CreateWindowFrom((void *)ui.label->winId()); if (screen == NULL) { printf("Could not create window - %s\n", SDL_GetError()); return; } sdlRenderer = SDL_CreateRenderer(screen, -1, 0); sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, pCodecCtx->width, pCodecCtx->height); sdlRect.x = 0; sdlRect.y = 0; sdlRect.w = pCodecCtx->width; sdlRect.h = pCodecCtx->height; //end SDL----------------------- //av_read_frame讀取一幀未解碼的資料 while (av_read_frame(pFormatCtx, packet) >= 0){ //如果是視訊資料 if (packet->stream_index == videoindex){ //解碼一幀視訊資料 ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); if (ret < 0){ printf("Decode Error.\n"); return; } if (got_picture){ sws_scale(img_convert_ctx, (const unsigned char* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameYUV->data, pFrameYUV->linesize); SDL_UpdateYUVTexture(sdlTexture, &sdlRect, pFrameYUV->data[0], pFrameYUV->linesize[0], pFrameYUV->data[1], pFrameYUV->linesize[1], pFrameYUV->data[2], pFrameYUV->linesize[2]); SDL_RenderClear(sdlRenderer); SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &sdlRect); SDL_RenderPresent(sdlRenderer); SDL_Delay(40); } } av_free_packet(packet); } sws_freeContext(img_convert_ctx); av_frame_free(&pFrameYUV); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); }View Code