ffmpeg H264转MP4

版本是ffmpeg4.3(高几个版本或者低几个版本都可以)

结果 把H264转成MP4
使用的H264文件是D:/videos/264.dat(dat后缀名无所谓)
264.dat下载,不用积分
可以使用ffplay播放

ffplay  D:/videos/264.dat

最终生成文件可以双击播放

void startTransform()
{
    int nRet = 0;
    AVFormatContext *pInFmtCtx = nullptr;
    const char *pInFileName = "D:/videos/264.dat";
    const char *pOutFileName = "D:/output/264.mp4";
    AVDictionary *pDic = nullptr;
    nRet = avformat_open_input(&pInFmtCtx,pInFileName,nullptr,&pDic);
    if( nRet < 0)
    {
        printf("Could not open input file.");
        return;
    }
    avformat_find_stream_info(pInFmtCtx, nullptr);
    printf("===========Input Information==========\n");
    av_dump_format(pInFmtCtx, 0, pInFileName, 0);
    printf("======================================\n");
    //Output
    AVFormatContext *pMp4FmtCtx  = nullptr;
    AVOutputFormat* pMp4OutFormat = nullptr;
    avformat_alloc_output_context2(&pMp4FmtCtx , nullptr, nullptr, pOutFileName);
    if (!pMp4FmtCtx ) {
        printf("Could not create output context\n");
        return;
    }
    pMp4OutFormat = pMp4FmtCtx->oformat;
    if (avio_open(&(pMp4FmtCtx->pb), pOutFileName, AVIO_FLAG_READ_WRITE) < 0)
    {
       printf("avio_open fail.");
       return;
    }
    unsigned int i = 0,videoindex_v=0;
    int videoindex_out=0;
    for (;i < pInFmtCtx->nb_streams; i++)
    {
        AVStream *in_stream = pInFmtCtx->streams[i];
        AVCodec *pInCodec = avcodec_find_decoder(in_stream->codecpar->codec_id);
        AVCodecContext* pInCodecCtx = avcodec_alloc_context3(pInCodec); //
        nRet = avcodec_parameters_to_context(pInCodecCtx, pInFmtCtx->streams[videoindex_v]->codecpar);
        AVStream *out_stream = avformat_new_stream(pMp4FmtCtx, pInCodecCtx->codec);
        if (!out_stream) {
            printf("Failed allocating output stream\n");
            return;
        }
        videoindex_v = i;
        videoindex_out = out_stream->index;

        if( nRet < 0)
        {
            printf("avcodec_parameters_to_context fail.\n");
            return;
        }

        pInCodecCtx->codec_tag = 0;
        if (pMp4FmtCtx->oformat->flags & AVFMT_GLOBALHEADER)
            pInCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;

        nRet = avcodec_parameters_from_context(out_stream->codecpar, pInCodecCtx);
        if( nRet < 0)
        {
            printf("avcodec_parameters_from_context fail.\n");
            return;
        }
    }
    printf("==========Output Information==========\n");
    av_dump_format(pMp4FmtCtx, 0, pOutFileName, 1);
    printf("======================================\n");
    //Write file header
    if (avformat_write_header(pMp4FmtCtx, nullptr) < 0) {
        printf("Error occurred when opening output file\n");
        return;
    }
    AVPacket pkt;
    int frame_index = 0;
    int64_t cur_pts_v = 0;
    while (1) {
        AVFormatContext *ifmt_ctx;
        int stream_index = 0;
        AVStream *in_stream, *out_stream;
        //Get an AVPacket
        //if(av_compare_ts(cur_pts_v,ifmt_ctx_v->streams[videoindex_v]->time_base,cur_pts_a,ifmt_ctx_a->streams[audioindex_a]->time_base) <= 0)
        {
            ifmt_ctx = pInFmtCtx;
            stream_index = videoindex_out;
            if (av_read_frame(ifmt_ctx, &pkt) >= 0) {
                do {
                    in_stream = ifmt_ctx->streams[pkt.stream_index];
                    out_stream = pMp4FmtCtx->streams[stream_index];
                    printf("stream_index==%d,pkt.stream_index==%d,videoindex_v=%d\n", stream_index, pkt.stream_index, videoindex_v);
                    if (pkt.stream_index == videoindex_v) {
                        //FIX:No PTS (Example: Raw H.264)
                        //Simple Write PTS
                        if (pkt.pts == AV_NOPTS_VALUE) {
                            //Write PTS
                            AVRational time_base1 = in_stream->time_base;
                            //Duration between 2 frames (us)
                            int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(in_stream->r_frame_rate);
                            //Parameters
                            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);
                            frame_index++;
                        }
                        cur_pts_v = pkt.pts;
                        break;
                    }
                } while (av_read_frame(ifmt_ctx, &pkt) >= 0);
            }
            else {
                break;
            }
        }

        //Convert 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;
        pkt.stream_index = stream_index;
        printf("Write 1 Packet. size:%5d\tpts:%lld\n", pkt.size, pkt.pts);
        //Write
        if (av_interleaved_write_frame(pMp4FmtCtx, &pkt) < 0) {
            printf("Error muxing packet\n");
            break;
        }
        av_free_packet(&pkt);
    }
    //Write file trailer
    av_write_trailer(pMp4FmtCtx);
}
int main()
{
	startTransform();
}
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值