simplest_ffmpeg_streamer加注释版

给雷神的代码加了注释。。。应该比较详细了吧,毕竟博主也是才刚刚接触,有兴趣的可以一起交流经验。
环境:windows10+VS2015
具体工程就不放了,因为我懒。。。如有需要请留言
参考资料(花了15积分下的呢!):链接:http://pan.baidu.com/s/1gfq2407 密码:wbik
代码:

// simplest_ffmpeg_streamer.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>

#define __STDC_CONSTANT_MACROS

#ifdef _WIN32
//Windows
extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/mathematics.h"
#include "libavutil/time.h"
};
#endif
int main(int argc, char *argv[])
{
    AVOutputFormat *ofmt = NULL;
    AVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = 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 = "test.flv";

    out_filename = "rtmp://localhost/publishlive/livestream";
    //注册所有组件
    av_register_all();
    //注册网络相关的组件
    avformat_network_init();
    /*
    打开输入视频文件,并获得其一些相关信息(&AVFormatContext(双重指针函数), 输入文件名,
    强制指定AVFormatContext中AVInputFormat的。这个参数一般情况下可以设置为NULL,这样FFmpeg可以自动检测AVInputFormat
    ,附加的一些选项,一般情况下可以设置为NULL)
    */
    if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0)
    {
        printf("Could not open input file.");
        goto end;
    }
    //获取视频文件相关信息,错误时返回值为-1
    if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
        printf("Failed to retrieve input stream information");
        goto end;
    }
    //nb_streams:输入视频的AVStream个数
    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        //ifmt_ctx->stream[i],输入视频的AVStream[]数组,具体结构见ppt上的介绍。标记为视频类型
        if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoindex = i;
            break;
        }
    }
    //打印ifmt_ctx里面的具体信息,0代表是input, 1代表是output
    av_dump_format(ifmt_ctx, 0, in_filename, 0);
    /*
    用于初始化AVFormatContext结构体
    ctx:函数调用成功之后创建的AVFormatContext结构体。
    oformat:指定AVFormatContext中的AVOutputFormat,用于确定输出格式。如果指定为NULL,可以设定后两个参数(format_name或者filename)由FFmpeg猜测输出格式。
    PS:使用该参数需要自己手动获取AVOutputFormat,相对于使用后两个参数来说要麻烦一些。
    format_name:指定输出格式的名称。根据格式名称,FFmpeg会推测输出格式。输出格式可以是“flv”,“mkv”等等。
    filename:指定输出文件的名称。根据文件名称,FFmpeg会推测输出格式。文件名称可以是“xx.flv”,“yy.mkv”等等。
    函数执行成功的话,其返回值大于等于0。
    */
    avformat_alloc_output_context2(&ofmt_ctx, NULL, "flv", out_filename);
    if (!ofmt_ctx) {
        printf("Could not create output context\n");
        ret = AVERROR_UNKNOWN;
        goto end;
    }
    //AVOutputFormat类型
    ofmt = ofmt_ctx->oformat;
    for (i = 0; i < ifmt_ctx->nb_streams; i++) {
        //根据输入流创建输出流
        AVStream *in_stream = ifmt_ctx->streams[i];
        //创建输出码流的AVStream
        AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);
        if (!out_stream) {
            printf("Failed allocating output stream\n");
            ret = AVERROR_UNKNOWN;
            goto end;
        }
        //拷贝输入视频码流的AVCodecContex的数值到输出视频码流的AVCodecContex
        ret = avcodec_copy_context(out_stream->codec, in_stream->codec);
        //失败返回-1
        if (ret < 0) {
            printf("Failed to copy context from input to output stream codec context\n");
            goto end;
        }
        //AVCodecContex的codec_tag
        out_stream->codec->codec_tag = 0;
        //设置AVOutputFormat的文件头的类型
        if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
            out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
        //打印一下ofmt_ctx里面的具体信息,最后的1代表是output, 0代表是input
        av_dump_format(ofmt_ctx, 0, out_filename, 1);
        //打开输出URL(Open output URL)
        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);
                goto end;
            }
        }
        //写文件头
        ret = avformat_write_header(ofmt_ctx, NULL);
        if (ret < 0) {
            printf("Error occurred when opening output URL\n");
            goto end;
        }
        //获取
        start_time = av_gettime();
        //对AVStream[]数组的操作
        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) {
                //time_base AVStream的时间基
                AVRational time_base1 = ifmt_ctx->streams[videoindex]->time_base;
                //r_frame_rate AVStream的帧率
                int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(ifmt_ctx->streams[videoindex]->r_frame_rate);
                pkt.pts = (double)(frame_index*calc_duration) / (double)(av_q2d(time_base1)*AV_TIME_BASE);
                //dts 解码时间戳
                pkt.dts = pkt.pts;
                //duration 输入视频的时长,在AVFormatContext结构体中
                pkt.duration = (double)calc_duration / (double)(av_q2d(time_base1)*AV_TIME_BASE);
            }
            //Delay,具体变量意义见上文
            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.dts, time_base, time_base_q);
                int64_t now_time = av_gettime() - start_time;
                if (pts_time > now_time)
                    //延时函数,为了使时间戳能与解码出来的图像相对应
                    av_usleep(pts_time - now_time);
            }
            //AVStream
            in_stream = ifmt_ctx->streams[pkt.stream_index];
            out_stream = ofmt_ctx->streams[pkt.stream_index];
            //copy packet
            //转换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++;
            }
            //将AVPacket(存储视频压缩码流数据)写入文件
            ret = av_interleaved_write_frame(ofmt_ctx, &pkt);

            if (ret < 0) {
                printf("Error muxing packet\n");
                break;
            }
            //销毁指针,释放内存
            av_free_packet(&pkt);
        }
        //写文件尾
        av_write_trailer(ofmt_ctx);
    end:
        //销毁AVFormatContext指针,释放内存
        avformat_close_input(&ifmt_ctx);
        //close output
        if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE)) {
            avio_close(ofmt_ctx->pb);
        }
        //销毁AVOutputFormat指针,释放内存
        avformat_free_context(ofmt_ctx);
        if (ret < 0 && ret != AVERROR_EOF) {
            printf("ERROR occurred.\n");
            return -1;
        }

    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值