31.FFmpeg学习笔记 - iOS上解码视频并播放(暂无声音)

本文目标:输入一个视频文件,然后播放视频画面(暂时不播放声音)。

主要结合前面写的几篇文章,进行播放:

15.FFmpeg学习笔记 - 解复用和解码(新的解码API)

27.FFmpeg学习笔记 - 用libswscale转换原始视频格式

10.OpenGL学习笔记 - 在iOS上播放YUV

程序流程是:先解码,然后将视频格式转换为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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值