ffmpeg学习2 最简单的播放器

在Mac OS 10.15环境下,基于FFmpeg 4.2.2版本和SDL 2.0,本文介绍了如何构建最简单的视频播放器。教程参照了CSND博主leixiaohua1020的详细步骤。
摘要由CSDN通过智能技术生成

运行环境: Mac OS 10.15

FFMPEG版本: 4.2.2

SDL: 2.0

参考 https://blog.csdn.net/leixiaohua1020/article/details/38868499

//
//  main.cpp
//  ffmpeg_sample2
//
//  Created by gaoguanglei on 2020/1/24.
//  Copyright © 2020 gaoguanglei. All rights reserved.
//

#include <iostream>

extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
    #include <SDL2/SDL.h>
    #include <libavutil/imgutils.h>
}


#define REFRESH_EVENT (SDL_USEREVENT+1)
#define BREAK_EVENT (SDL_USEREVENT+2)

int g_thread_exit = 0;
int g_thread_pause = 0;

int  refresh_thread(void*)
{
    while(!g_thread_exit)
    {
        if(!g_thread_pause)
        {
            SDL_Event event;
            event.type = REFRESH_EVENT;
            SDL_PushEvent(&event);
            SDL_Delay(40);
        }
        else
        {
            continue;
        }
    }
    SDL_Event event;
    event.type = BREAK_EVENT;
    SDL_PushEvent(&event);

    return 0;

}

int main(int argc, const char * argv[]) {
    // insert code here...
    
  //  avformat_network_init();

    AVFormatContext* pAVFormatContext = avformat_alloc_context();
    /** Open an input stream and read the header. The codecs are not opened.
     * The stream must be closed with avformat_close_input().
     */
    if(avformat_open_input(&pAVFormatContext, "/Users/gaoguanglei/work/test.mov", nullptr, nullptr)!=0)
    {
        printf("could not open input stream\n");
    }

    /**
     * Read packets of a media file to get stream information. This
     * is useful for file formats with no headers such as MPEG. This
     * function also computes the real framerate in case of MPEG-2 repeat
     * frame mode.
     * The logical file position is not changed by this function;
     * examined packets may be buffered for later processing.
     */
    if(avformat_find_stream_info(pAVFormatContext, nullptr) != 0)
    {
        printf("count not find stream info\n");
    }

    int videIndex = -1;
    for(int i=0; i<pAVFormatContext->nb_streams; i++)
    {
        if(pAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videIndex = i;
            break;
        }
    }

    if(videIndex == -1)
    {
        printf("video index == -1, return\n");
        return 0;
    }
    /**
    * Find a registered decoder with a matching codec ID.
    *
    * @param id AVCodecID of the requested decoder
    * @return A decoder if one was found, NULL otherwise.
    */
    
    AVCodec* pAVCodec = avcodec_find_decoder(pAVFormatContext->streams[videIndex]->codecpar->codec_id);

    if(pAVCodec == nullptr)
    {
        printf("could not find codec\n");
    }

    /**
    * Allocate an AVCodecContext and set its fields to default values. The
    * resulting struct should be freed with avcodec_free_context().
    *
    * @param codec if non-NULL, allocate private data and initialize defaults
    *              for the given codec. It is illegal to then call avcodec_open2()
    *              with a different codec.
    *              If NULL, then the codec-specific defaults won't be initialized,
    *              which may result in suboptimal default settings (this is
    *              important mainly for encoders, e.g. libx264).
    *
    * @return An AVCodecContext filled with default values or NULL on failure.
    */
    AVCodecContext* pAVCodecContext = avcodec_alloc_context3(pAVCodec);

    /**
    * Fill the codec context based on the values from the supplied codec
    * parameters. Any allocated fields in codec that have a corresponding field in
    * par are freed and replaced with duplicates of the corresponding field in par.
    * Fields in codec that do not have a counterpart in par are not touched.
    *
    * @return >= 0 o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值