#include "videoplayer.h"
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/pixfmt.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
}
#include <stdio.h>
VideoPlayer::VideoPlayer()
{
}
VideoPlayer::~VideoPlayer()
{
}
void VideoPlayer::startPlay()
{
///调用 QThread 的start函数 将会自动执行下面的run函数 run函数是一个新的线程
this->start();
}
void VideoPlayer::run()
{
char *file_path = mFileName.toUtf8().data();
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame, *pFrameRGB;
AVPacket *packet;
uint8_t *out_buffer;
static struct SwsContext *img_convert_ctx;
int videoStream, numBytes;
int ret;
av_register_all(); //初始化FFMPEG 调用了这个才能正常适用编码器和解码器
pFormatCtx = avformat_alloc_context();
if (avformat_open_input(&pFormatCtx, file_path, NULL, NULL) != 0) {
printf("can't open the file. \n");
return;
}
if (avformat_find_stream_info(pFormatCtx, NULL) < 0) {
printf("Could't find stream infomation.\n");
return;
}
videoStream = -1;
for (unsigned int i = 0; i < pFormatCtx->nb_streams; i++) {
if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
videoStream = i;
}
}
///如果videoStream为-1 说明没有找到视频流
if (videoStream == -1) {
printf("Didn't find a video stream.\n");
return;
}
///查找解码器
AVCodecParameters *codec_para = pFormatCtx->streams[videoStream]->codecpar;
pCodec = avcodec_find_decoder(codec_para->codec_id);
if (pCodec == 0) {
printf("Codec not found.\n");
return;
}
pCodecCtx = avcodec_alloc_context3(pCodec);
avcodec_parameters_to_context(pCodecCtx, codec_para);
///打开解码器
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
printf("Could not open codec.\n");
return;
}
pFrame = av_frame_alloc();
pFrameRGB = av_frame_alloc();
///这里我们改成了 将解码后的YUV数据转换成RGB32
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);
numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB32, pCodecCtx->width,pCodecCtx->height, 1);
out_buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
av_image_fill_arrays(pFrameRGB->data, pFrameRGB->linesize, out_buffer, AV_PIX_FMT_RGB32, pCodecCtx->width, pCodecCtx->height, 1);
packet = av_packet_alloc();
while (1)
{
if (av_read_frame(pFormatCtx, packet) < 0)
{
break; //这里认为视频读取完了
}
if (packet->stream_index == videoStream)
{
ret = avcodec_send_packet(pCodecCtx, packet);
if (ret < 0) {
continue;
}
while (avcodec_receive_frame(pCodecCtx, pFrame)>=0)
{
// printf("pkt pts = %lld\n", packet->pts);
// printf("frame pts = %lld\n", pFrame->pts);
// uint64_t pts = av_frame_get_best_effort_timestamp(pFrame);
sws_scale(img_convert_ctx,
(uint8_t const * const *) pFrame->data,
pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data,
pFrameRGB->linesize);
//把这个RGB数据 用QImage加载
QImage tmpImg((uchar *)out_buffer, pCodecCtx->width, pCodecCtx->height, QImage::Format_RGB32);
QImage image = tmpImg.copy(); //把图像复制一份 传递给界面显示
emit sig_GetOneFrame(image); //发送信号
}
}
av_packet_unref(packet);
msleep(5); //停一停 不然放的太快了
}
av_packet_free(&packet);
av_free(out_buffer);
av_free(pFrameRGB);
avcodec_close(pCodecCtx);
avformat_close_input(&pFormatCtx);
}
没有合适的资源?快使用搜索试试~ 我知道了~
QT+ffmpeg播放视频
共53个文件
d:6个
o:6个
hmap:6个
1星 需积分: 19 59 下载量 25 浏览量
2017-08-29
21:51:07
上传
评论 2
收藏 879KB ZIP 举报
温馨提示
使用qt和ffmpeg播放视频。
资源推荐
资源详情
资源评论
收起资源包目录
VideoPlayer.zip (53个子文件)
VideoPlayer
VideoPlayer.xcodeproj
xcshareddata
xcschemes
VideoPlayer.xcscheme 4KB
project.xcworkspace
xcshareddata
WorkspaceSettings.xcsettings 264B
xcuserdata
stellazhou.xcuserdatad
WorkspaceSettings.xcsettings 255B
UserInterfaceState.xcuserstate 16KB
xcuserdata
stellazhou.xcuserdatad
xcschemes
xcschememanagement.plist 427B
qt_preprocess.mak 5KB
qt_makeqmake.mak 26KB
project.pbxproj 29KB
VideoPlayer_2.pro 1KB
.DS_Store 6KB
src
mainwindow.ui 791B
.DS_Store 6KB
mainwindow.cpp 1KB
main.cpp 347B
videoplayer.h 479B
mainwindow.h 590B
videoplayer.cpp 4KB
.qmake.stash 3KB
ui_mainwindow.h 2KB
VideoPlayer.build
Debug
Qt Preprocess.build
dgph 38KB
dgph~ 38KB
Script-7EF0942E79C014DCEC8976BC.sh 104B
Script-5E618435888B9D49F8540165.sh 103B
VideoPlayer.build
dgph 57KB
dgph~ 57KB
VideoPlayer-project-headers.hmap 2KB
VideoPlayer-all-target-headers.hmap 2KB
VideoPlayer-own-target-headers.hmap 2KB
VideoPlayer.hmap 2KB
VideoPlayer-all-non-framework-target-headers.hmap 2KB
VideoPlayer-generated-files.hmap 121B
Objects-normal
x86_64
main.d 8KB
main.dia 220B
dummy.o 1KB
dummy.d 73B
moc_mainwindow.d 7KB
main.o 646KB
mainwindow.d 10KB
VideoPlayer.LinkFileList 713B
mainwindow.o 866KB
videoplayer.dia 220B
dummy.dia 220B
VideoPlayer_dependency_info.dat 60KB
videoplayer.o 420KB
moc_videoplayer.o 365KB
moc_mainwindow.o 592KB
mainwindow.dia 220B
moc_mainwindow.dia 220B
moc_videoplayer.dia 220B
videoplayer.d 7KB
moc_videoplayer.d 5KB
Release
Qt Preprocess.build
dgph 802B
dgph~ 88B
共 53 条
- 1
资源评论
- qq_375445992017-09-04extern "C"包含的库文件去哪里了?
摩西2016
- 粉丝: 16
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功