本文目标:输入一个视频文件,然后播放视频画面(暂时不播放声音)。
主要结合前面写的几篇文章,进行播放:
15.FFmpeg学习笔记 - 解复用和解码(新的解码API)
27.FFmpeg学习笔记 - 用libswscale转换原始视频格式
程序流程是:先解码,然后将视频格式转换为YUV420P格式,最后用OpenGL在iOS上播放出来。
程序解码后播放时,只是简单的延时40ms(一般帧率是25),然后播放下一帧。后续有待优化。
流程代码如下:
开启子线程进行解码,得到数据后在主线程进行播放。MyView封装了前面文章写的OpenGL播放YUV的代码,并且只能播放YUV420P格式的数据。所以解码后得到的数据格式不是YUV420P,则需要进行转换。
下面是源码:
static MyView *myView;
static const char *src_filename = "/Users/zhw/Desktop/resource/out.mp4";
static AVFormatContext *fmt_ctx = NULL;
static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx = NULL;
static AVStream *video_stream = NULL;
static int video_stream_idx = -1;
static int width, height;
static enum AVPixelFormat src_pix_fmt;
static AVFrame *frame = NULL;
static AVPacket pkt;
static uint8_t *video_dst_data[4] = {NULL};
static int video_dst_linesize[4];
static int video_dst_bufsize;
struct SwsContext *sws_ctx;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
myView = _myView;
[NSThread detachNewThreadWithBlock:^{
demux_decode();
}];
}
static void demux_decode(void)
{
int ret = 0;
/* 打开文件,创建AVFormatContext */
if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
printf("Could not open source fi