環(huán)境說(shuō)明
我這里使用的是Qt5環(huán)境秃励,當(dāng)然你可以使用其他環(huán)境,原理應(yīng)該都差不多的吉捶。
導(dǎo)入ffmpeg庫(kù)
首先在Qt的工程文件莺治,就是.pro格式的那個(gè),打開(kāi)帚稠,然后加入下面代碼谣旁,我這里對(duì)windows和mac系統(tǒng)做了一下兼容,你選擇其中一個(gè)即可滋早,如果是windows系統(tǒng)榄审,那你得改一下INCLUDEPATH 路徑為你存放ffmpeg的路徑。
win32 {
INCLUDEPATH += "F:\\cpppath\\ffmpeg-win64-dev\\include"
LIBS += -LF:\cpppath\ffmpeg-win64-dev\lib -lavutil -lavformat -lavcodec -lavfilter -lswscale -lswresample -lopengl32 -luser32
}
mac {
LIBS += -L/usr/local/lib -lavutil -lavformat -lavcodec -lavfilter -lswscale -lswresample -framework CoreFoundation
INCLUDEPATH = /usr/local/include
}
流程分析
大概流程是這樣的:
- 使用ffmpeg打開(kāi)視頻文件
- 讀取視頻第一幀數(shù)據(jù)
- 對(duì)視頻幀進(jìn)行解碼杆麸,得到圖片原始數(shù)據(jù)
- 對(duì)原始數(shù)據(jù)進(jìn)行加工處理搁进,轉(zhuǎn)換成我們需要的圖片對(duì)象
關(guān)鍵代碼
void MainWindow::createPreviewWidthFile(const char *file){
AVFormatContext* fmt_ctx_ = nullptr;
//打開(kāi)視頻文件
int errCode = avformat_open_input(&fmt_ctx_, file, nullptr, nullptr);
if(errCode != 0){
qDebug() << "avformat_open_input fail" << errCode;
return;
}
//讀取音視頻流信息
errCode = avformat_find_stream_info(fmt_ctx_, nullptr);
if(errCode != 0){
qDebug() << "avformat_find_stream_info fail" << errCode;
avformat_close_input(&fmt_ctx_);
return;
}
//打印輸出視頻相關(guān)信息
av_dump_format(fmt_ctx_, 0, file, 0);
AVPacket* pkt = av_packet_alloc();
AVFrame* temp_frame = av_frame_alloc();
SwsContext* sws_ctx = nullptr;
int ret = 0;
QImage preview;
bool preview_done = false;
for (int i=0; i<int(fmt_ctx_->nb_streams) && !preview_done; i++){
//只處理視頻信息
if (fmt_ctx_->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
//查找視頻解碼器
AVCodec* codec = avcodec_find_decoder(fmt_ctx_->streams[i]->codecpar->codec_id);
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
//根據(jù)提供的編解碼器參數(shù)的值填充編解碼器上下文
avcodec_parameters_to_context(codec_ctx, fmt_ctx_->streams[i]->codecpar);
//打開(kāi)解碼器
avcodec_open2(codec_ctx, codec, nullptr);
//讀取幀數(shù)據(jù)
while (av_read_frame(fmt_ctx_, pkt) >= 0){
av_frame_unref(temp_frame);
//對(duì)視頻幀數(shù)據(jù)進(jìn)行解碼
while ((ret = avcodec_receive_frame(codec_ctx, temp_frame)) == AVERROR(EAGAIN)){
ret = avcodec_send_packet(codec_ctx, pkt);
if (ret < 0) {
qCritical() << "Failed to send packet to decoder." << ret;
break;
}
}
if(ret < 0 && ret != AVERROR_EOF){
qDebug() << "Failed to receive packet from decoder." << ret;
continue;
}
//等比例縮放
int dstH = 240;
int dstW = qRound(dstH * (float(temp_frame->width)/float(temp_frame->height)));
//消除可能的告警
dstH = (dstH >> 4) << 4;
dstW = (dstW >> 4) << 4;
sws_ctx = sws_getContext(
temp_frame->width,
temp_frame->height,
static_cast<AVPixelFormat>(temp_frame->format),
dstW,
dstH,
static_cast<AVPixelFormat>(AV_PIX_FMT_RGBA),
SWS_FAST_BILINEAR,
nullptr,
nullptr,
nullptr
);
int linesize[AV_NUM_DATA_POINTERS];
linesize[0] = dstW*4;
//生成圖片
preview = QImage(dstW, dstH, QImage::Format_RGBA8888);
uint8_t* data = preview.bits();
sws_scale(sws_ctx,
temp_frame->data,
temp_frame->linesize,
0,
temp_frame->height,
&data,
linesize);
sws_freeContext(sws_ctx);
avcodec_close(codec_ctx);
avcodec_free_context(&codec_ctx);
preview_done = true;
break;
}
}
}
//釋放資源
av_frame_free(&temp_frame);
av_packet_free(&pkt);
avformat_close_input(&fmt_ctx_);
//使用該圖片浪感,貼圖到textlabel
if(preview_done){
ui->label->setPixmap(QPixmap::fromImage(preview));
}
}
該代碼只是完成了一個(gè)基本的功能,而且可能并不完善饼问,具體使用需要自己完善了影兽。
效果:
在這里插入圖片描述