iOS直播內(nèi)容保存為本地視頻

前段時(shí)間接了個(gè)需求,就是將直播內(nèi)容保存為本地視頻,期間踩了一些坑,所以記錄一下。

PS:直播流是 RTMP流,播放器是基于 ffmpeg 的ijkplayer。

思路其實(shí)很簡(jiǎn)單,播放器已經(jīng)將視頻碼流和音頻碼流封裝好,我們只需要將這些封裝好的壓縮編碼數(shù)據(jù)保存成視頻文件就 OK了。

一些用到的參數(shù)

    AVFormatContext *m_ofmt_ctx;        // 用于輸出的AVFormatContext結(jié)構(gòu)體
    AVOutputFormat *m_ofmt;
    pthread_mutex_t record_mutex;       // 鎖
    int is_record;                      // 是否在錄制             
    int record_error;    
    
    int is_first;                       // 第一幀數(shù)據(jù)
    int64_t start_pts;                  // 開始錄制時(shí)pts
    int64_t start_dts;                  // 開始錄制時(shí)dts

1.拷貝參數(shù),打開輸出文件,寫文件頭

int ffp_start_record(FFPlayer *ffp, const char *file_name)
{
    assert(ffp);
    VideoState *is = ffp->is;
    
    ffp->m_ofmt_ctx = NULL; 
    ffp->m_ofmt = NULL;
    ffp->is_record = 0; 
    ffp->record_error = 0; 
    
    if (!file_name || !strlen(file_name)) { // 沒有路徑
        av_log(ffp, AV_LOG_ERROR, "filename is invalid");
        goto end;
    }
    
    if (!is || !is->ic|| is->paused || is->abort_request) { // 沒有上下文,或者上下文已經(jīng)停止
        av_log(ffp, AV_LOG_ERROR, "is,is->ic,is->paused is invalid");
        goto end;
    }
    
    if (ffp->is_record) { // 已經(jīng)在錄制
        av_log(ffp, AV_LOG_ERROR, "recording has started");
        goto end;
    }
    
    // 初始化一個(gè)用于輸出的AVFormatContext結(jié)構(gòu)體
    avformat_alloc_output_context2(&ffp->m_ofmt_ctx, NULL, NULL, file_name);
    if (!ffp->m_ofmt_ctx) {
        av_log(ffp, AV_LOG_ERROR, "Could not create output context filename is %s\n", file_name);
        goto end;
    }
    ffp->m_ofmt = ffp->m_ofmt_ctx->oformat;
    
    for (int i = 0; i < is->ic->nb_streams; i++) {
        // 對(duì)照輸入流創(chuàng)建輸出流通道
        AVStream *in_stream = is->ic->streams[i];
        AVStream *out_stream = avformat_new_stream(ffp->m_ofmt_ctx, in_stream->codec->codec);
        if (!out_stream) {
            av_log(ffp, AV_LOG_ERROR, "Failed allocating output stream\n");
            goto end;
        }

        // 將輸入視頻/音頻的參數(shù)拷貝至輸出視頻/音頻的AVCodecContext結(jié)構(gòu)體
        av_log(ffp, AV_LOG_DEBUG, "in_stream->codec;%@\n", in_stream->codec);
        if (avcodec_copy_context(out_stream->codec, in_stream->codec) < 0) {
            av_log(ffp, AV_LOG_ERROR, "Failed to copy context from input to output stream codec context\n");
            goto end;
        }
        
        out_stream->codec->codec_tag = 0;
        if (ffp->m_ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) {
            out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
        }
    }
    
     av_dump_format(ffp->m_ofmt_ctx, 0, file_name, 1);
    
    // 打開輸出文件
    if (!(ffp->m_ofmt->flags & AVFMT_NOFILE)) {
        if (avio_open(&ffp->m_ofmt_ctx->pb, file_name, AVIO_FLAG_WRITE) < 0) {
            av_log(ffp, AV_LOG_ERROR, "Could not open output file '%s'", file_name);
            goto end;
        }
    }

    // 寫視頻文件頭
    if (avformat_write_header(ffp->m_ofmt_ctx, NULL) < 0) {
        av_log(ffp, AV_LOG_ERROR, "Error occurred when opening output file\n");
        goto end;
    }

    ffp->is_record = 1;
    ffp->record_error = 0;
    pthread_mutex_init(&ffp->record_mutex, NULL);
    
    return 0;
end:
    ffp->record_error = 1;
    return -1;
}

ffp_start_record方法,點(diǎn)擊開始錄制時(shí)調(diào)用。

2.保存為視頻文件

int ffp_record_file(FFPlayer *ffp, AVPacket *packet)
{
    assert(ffp);
    VideoState *is = ffp->is;
    int ret = 0;
    AVStream *in_stream;
    AVStream *out_stream;
    
    if (ffp->is_record) {
        if (packet == NULL) {
            ffp->record_error = 1;
            av_log(ffp, AV_LOG_ERROR, "packet == NULL");
            return -1;
        }
        
        AVPacket *pkt = (AVPacket *)av_malloc(sizeof(AVPacket)); // 與看直播的 AVPacket分開,不然卡屏
        av_new_packet(pkt, 0);
        if (0 == av_packet_ref(pkt, packet)) {
            pthread_mutex_lock(&ffp->record_mutex);
            
            if (!ffp->is_first) { // 錄制的第一幀,時(shí)間從0開始
                ffp->is_first = 1;
                pkt->pts = 0;
                pkt->dts = 0;
            } else { // 之后的每一幀都要減去,點(diǎn)擊開始錄制時(shí)的值,這樣的時(shí)間才是正確的
                pkt->pts = abs(pkt->pts - ffp->start_pts);
                pkt->dts = abs(pkt->dts - ffp->start_dts);
            }

            in_stream  = is->ic->streams[pkt->stream_index];
            out_stream = ffp->m_ofmt_ctx->streams[pkt->stream_index];
            
            // 轉(zhuǎn)換PTS/DTS
            pkt->pts = av_rescale_q_rnd(pkt->pts, in_stream->time_base, out_stream->time_base, (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
            pkt->dts = av_rescale_q_rnd(pkt->dts, in_stream->time_base, out_stream->time_base, (AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
            pkt->duration = av_rescale_q(pkt->duration, in_stream->time_base, out_stream->time_base);
            pkt->pos = -1;
                  
            // 寫入一個(gè)AVPacket到輸出文件
            if ((ret = av_interleaved_write_frame(ffp->m_ofmt_ctx, pkt)) < 0) {
                av_log(ffp, AV_LOG_ERROR, "Error muxing packet\n");
            }
            
            av_packet_unref(pkt);
            pthread_mutex_unlock(&ffp->record_mutex);            
        } else {
            av_log(ffp, AV_LOG_ERROR, "av_packet_ref == NULL");
        }
    }
    return ret;
}

ffp_record_file可放在處理流的函數(shù)里面,用ffmpeg的可放在read_thread函數(shù)中

if (!ffp->is_first && pkt->pts == pkt->dts) { // 獲取開始錄制前dts等于pts最后的值,用于
     ffp->start_pts = pkt->pts;
     ffp->start_dts = pkt->dts;
}
        
if (ffp->is_record) { // 可以錄制時(shí),寫入文件
   if (0 != ffp_record_file(ffp, pkt)) {
       ffp->record_error = 1;
       ffp_stop_record(ffp);
   }
}

需要注意3點(diǎn):

  1. 要av_malloc一個(gè)AVPacket,不然會(huì)影響到播放器的播放。
  2. 因?yàn)槲覀兡玫氖遣シ艜r(shí)的壓縮數(shù)據(jù),開始錄制時(shí),可能已經(jīng)播放一段時(shí)間了,取到的是播放時(shí)候的 pts 和 dts,所以得記錄錄制時(shí)的初始幀,之后的每一幀都要減去初始幀。(剛開始我也踩坑了,ijk 播放沒問題,用別的播放器,開頭出現(xiàn)了幾秒鐘黑屏)
  3. 只有AVStream中的PTS*time_base=真正的時(shí)間,我們轉(zhuǎn)換下PTS/DTS。(這里我也沒理解透,不轉(zhuǎn)換音視頻會(huì)不同步)

3.最后結(jié)束錄制

int ffp_stop_record(FFPlayer *ffp)
{
    assert(ffp);    
    if (ffp->is_record) {
        ffp->is_record = 0;
        pthread_mutex_lock(&ffp->record_mutex);
        if (ffp->m_ofmt_ctx != NULL) {
            av_write_trailer(ffp->m_ofmt_ctx);            
            if (ffp->m_ofmt_ctx && !(ffp->m_ofmt->flags & AVFMT_NOFILE)) {
                avio_close(ffp->m_ofmt_ctx->pb);
            }
            avformat_free_context(ffp->m_ofmt_ctx);
            ffp->m_ofmt_ctx = NULL;
            ffp->is_first = 0;
        }
        pthread_mutex_unlock(&ffp->record_mutex);
        pthread_mutex_destroy(&ffp->record_mutex);
        av_log(ffp, AV_LOG_DEBUG, "stopRecord ok\n");
    } else {
        av_log(ffp, AV_LOG_ERROR, "don't need stopRecord\n");
    }
    return 0;
}

結(jié)束錄制時(shí),調(diào)用ffp_stop_record方法,直播內(nèi)容就保存為本地視頻了。

參考:

最后編輯于
?著作權(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)容

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