ffmpeg play network streaming

今天我們開始正式進入FFmpeg的篇章,FFmpeg作爲著名的開源框架,可以生成用於處理多媒體框架的庫和程序,是音視頻界內的聖經,市面上直播開發99%都是基於FFmpeg來開發的,這足以證明FFmpeg的強大。關於FFmpeg的源碼和官方文檔可以去FFmpeg下載源碼和編譯好的庫。
  閒話不多說,下面就開始今天的主要內容,FFmpeg保存網絡流到本地
  直播不像點播,當我們看到想看的內容時,我們不能倒退回去,但是我們可以保存直播流爲本地文件,這樣我們想看隨時都可以。

保存網絡流的流程主要有以下步驟:
   第一步:註冊所有的組件(編解碼、濾鏡特效處理庫、封裝格式處理庫、工具庫、音頻採樣數據格式轉換庫、視頻像素數據格式轉換等等...)
  第二步:獲取視頻流的封裝信息,查找視頻和音頻流的位置
  第三步:查找視頻和音頻解碼器id,根據解碼器id打開解碼器
  第四步:創建輸出流並拷貝流上下文信息
  第五步:循環讀取網絡流,解碼packet並寫入本地
  第六步:關閉解碼器釋放內存

源碼


#include "stdafx.h"
#include "pch.h"
#include <string>
#include <memory>
#include <thread>
#include <iostream>
using namespace std;

AVFormatContext *inputContext = nullptr;
AVFormatContext * outputContext;
int64_t lastReadPacktTime ;

static int interrupt_cb(void *ctx)
{
    int  timeout  = 3;
    if(av_gettime() - lastReadPacktTime > timeout *1000 *1000)
    {
        return -1;
    }
    return 0;
}
int OpenInput(string inputUrl)
{
    inputContext = avformat_alloc_context();    
    lastReadPacktTime = av_gettime();
    inputContext->interrupt_callback.callback = interrupt_cb;
    int ret = avformat_open_input(&inputContext, inputUrl.c_str(), nullptr,nullptr);
    if(ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "Input file open input failed\n");
        return  ret;
    }
    ret = avformat_find_stream_info(inputContext,nullptr);
    if(ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "Find input file stream inform failed\n");
    }
    else
    {
        av_log(NULL, AV_LOG_FATAL, "Open input file  %s success\n",inputUrl.c_str());
    }
    return ret;
}

shared_ptr<AVPacket> ReadPacketFromSource()
{
    shared_ptr<AVPacket> packet(static_cast<AVPacket*>(av_malloc(sizeof(AVPacket))), [&](AVPacket *p) { av_packet_free(&p); av_freep(&p);});
    av_init_packet(packet.get());
    lastReadPacktTime = av_gettime();
    int ret = av_read_frame(inputContext, packet.get());
    if(ret >= 0)
    {
        return packet;
    }
    else
    {
        return nullptr;
    }
}
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
{
    if (pkt->pts != AV_NOPTS_VALUE)
        pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
    if (pkt->dts != AV_NOPTS_VALUE)
        pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
    if (pkt->duration > 0)
        pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
}
int WritePacket(shared_ptr<AVPacket> packet)
{
    auto inputStream = inputContext->streams[packet->stream_index];
    auto outputStream = outputContext->streams[packet->stream_index];               
    av_packet_rescale_ts(packet.get(),inputStream->time_base,outputStream->time_base);//時間戳轉換,輸入上下文與輸出上下文時間基準不同
    //也可以用av_write_frame
    return av_interleaved_write_frame(outputContext, packet.get());
}

int OpenOutput(string outUrl)
{
    
    int ret  = avformat_alloc_output_context2(&outputContext, nullptr, "mpegts", outUrl.c_str());
    if(ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "open output context failed\n");
        goto Error;
    }

    ret = avio_open2(&outputContext->pb, outUrl.c_str(), AVIO_FLAG_WRITE,nullptr, nullptr); 
    if(ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "open avio failed");
        goto Error;
    }

    for(int i = 0; i < inputContext->nb_streams; i++)
    {
        //輸出依賴於輸入
        AVStream * stream = avformat_new_stream(outputContext, inputContext->streams[i]->codec->codec);             
        ret = avcodec_copy_context(stream->codec, inputContext->streams[i]->codec); 
        if(ret < 0)
        {
            av_log(NULL, AV_LOG_ERROR, "copy coddec context failed");
            goto Error;
        }
    }

    ret = avformat_write_header(outputContext, nullptr);
    if(ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "format write header failed");
        goto Error;
    }

    av_log(NULL, AV_LOG_FATAL, " Open output file success %s\n",outUrl.c_str());            
    return ret ;
Error:
    if(outputContext)
    {
        for(int i = 0; i < outputContext->nb_streams; i++)
        {
            avcodec_close(outputContext->streams[i]->codec);
        }
        avformat_close_input(&outputContext);
    }
    return ret ;
}

void CloseInput()
{
    if(inputContext != nullptr)
    {
        avformat_close_input(&inputContext);
    }
}

void CloseOutput()
{
    if(outputContext != nullptr)
    {
        for(int i = 0 ; i < outputContext->nb_streams; i++)
        {
            AVCodecContext *codecContext = outputContext->streams[i]->codec;
            avcodec_close(codecContext);
        }
        avformat_close_input(&outputContext);
    }
}
void Init()
{
    av_register_all();
    avfilter_register_all();
    avformat_network_init();
    av_log_set_level(AV_LOG_ERROR);
}
int main(int argc, char *argv[])
{
    Init();
    int ret = OpenInput("rtmp://v1.one-tv.com/live/mpegts.stream");
    if(ret >= 0)
    {
        //rtmp://192.168.1.107/oflaDemo/test
        //ret = OpenOutput("rtmp://127.0.0.1:1935/live/stream0"); //播放地址爲rtmp://127.0.0.1/live/stream0 live=1
        ret = OpenOutput("D:\\test.ts"); 
    }
    if(ret <0) goto Error;

    while(true)
    {
        auto packet = ReadPacketFromSource();
        if(packet)
        {
            ret = WritePacket(packet);
            if(ret >= 0)
            {
                cout<<"WritePacket Success!"<<endl;
            }
            else
            {
                cout<<"WritePacket failed!"<<endl;
            }
        }
        else
        {
            break;
        }
    }
    

Error:
    CloseInput();
    CloseOutput();
    while(true)
    {
        this_thread::sleep_for(chrono::seconds(100));
    }
    return 0;
}

在上面代碼函數裏面有個函數av_packet_rescale_ts是不是看不懂,其實這裏是跳調整時間戳,因爲輸入和輸出流的時間基準不一定相同,所有這裏需要進行時間戳轉換。

  interrupt_cb這個函數的官方解釋是爲I/O層自定義中斷回調,在avformat_open_input之前設置,其實就是讀取輸入數據時的一個回調,在這裏我們稍微做一個超時處理,如果讀取超過3秒就返回一個錯誤碼中斷讀取流數據。

  好了,利用FFmpeg保存網絡流就是這麼簡單,這裏留個擴展,你可以試試利用FFmpeg保存網絡圖片。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章