代码拉取完成,页面将自动刷新
//
// Created by zhangshiyu on 18-9-20.
//
extern "C" {
#include "FormatUtil.h"
#include "SDLUtil.h"
//主函数
int main(int argc, char **argv) {
char *path = "/home/zhangshiyu/Videos/4.27-1.mp4";// char *path="/home/zhangshiyu/Videos/xiaoliange.mp4";
// char *path="rtsp://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";
av_register_all();
AVFormatContext *pFormatContext;
pFormatContext = avformat_alloc_context();
//打开
if (avformat_open_input(&pFormatContext, path, NULL, NULL) != 0) {
printf("cannot open ");
return -1;
}
//找流
if (avformat_find_stream_info(pFormatContext, NULL) < 0) {
printf("cannot find stream");
}
// 在控制台输出文件信息
av_dump_format(pFormatContext, 0, path, 0);
int videoStream = -1;
int audioStream = -1;
//视音频流个数 遍历
for (int i = 0; i < pFormatContext->nb_streams; i++) {
if (pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO&&videoStream<0) {
videoStream = i;
}
if (pFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO&&audioStream<0) {
audioStream = i;
}
}
if (videoStream==-1){
printf("cannot videostream index");
}
if (audioStream==-1){
printf("cannot audiostream index");
}
//音视频码上下文
AVCodecContext *videoCodecContext = pFormatContext->streams[videoStream]->codec;
AVCodecContext *audioCodecContext = pFormatContext->streams[audioStream]->codec;
AVCodecContext *audioCodecContextOrg = pFormatContext->streams[audioStream]->codec;
//音视频解码器
AVCodec *videoCodec = avcodec_find_decoder(videoCodecContext->codec_id);
AVCodec *audioCodec = avcodec_find_decoder(audioCodecContext->codec_id);
audioCodecContext=avcodec_alloc_context3(audioCodec);
if (avcodec_copy_context(audioCodecContext,audioCodecContextOrg)!=0){
printf("audio cp failure");
}
//打开视频
if (avcodec_open2(videoCodecContext, videoCodec, NULL) < 0) {
printf("open video failure");
return -1;
}
//打开音频
if (avcodec_open2(audioCodecContext,audioCodec,NULL)<0){
printf("open audio failure ");
}
SDLManager sdlManager(1080,1080*videoCodecContext->height/videoCodecContext->width);
sdlManager.initSDLVideo();
AVFrame *pFrame = NULL;
AVFrame *yuvFrame = NULL;
pFrame = av_frame_alloc();
yuvFrame = av_frame_alloc();
if (yuvFrame == NULL) {
printf("video frame not");
return -1;
}
//资源数据内存大小
int numBytes = 0;
uint8_t *buffer = NULL;
AVPixelFormat pixelFormat=AV_PIX_FMT_YUV420P;
// AVPixelFormat pixelFormat=AV_PIX_FMT_RGBA;
numBytes = av_image_get_buffer_size(pixelFormat, videoCodecContext->width, videoCodecContext->height,1);
buffer = (uint8_t *) av_malloc(numBytes * sizeof(uint8_t));
av_image_fill_arrays(yuvFrame->data, yuvFrame->linesize,buffer,
pixelFormat,videoCodecContext->width, videoCodecContext->height,1);
SwsContext *sws_ctx;
sws_ctx = sws_getContext(videoCodecContext->width,
videoCodecContext->height,
videoCodecContext->pix_fmt,
videoCodecContext->width,
videoCodecContext->height,
pixelFormat,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
//读取包
int frameFinished;
AVPacket videoPacket;
while (av_read_frame(pFormatContext, &videoPacket)>=0) {
//video解析
if (videoPacket.stream_index == videoStream) {
if (avcodec_decode_video2(videoCodecContext, pFrame, &frameFinished, &videoPacket)<0){
printf("error decode video");
}
if (frameFinished) {
sws_scale(sws_ctx, (uint8_t const *const *) pFrame->data,
pFrame->linesize, 0, videoCodecContext->height,
yuvFrame->data, yuvFrame->linesize);
// printf("size:%d",yuvFrame->linesize[0]);
sdlManager.updateVideo( yuvFrame->data[0], yuvFrame->linesize[0],
yuvFrame->data[1], yuvFrame->linesize[1],
yuvFrame->data[2], yuvFrame->linesize[2]);
sdlManager.updateEvent();
}
}
av_free_packet(&videoPacket);
}
av_free(&buffer);
av_free(yuvFrame);
// Free the YUV frame
av_free(pFrame);
// Close the codecs
avcodec_close(videoCodecContext);
avcodec_close(audioCodecContext);
// Close the video
avformat_close_input(&pFormatContext);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。