FFmpeg實(shí)戰(zhàn) 保存網(wǎng)絡(luò)流

??今天我們開始正式進(jìn)入FFmpeg的篇章,F(xiàn)Fmpeg作為著名的開源框架,可以生成用于處理多媒體框架的庫和程序,是音視頻界內(nèi)的圣經(jīng),市面上直播開發(fā)99%都是基于FFmpeg來開發(fā)的,這足以證明FFmpeg的強(qiáng)大。關(guān)于FFmpeg的源碼和官方文檔可以去FFmpeg下載源碼和編譯好的庫。
??閑話不多說,下面就開始今天的主要內(nèi)容,F(xiàn)Fmpeg保存網(wǎng)絡(luò)流到本地
??直播不像點(diǎn)播,當(dāng)我們看到想看的內(nèi)容時(shí),我們不能倒退回去,但是我們可以保存直播流為本地文件,這樣我們想看隨時(shí)都可以。

保存網(wǎng)絡(luò)流的流程主要有以下步驟:
?? 第一步:注冊(cè)所有的組件(編解碼、濾鏡特效處理庫、封裝格式處理庫、工具庫、音頻采樣數(shù)據(jù)格式轉(zhuǎn)換庫、視頻像素?cái)?shù)據(jù)格式轉(zhuǎn)換等等...)
??第二步:獲取視頻流的封裝信息,查找視頻和音頻流的位置
??第三步:查找視頻和音頻解碼器id,根據(jù)解碼器id打開解碼器
??第四步:創(chuàng)建輸出流并拷貝流上下文信息
??第五步:循環(huán)讀取網(wǎng)絡(luò)流,解碼packet并寫入本地
??第六步:關(guān)閉解碼器釋放內(nèi)存

源碼

#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);//時(shí)間戳轉(zhuǎn)換,輸入上下文與輸出上下文時(shí)間基準(zhǔn)不同
    //也可以用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;
}

??在上面代碼函數(shù)里面有個(gè)函數(shù)av_packet_rescale_ts是不是看不懂,其實(shí)這里是跳調(diào)整時(shí)間戳,因?yàn)檩斎牒洼敵隽鞯臅r(shí)間基準(zhǔn)不一定相同,所有這里需要進(jìn)行時(shí)間戳轉(zhuǎn)換。

??interrupt_cb這個(gè)函數(shù)的官方解釋是為I/O層自定義中斷回調(diào),在avformat_open_input之前設(shè)置,其實(shí)就是讀取輸入數(shù)據(jù)時(shí)的一個(gè)回調(diào),在這里我們稍微做一個(gè)超時(shí)處理,如果讀取超過3秒就返回一個(gè)錯(cuò)誤碼中斷讀取流數(shù)據(jù)。

??好了,利用FFmpeg保存網(wǎng)絡(luò)流就是這么簡(jiǎn)單,這里留個(gè)擴(kuò)展,你可以試試?yán)肍Fmpeg保存網(wǎng)絡(luò)圖片。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容