1 Star 1 Fork 2

城主的项目/FFMPEG视音频解惑

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ChengzhuVideoPlay.cpp 4.79 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoyunchengzhu 提交于 2020-04-30 18:27 . d
//
// 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);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiaoyungongcheng/ffmpeg_demo.git
git@gitee.com:xiaoyungongcheng/ffmpeg_demo.git
xiaoyungongcheng
ffmpeg_demo
FFMPEG视音频解惑
master

搜索帮助