FFmpeg實戰 保存網絡流

??今天我們開始正式進入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保存網絡圖片。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 教程一:視頻截圖(Tutorial 01: Making Screencaps) 首先我們需要了解視頻文件的一些基...
    90后的思維閱讀 4,794評論 0 3
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,523評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,252評論 4 61
  • 今天主要講兩點,一是不要在原則問題上面輕易妥協,妥協的結果就是自己變得懈怠,做出來的產品和事情也不一定能達到百分百...
    萌叔叔666閱讀 206評論 0 0
  • 民國三十六年冬禪七中,我(靈源老和尚)上方丈請開示。 師公問我:“你用什么工夫?”我說:“亦念佛,亦參禪,禪凈雙修...
    洞見妙山閱讀 483評論 0 0