ffmpeg-avio实战:打开本地文件或者网络直播流dome

使用ffmpeg打开打开本地文件或者网络直播流的一个小dome。流程产靠ffmpeg4.x系列的解码流程-CSDN博客

cpp 复制代码
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>  
#include <libavformat/avio.h>
#include <libavutil/file.h>
#include <libavutil/dict.h>
#include <stdio.h>
#include <libavutil/error.h>



int main()
{
    const char* url = "/home/yth/ffmpeg_text/videos/elon.mp4";
    int err = -1;

    //1.
    av_register_all();
    avformat_network_init(); //初始化网络相关的功能,用于处理网络的流媒体
    
    printf("hello ffmpeg\n");

    AVFormatContext* pFormatCtx = NULL; //用于存储媒体文件或流的信息
    AVInputFormat* piFmt = NULL; //指向输入格式的指针,用于指定打开文件时的格式    
    
    printf("helloc, avformat_alloc_context\n");
    if((pFormatCtx = avformat_alloc_context()) == NULL) //为pFormatCtx分配内存
    {
        printf("fail avformat_alloc_context()\n");
        goto avformat_alloc_context_err;
    }
    pFormatCtx->probesize = 5000000;
    pFormatCtx->max_analyze_duration  = 5000000;

    //2.
    printf("打开媒体流\n");
    //打开本地文件或网络流直播流
    //替换为实际的rtsp地址 或在文件路径
    //rtsp地址: rtsp://127.0.0.1:8554/rtsp1
    //本地文件: ./ymx.mp4
    err = avformat_open_input(&pFormatCtx, url,  NULL, NULL);
    if(err < 0)
    {
        char errbuf[AV_ERROR_MAX_STRING_SIZE];
        av_strerror(err, errbuf, sizeof(errbuf));
        fprintf(stderr,"fail avformat_open_input:%s\n",errbuf );
        goto avformat_open_input_err;
    }
    else
    {
        printf("open stream success\n"); //成功打开
    }

    //3.
    err = avformat_find_stream_info(pFormatCtx, NULL); //获取流的信息,填充pFormatCtx
    if(err < 0 )
    {
        printf("fail avformat_find_stream_info\n");
        goto avformat_find_stream_info_err;
    }
    else
    {
        printf("open stream success\n"); //获取流信息成功
        printf("有n路流 = %d\n", pFormatCtx->nb_streams);
    }



avformat_find_stream_info_err:
    avformat_close_input(&pFormatCtx); //关闭输入文件或流
avformat_open_input_err:
    avformat_network_deinit(); //取消网络流库的初始化
avformat_alloc_context_err:
    return err;

    
}

如果是本地文件没有使用 av_register_all(); 打开媒体文件失败,打印失败原因。比如,如果上面没有调用函数av_register_all,则会打印"XXX failed!:Invaliddata found when processing input".

这是代码的实现,我们来执行一下,配合其他工具看看打开的视频流消息是不是对的。

两路流是正确的。

相关推荐
努力还债的学术吗喽18 小时前
ffmpeg离线安装到服务器:解决conda/sudo/无法安装的通用方案
服务器·ffmpeg·conda
zymill1 天前
hysAnalyser --- UDP实时流分析使用指南
ffmpeg·ts流分析·mpegts·音视频分析·数字电视流录制·audio vivid·视频分析工具
Everbrilliant892 天前
FFmpeg解码音频数据AudioTrack/OpenSL播放
ffmpeg·音视频·audiotrack·opensl·ffmpeg音频解码播放·decodethread·opensl播放与解码同步
海南java第二人5 天前
数据库范式详解:从冗余到规范的升华之旅
数据库·oracle·ffmpeg
mortimer5 天前
只需三步,动手搭建一个本地免费【实时语音转录】工具WhisperLiveKit
ffmpeg·github·openai
Hi202402177 天前
消除FFmpeg库的SONAME依赖
linux·ffmpeg
mortimer8 天前
使用阿里AI模型去除背景噪音:单文件40行代码实现
python·ffmpeg·阿里巴巴
今天也想MK代码9 天前
WebRtc语音通话前置铃声处理
ffmpeg·webrtc
撬动未来的支点9 天前
解读ffmpeg控制台输出
ffmpeg
戴草帽的大z9 天前
使用V4L2工具验证RK3588平台视频设备节点数据有效性
ffmpeg·音视频·rk3588·nv12·v4l2-ctl