QT+FFMPEG实现视频播放
開發環境:MinGW+QT5.9+FFMPEG20190212
一、開發環境搭建
FFMPEG的開發環境部署比如容易,在官網下載庫文件,然后在QT里面指定路徑,把相關dll文件放到exe目錄下就可以了,不需要根據開發工具重新編譯。
(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這個版本里面,所以兩個版本都要下載。
(2)添加庫
將下載的文件解壓縮,然后新建一個QT工程,在pro添加lib目錄和include目錄的路徑。
?
INCLUDEPATH +="E:\\Lib\\ffmpeg\\include"LIBS += -LE:\Lib\ffmpeg\lib -lavutil -lavformat -lavcodec -lavdevice -lavfilter -lpostproc -lswresample -lswscale?
然后將shared下的動態庫添加到exe目錄下。
?
二、代碼實現播放功能
在界面上放置一個QLabel和QPushButton控件,當點擊按鈕時實現以下功能:
#include <QTime> extern "C"{ #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #include <libavutil/imgutils.h> } void Delay(int msec) {QTime dieTime = QTime::currentTime().addMSecs(msec);while( QTime::currentTime() < dieTime )QCoreApplication::processEvents(QEventLoop::AllEvents, 100); }void MainWindow::on_btnPlay_clicked() {AVFormatContext *pFormatCtx;int i, videoindex;AVCodecContext *pCodecCtx;AVCodec *pCodec;AVFrame *pFrame, *pFrameRGB;unsigned char *out_buffer;AVPacket *packet;int ret, got_picture;struct SwsContext *img_convert_ctx;char filepath[] = "E:\\media\\1.avi";//初始化編解碼庫 av_register_all();//創建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();pFrameRGB = av_frame_alloc();//創建動態內存,創建存儲圖像數據的空間//av_image_get_buffer_size獲取一幀圖像需要的大小out_buffer = (unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_RGB32, pCodecCtx->width, pCodecCtx->height, 1));av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer,AV_PIX_FMT_RGB32, pCodecCtx->width, pCodecCtx->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,pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB32, SWS_BICUBIC, NULL, NULL, NULL);//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,pFrameRGB->data, pFrameRGB->linesize);QImage img((uchar*)pFrameRGB->data[0],pCodecCtx->width,pCodecCtx->height,QImage::Format_RGB32);ui->label->setPixmap(QPixmap::fromImage(img));Delay(40);}}av_free_packet(packet);}sws_freeContext(img_convert_ctx);av_frame_free(&pFrameRGB);av_frame_free(&pFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx); }?
以上就是QT顯示視頻圖像的簡單例子,對于對FFMPEG感興趣想學習更多內容的,推薦看看下面博主的博文,對FFMPEG的介紹非常詳細:
https://blog.csdn.net/leixiaohua1020/article/details/15811977
?
轉載于:https://www.cnblogs.com/WushiShengFei/p/10837264.html
總結
以上是生活随笔為你收集整理的QT+FFMPEG实现视频播放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 纯前端表格控件SpreadJS V12.
- 下一篇: BCI2000对win10的支持