FFMPEG SDK 开发介绍

简介: FFMPEG SDK 开发介绍 1.简介:     ffmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。 使用ffmpeg能够完成如下功能:parse,demux,decode,filter(preprocessing),encode,mux,stream和player等. 2.下载和编译:     下载地址:  http://ffmpeg.
FFMPEG SDK 开发介绍

1.简介:
    ffmpeg是一套可以用来记录、转换数字音频、视频,并能将其转化为流的开源计算机程序。
使用ffmpeg能够完成如下功能:parse,demux,decode,filter(preprocessing),encode,mux,stream和player等.

2.下载和编译:

    下载地址:  http://ffmpeg.org/download.html

    编译:
       1)windows平台static library/shared library, 编译工具:mingw-gcc或者在linux平台下交叉编译(推荐)
       2)linux平台static library/shared library, 编译工具:gcc

    模块:
        libavcodec    - 编码解码器
        libavdevice   - 输入输出设备的支持
        libavfilter   - 视音频滤镜支持
        libavformat   - 视音频等格式的解析
        libavutil     - 工具库
        libpostproc   - 后期效果处理
        libswscale    - 图像颜色、尺寸转换
    
3.SDK介绍和开发(基于ffmpeg 0.8 sdk)
    ffmpeg每部分功能都采用plugin的方式,使用统一的接口调用,这样就能够非常方便的使用和扩展。
    plugin分为几种:muxer,demuxer,protocol,hwaccel,encoder,decoder,parser,bitstream,filter,...
    因此在使用SDK的时候第一步就是注册plugin
    
    avcodec_register_all()  : 注册 hwaccel,encoder,decoder,parser,bitstream
    av_register_all()       : 注册 muxer,demuxer,protocol
    avfilter_register_all() : 注册 滤镜filter
    
    下面根据不同的应用场景,给出主要的代码示例(仅是代码片断,不一定能编译通过):
    
    1)如何获取媒体文件的信息(Parser):
    // 参考V3代码: interface IFileDecoder, media/impl/filedecoderimpl.cpp
    
    {
        av_register_all();
        AVFormatContext * pFormatCtx = NULL;
        int err = 0;
        const char *fileName = "c:\\test.mp4";
        err = av_open_input_file(&pFormatCtx, fileName, NULL, 0, NULL);
        if(err != 0)
        {
            // break ;
        }
        err = av_find_stream_info(pFormatCtx);
        if(err < 0)
        {
            // break ;
        }
        for(uint32_t i = 0; i < pFormatCtx->nb_streams; i ++)
        {
            // stream 结构数据
            AVStream *pStream = pFormatCtx->streams[i];
            // 帧率信息
            AVRational frameRate = pStream->r_frame_rate;
            // 时间单位比率
            AVRational timeBase = pStream->time_base;
            // stream duration
            int64_t duration = pStream->duration;
            
            // 获取Codec数据结构
            AVCodecContext *pCodecCtx = pStream->codec;
            AVMediaType codecType = pCodecCtx->codec_type;
           
            CodecID codecId = pCodecCtx->codec_id;
           
            
            if(codecType == AVMEDIA_TYPE_VIDEO)
            {
                // 获取Video基本信息
                int width = pCodecCtx->width;
                int height = pCodecCtx->height;
                PixelFormat pixelFormat = pCodecCtx->pix_fmt;
            }
            else if(codecType == AVMEDIA_TYPE_AUDIO)
            {
                // 获取Audio基本信息
                int channels = pCodecCtx->channels;
                int sample_rate = pCodecCtx->sample_rate;
                AVSampleFormat sampleFmt = pCodecCtx->sample_fmt;
            }
        }
        // 释放
        if(pFormatCtx != NULL)
        {
            av_close_input_file(pFormatCtx);
            pFormatCtx = NULL;
        }    
    }
    
    2)读取sample数据(Read raw sample不解码)
    // 参考V3代码: interface IFileDecoder, media/impl/filedecoderimpl.cpp

    {
        // 参考Parser代码
        // av_register_all();
        // AVFormatContext * pFormatCtx = NULL;
        // err = av_open_input_file(&pFormatCtx, fileName, NULL, 0, NULL);
    
        AVPacket packet;
        av_init_packet(&packet);
        int ret = av_read_frame(pFormatCtx, &packet);
        if(ret >= 0)
        {
            int streamIndex = packet.stream_index;
            AVStream *pStream = pFormatCtx->streams[streamIndex];
            AVCodecContext *pCodecCtx = pStream->codec;
            // 计算timestamp
    
            // 转换时间到1/1000000秒
            AVRational time_base;
            time_base.num = 1;
            time_base.den = 1000000;
            
            // 25.0     1/25,   29.97    1001/30000
            
            // 获取 dts/pts
            const int64_t dts = av_rescale_q(packet.dts, pStream->time_base, time_base);
            const int64_t pts = av_rescale_q(packet.pts, pStream->time_base, time_base);
            uint8_t *data = packet.data;
            int size = packet.size;
            bool isKey = ((packet.flags & AV_PKT_FLAG_KEY) == AV_PKT_FLAG_KEY);    
        }
        av_free_packet(&packet);        
    }
    
    3)解码sample(Video ES=>YUV/RGB,  Audio ES=>PCM)
    // 参考V3代码: interface IVideoDecoder/IAudioDecoder, media/impl/videodecoderimpl.cpp/audiodecoderimpl.cpp
    {
        // 参考Parser,Read raw sample代码
        
相关文章
|
4天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
7月前
|
算法 IDE 开发工具
火爆全网开源额温枪同平台之华大HC32L136 SDK开发入门
火爆全网开源额温枪同平台之华大HC32L136 SDK开发入门
136 1
|
5月前
|
API 语音技术 开发工具
FastASR+FFmpeg(音视频开发+语音识别)(二)
FastASR+FFmpeg(音视频开发+语音识别)(二)
122 0
|
19天前
|
编解码 缓存 算法
FFmpeg开发笔记(四)FFmpeg的动态链接库介绍
FFmpeg是一个强大的多媒体处理框架,提供ffmpeg、ffplay和ffprobe工具及八个库:avcodec(编解码)、avdevice(设备输入输出)、avfilter(音视频滤镜)、avformat(格式处理)、avutil(通用工具和算法)、postproc(后期效果)、swresample(音频重采样)和swscale(视频图像转换)。这些库支持定制化开发,涵盖了从采集、编码、过滤到输出的全过程。了解详细FFmpeg开发信息,可参考《FFmpeg开发实战:从零基础到短视频上线》。
32 0
FFmpeg开发笔记(四)FFmpeg的动态链接库介绍
|
19天前
|
编解码 搜索推荐 开发者
FFmpeg开发笔记(三)FFmpeg的可执行程序介绍
FFmpeg提供ffmpeg、ffplay和ffprobe三个可执行程序。ffmpeg用于音视频转换和查询支持信息,如编解码器、文件格式和协议。ffplay是一个简单的播放器,支持播放音视频并显示相关信息。ffprobe用于分析多媒体文件参数和数据包详情。《FFmpeg开发实战:从零基础到短视频上线》一书提供更深入的开发知识。
24 4
FFmpeg开发笔记(三)FFmpeg的可执行程序介绍
|
20天前
|
Linux API C语言
FFmpeg开发笔记(一)搭建Linux系统的开发环境
本文指导初学者如何在Linux上搭建FFmpeg开发环境。首先,由于FFmpeg依赖第三方库,可以免去编译源码的复杂过程,直接安装预编译的FFmpeg动态库。推荐网站<https://github.com/BtbN/FFmpeg-Builds/releases>提供适用于不同系统的FFmpeg包。但在安装前,需确保系统有不低于2.22版本的glibc库。详细步骤包括下载glibc-2.23源码,配置、编译和安装。接着,下载Linux版FFmpeg安装包,解压至/usr/local/ffmpeg,并设置环境变量。最后编写和编译简单的C或C++测试程序验证FFmpeg环境是否正确配置。
37 8
FFmpeg开发笔记(一)搭建Linux系统的开发环境
|
4月前
|
开发工具 CDN 容器
基于Html+腾讯云播SDK开发的m3u8播放器
周末业余时间在家无事,学习了一下腾讯的云播放sdk,并制作了一个小demo(m3u8播放器),该在线工具是基于腾讯的云播sdk开发的,云播sdk非常牛,可以支持多种播放格式。
101 1
|
22天前
|
开发工具
使用FFmpeg4.3.1的SDK官方开发包编译ffmpeg.c(二)
使用FFmpeg4.3.1的SDK官方开发包编译ffmpeg.c(二)
12 0
|
5月前
|
API 开发工具 C#
一套基于 .NET Core 开发的支付SDK集 - paylink
一套基于 .NET Core 开发的支付SDK集 - paylink
|
4月前
|
API 开发工具 C#
[相机开发] VC++联合相机SDK开发
[相机开发] VC++联合相机SDK开发
42 0