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
使用R19CNDK编译完整的FFMPE4.2.2库,用于集成到Android平台使用,完成音视频开发。 压缩包里包含了完整源码,配置文件、编译成功的动态库。如何需要重新编译,可以自己看配置文件。 配置文件包含了arm-v7、arm64-v8两种,根据自己平台可以选择编译。 ffmpeg里编译时包含了X264解码库。 支持的解码库如下; "当前APP的工作路径:/home/wbyq/qt_code/build-ffmpeg_code-Desktop_Qt_5_12_6_GCC_64bit-Release\n" "FFMPEG的版本号:4.2.2\n" "[Encode][Video]a64multi ID=142" "[Encode][Video]a64multi5 ID=143" "[Encode][Video]alias_pix ID=175" "[Encode][Video]amv ID=107" "[Encode][Video]apng ID=32782" "[Encode][Video]asv1 ID=31" "[Encode][Video]asv2 ID=32" "[Encode][Video]avrp ID=32769" "[Encode][Video]avui ID=32771" "[Encode][Video]ayuv ID=32772" "[Encode][Video]bmp ID=78" "[Encode][Video]cinepak ID=43" "[Encode][Video]cljr ID=36" "[Encode][Audeo]comfortnoise ID=86077" "[Encode][Video]dnxhd ID=99" "[Encode][Video]dpx ID=128" "[Encode][Video]dvvideo ID=24" "[Encode][Video]ffv1 ID=33" "[Encode][Video]ffvhuff ID=67" "[Encode][Video]fits ID=32803" "[Encode][Video]flashsv ID=86" "[Encode][Video]flashsv2 ID=131" "[Encode][Video]flv ID=21" "[Encode][Video]gif ID=97" "[Encode][Video]h261 ID=3" "[Encode][Video]h263 ID=4" "[Encode][Video]h263p ID=19" "[Encode][Video]huffyuv ID=25" "[Encode][Video]jpeg2000 ID=88" "[Encode][Video]jpegls ID=11" "[Encode][Video]ljpeg ID=9" "[Encode][Video]magicyuv ID=32787" "[Encode][Video]mjpeg ID=7" "[Encode][Video]mpeg1video ID=1" "[Encode][Video]mpeg2video ID=2" "[Encode][Video]mpeg4 ID=12" "[Encode][Video]msmpeg4v2 ID=15" "[Encode][Video]msmpeg4 ID=16" "[Encode][Video]msvideo1 ID=46" "[Encode][Video]pam ID=66" "[Encode][Video]pbm ID=63" "[Encode][Video]pcx ID=109" "[Encode][Video]pgm ID=64" "[Encode][Video]pgmyuv ID=65" "[Encode][Video]png ID=61" "[Encode][Video]ppm ID=62" "[Encode][Video]prores ID=147" "[Encode][Video]prores_aw ID=147" "[Encode][Video]prores_ks ID=147" "[Encode][Video]qtrle ID=55" "[Encode][Video]r10k ID=144" "[Encode][Video]r210 ID=133" "[Encode][Video]rawvideo ID=13" "[Encode][Video]roqvideo ID=38" "[Encode][Video]rv10 ID=5" "[Encode][Video]rv20 ID=6" "[Encode][Audeo]s302m ID=65562" "[Encode][Video]sgi ID=101" "[Encode][Video]snow ID=32780" "[Encode][Video]sunrast ID=110" "[Encode][Video]svq1 ID=22" "[Encode][Video]targa ID=93" "[Encode][Video]tiff ID=96" "[Encode][Video]utvideo ID=152" "[Encode][Video]v210 ID=127" "[Encode][Video]v308 ID=32774" "[Encode][Video]v408 ID=32775" "[Encode][Video]v410 ID=156" "[Encode][Video]vc2 ID=116" "[Encode][Video]wrapped_avframe ID=135169" "[Encode][Video]wmv1 ID=17" "[Encode][Video]wmv2 ID=18" "[Encode][Video]xbm ID=159" "[Encode][Video]xface ID=32779" "[Encode][Video]xwd ID=157" "[Encode][Video]y41p ID=32768" "[Encode][Video]yuv4 ID=32776" "[Encode][Video]zlib ID=54" "[Encode][Video]zmbv ID=81" "[Encode][Audeo]aac ID=86018" "[Encode][Audeo]ac3 ID=86019" "[Encode][Audeo]ac3_fixed ID=86019" "[Encode][Audeo]alac ID=86032" "[Encode][Audeo]aptx ID=88081" "[Encode][Audeo]aptx_hd ID=88082" "[Encode][Audeo]dca ID=86020" "[Encode][Audeo]eac3 ID=86056" "[Encode][Audeo]flac ID=86028" "[Encode][Audeo]g723_1 ID=86068" "[Encode][Audeo]mlp ID=86045" "[Encode][Audeo]mp2 ID=86016" "[Encode][Audeo]mp2fixed ID=86016" "[Encode][Audeo]nellymoser ID=86049" "[Encode][Audeo]opus ID=86076" "[Encode][Audeo]real_144 ID=77824" "[Encode][Audeo]sbc ID=88083" "[Encode][Audeo]sonic ID=88065" "[Encode][Audeo]sonicls ID=88066" "[Encode][Audeo]truehd ID=86060" "[Encode][Audeo]tta ID=86038" "[Encode][Audeo]vorbis ID=86021" "[Encode][Audeo]wavpack ID=86041" "[Encode][Audeo]wmav1 ID=86023" "[Encode][Audeo]wmav2 ID=86024" "[Encode][Audeo]pcm_alaw ID=65543" "[Encode][Audeo]pcm_dvd ID=65555" "[Encode][Audeo]pcm_f32be ID=65556" "[Encode][Audeo]pcm_f32le ID=65557" "[Encode][Audeo]pcm_f64be ID=65558" "[Encode][Audeo]pcm_f64le ID=65559" "[Encode][Audeo]pcm_mulaw ID=65542" "[Encode][Audeo]pcm_s8 ID=65540" "[Encode][Audeo]pcm_s8_planar ID=65563" "[Encode][Audeo]pcm_s16be ID=65537" "[Encode][Audeo]pcm_s16be_planar ID=65566" "[Encode][Audeo]pcm_s16le ID=65536" "[Encode][Audeo]pcm_s16le_planar ID=65554" "[Encode][Audeo]pcm_s24be ID=65549" "[Encode][Audeo]pcm_s24daud ID=65552" "[Encode][Audeo]pcm_s24le ID=65548" "[Encode][Audeo]pcm_s24le_planar ID=65564" "[Encode][Audeo]pcm_s32be ID=65545" "[Encode][Audeo]pcm_s32le ID=65544" "[Encode][Audeo]pcm_s32le_planar ID=65565" "[Encode][Audeo]pcm_s64be ID=67585" "[Encode][Audeo]pcm_s64le ID=67584" "[Encode][Audeo]pcm_u8 ID=65541" "[Encode][Audeo]pcm_u16be ID=65539" "[Encode][Audeo]pcm_u16le ID=65538" "[Encode][Audeo]pcm_u24be ID=65551" "[Encode][Audeo]pcm_u24le ID=65550" "[Encode][Audeo]pcm_u32be ID=65547" "[Encode][Audeo]pcm_u32le ID=65546" "[Encode][Audeo]pcm_vidc ID=67588" "[Encode][Audeo]roq_dpcm ID=81920" "[Encode][Audeo]adpcm_adx ID=69641" "[Encode][Audeo]g722 ID=69660" "[Encode][Audeo]g726 ID=69643" "[Encode][Audeo]g726le ID=71684" "[Encode][Audeo]adpcm_ima_qt ID=69632" "[Encode][Audeo]adpcm_ima_wav ID=69633" "[Encode][Audeo]adpcm_ms ID=69638" "[Encode][Audeo]adpcm_swf ID=69645" "[Encode][Audeo]adpcm_yamaha ID=69646" "[Encode][Other]ssa ID=96269" "[Encode][Other]ass ID=96269" "[Encode][Other]dvbs
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值