ffmpeg4教程7:本地文件推流到rtmp

基于vs2017 vc++  ffmpeg4.0.2下测试

ffmpeg 环境配置请百度(vs2017 ffmpeg ) 

部分方法在https://blog.csdn.net/Java_lilin/article/details/85118365中查找

#include "pch.h"
#include <iostream>

extern "C" {
#include "libavcodec/avcodec.h" 
#include "libavformat/avformat.h"
#include "libavdevice/avdevice.h"
#include "libavutil/imgutils.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/audio_fifo.h"
#include  "libavutil/time.h"
#include  "libavutil/opt.h"
#include "libavutil/mathematics.h"
#include "libavutil/channel_layout.h"    
}

int main(){

AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL; //输入对应一个AVFormatContext,输出对应一个AVFormatContext
    AVOutputFormat  *ofmt = NULL;
    AVPacket        pkt;
    const char      *in_filename, *out_filename;
    int             ret, i;
    int             videoindex = -1;
    int             frame_index = 0;
    int64_t         start_time = 0;

    in_filename = "C:\\Users\\lilin\\Desktop\\1.mp4";//输入URL
    out_filename = "rtmp://192.168.0.108/live/f";//输出 URL[RTMP]

    avformat_network_init();
    //【2】 打开输入输入文件,获取视频索引号
    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0)
    {
        printf("Could not open input file.");
        return -1;
    }
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0)
    {
        printf("Failed to retrieve input stream information");
        return -1;
    }

    for (i = 0; i < ifmt_ctx->nb_streams; i++)
    {
        if (ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            videoindex = i;
            break;
        }
    }
    // 输出输入文件的格式信息
    av_dump_format(ifmt_ctx, 0, in_filename, 0);

    avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_filename); //RTMP 
    if (!ofmt_ctx)
    {
        printf("Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        return -1;
    }

    ofmt = ofmt_ctx->oformat;

    for (i = 0; i < ifmt_ctx->nb_streams; i++)
    {
        //根据输入流创建输出流
        AVStream *in_stream = ifmt_ctx->streams[i];
        AVCodec *incodec = avcodec_find_encoder(in_stream->codecpar->codec_id);
        if (!incodec) {
            printf("not found encoder 1 ");
            return -1;
        }
        AVStream *out_stream = avformat_new_stream(ofmt_ctx, incodec);
        if (!out_stream)
        {
            printf("Failed allocating output stream\n");
            return -1;
        }
        //复制AVCodecContext的设置   
        AVCodecContext *codec_ctx = avcodec_alloc_context3(incodec);
        ret = avcodec_parameters_to_context(codec_ctx, in_stream->codecpar);
        if (ret < 0) {
            printf("Failed to copy in_stream codecpar to codec context\n");
            return -1;
        }
        codec_ctx->codec_tag = 0;
        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
            codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

        ret = avcodec_parameters_from_context(out_stream->codecpar, codec_ctx);
        if (ret < 0) {
            printf("Failed to copy codec context to out_stream codecpar context\n");
        }
    }
    av_dump_format(ofmt_ctx, 0, out_filename, 1);

    if (!(ofmt->flags & AVFMT_NOFILE))
    {
        ret = avio_open(&ofmt_ctx->pb, out_filename, AVIO_FLAG_WRITE);
        if (ret < 0)
        {
            printf("Could not open output URL '%s'", out_filename);
            return -1;
        }
    }
    //写文件头
    ret = avformat_write_header(ofmt_ctx, NULL);
    if (ret < 0)
    {
        printf("Error occurred when opening output URL\n");
        return -1;
    }

    start_time = av_gettime();
    while (1)
    {
        AVStream *in_stream, *out_stream;
        //获取一个AVPacket
        ret = av_read_frame(ifmt_ctx, &pkt);

        if (ret < 0) break;

        //设置 PTS
        if (pkt.pts == AV_NOPTS_VALUE)
        {
            //原始两帧之间的时间
            int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(ifmt_ctx->streams[videoindex]->r_frame_rate);

            //设置一帧的时间参数
            AVRational time_base1 = ifmt_ctx->streams[videoindex]->time_base;
            pkt.pts = (double)(frame_index*calc_duration) / (double)(av_q2d(time_base1)*AV_TIME_BASE);
            pkt.dts = pkt.pts;
            pkt.duration = (double)calc_duration / (double)(av_q2d(time_base1)*AV_TIME_BASE);
        }

        //帧延时
        if (pkt.stream_index == videoindex)
        {
            AVRational time_base = ifmt_ctx->streams[videoindex]->time_base;
            AVRational time_base_q = { 1, AV_TIME_BASE };

            int64_t pts_time = av_rescale_q(pkt.pts, time_base, time_base_q); //显示的时间
            int64_t now_time = av_gettime() - start_time;

            if (pts_time > now_time)
                av_usleep(pts_time - now_time);
        }

        in_stream = ifmt_ctx->streams[pkt.stream_index];
        out_stream = ofmt_ctx->streams[pkt.stream_index];

        //转换PTS/DTS
        pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(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;

        //Print to Screen
        if (pkt.stream_index == videoindex)
        {
            printf("Send %8d video frames to output URL\n", frame_index);
            frame_index++;
        }

        ret = av_interleaved_write_frame(ofmt_ctx, &pkt);
        if (ret < 0)
        {
            printf("Error muxing packet\n");
            break;
        }

        av_packet_unref(&pkt);
    }
    //【5】写文件尾
    av_write_trailer(ofmt_ctx);

    avformat_close_input(&ifmt_ctx);

    //【6】 关闭输出
    if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) {
        avio_close(ofmt_ctx->pb);
    }
    avformat_free_context(ofmt_ctx);
    if (ret < 0 && ret != AVERROR_EOF)
    {
        printf("end Error occurred.\n");
        return -1;
    }

    return 0;

}

讨论群261074724

猜你喜欢

转载自blog.csdn.net/Java_lilin/article/details/85123579