ffmpeg - 如何使用 libavformat 连接 2 个具有相同编解码器的视频文件(重新混合)?

标签 ffmpeg libav libavformat muxer

我已经从 CDN 下载了 flv 格式的视频(视频 H264 和音频 AAC),并将它们重新合成为 MP4 格式。但是视频受长度限制。所以我已经分几个部分下载了每个视频:从起点开始,在第 1 点,在第 2 点(通过在 url 中使用 seek 参数)。每个点的开始时间都比前一个点的结束时间早一点。
使用 av_read_frame我扫描了所有部分,发现相交的数据包不仅具有相同的大小和顺序,而且它们的 dts/pts 彼此偏移了一个常数值。因此,要将开始视频与从第 1 点开始的视频连接起来,我必须执行以下操作:
1.在输出文件中创建输出头
2. 从起始视频中复制所有不相交的数据包。
3. 从点 1 开始的视频中复制所有不相交的数据包,并通过将其移动常量来更改 dts 值

如何使用 libav(不是 ffmpeg)来完成所有这些工作? 我读了How can libavformat be used without using other libav libraries .但是在 libav它不工作,因为没有 avformat_alloc_output_context2libav .也来源avconv.c source 对于像我这样的新手来说太复杂了,无法隔离与流复制操作相关的部分。
有人可以给我举个例子:
- 打开 输入文件1 输入文件2 (仅当程序与通用教程中的标准不同时才需要)
- 打开并写入 的 header 输出文件 具有相同的容器格式和相同的视频和音频格式
- 从 写入数据包输入文件1 输出文件 高达数据包,例如 pos == XXX- 从 写入数据包输入文件2 输出文件 用常数值改变他们的 dts(或任何需要的东西)
- 写正确trailer
计算我之前制作的 dts 中的时间偏移。

最佳答案

好的,我找到了那段代码,但正如我在评论中所说的那样,由于某种原因它不起作用。让我知道你能不能修好它

/*
 * merge.c
 *
 *  Created on: Nov 17, 2012
 *      Author: arash
 */

/* merge multiple "IDENTICAL" video file into one file */

#include <stdio.h>

#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"

AVFormatContext *i_fmt_ctx;
AVStream *i_video_stream;

AVFormatContext *o_fmt_ctx;
AVStream *o_video_stream;


int main(int argc, char* argv[])
{
    if (argc < 3)
    {
        fprintf(stderr, "usage : %s <input> [<input>...] <output>\n", argv[0]);
        return -1;
    }

    avcodec_register_all();
    av_register_all();

    /* should set to NULL so that avformat_open_input() allocate a new one */
    i_fmt_ctx = NULL;
    if (avformat_open_input(&i_fmt_ctx, argv[1], NULL, NULL)!=0)
    {
        fprintf(stderr, "could not open input file\n");
        return -1;
    }

    if (av_find_stream_info(i_fmt_ctx)<0)
    {
        fprintf(stderr, "could not find stream info\n");
        return -1;
    }

    //av_dump_format(i_fmt_ctx, 0, argv[1], 0);

    /* find first video stream */
    for (unsigned i=0; i<i_fmt_ctx->nb_streams; i++)
        if (i_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
        {
            i_video_stream = i_fmt_ctx->streams[i];
            break;
        }
    if (i_video_stream == NULL)
    {
        fprintf(stderr, "didn't find any video stream\n");
        return -1;
    }

    avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, argv[argc-1]);

    /*
     * since all input files are supposed to be identical (framerate, dimension, color format, ...)
     * we can safely set output codec values from first input file
     */
    o_video_stream = av_new_stream(o_fmt_ctx, 0);
    {
        AVCodecContext *c;
        c = o_video_stream->codec;
        c->bit_rate = 400000;
        c->codec_id = i_video_stream->codec->codec_id;
        c->codec_type = i_video_stream->codec->codec_type;
        c->time_base.num = i_video_stream->time_base.num;
        c->time_base.den = i_video_stream->time_base.den;
        fprintf(stderr, "time_base.num = %d time_base.den = %d\n", c->time_base.num, c->time_base.den);
        c->width = i_video_stream->codec->width;
        c->height = i_video_stream->codec->height;
        c->pix_fmt = i_video_stream->codec->pix_fmt;
        printf("%d %d %d", c->width, c->height, c->pix_fmt);
        c->flags = i_video_stream->codec->flags;
        c->flags |= CODEC_FLAG_GLOBAL_HEADER;
        c->me_range = i_video_stream->codec->me_range;
        c->max_qdiff = i_video_stream->codec->max_qdiff;

        c->qmin = i_video_stream->codec->qmin;
        c->qmax = i_video_stream->codec->qmax;

        c->qcompress = i_video_stream->codec->qcompress;
    }

    avio_open(&o_fmt_ctx->pb, argv[argc-1], AVIO_FLAG_WRITE);

    /* yes! this is redundant */
    av_close_input_file(i_fmt_ctx);

    avformat_write_header(o_fmt_ctx, NULL);

    int last_pts = 0;
    int last_dts = 0;
    for (int i=1; i<argc-1; i++)
    {
        i_fmt_ctx = NULL;
        if (avformat_open_input(&i_fmt_ctx, argv[i], NULL, NULL)!=0)
        {
            fprintf(stderr, "could not open input file\n");
            return -1;
        }

        if (av_find_stream_info(i_fmt_ctx)<0)
        {
            fprintf(stderr, "could not find stream info\n");
            return -1;
        }
        av_dump_format(i_fmt_ctx, 0, argv[i], 0);

        /* we only use first video stream of each input file */
        i_video_stream = NULL;
        for (unsigned s=0; s<i_fmt_ctx->nb_streams; s++)
            if (i_fmt_ctx->streams[s]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
            {
                i_video_stream = i_fmt_ctx->streams[s];
                break;
            }

        if (i_video_stream == NULL)
        {
            fprintf(stderr, "didn't find any video stream\n");
            return -1;
        }

        int64_t pts, dts;
        while (1)
        {
            AVPacket i_pkt;
            av_init_packet(&i_pkt);
            i_pkt.size = 0;
            i_pkt.data = NULL;
            if (av_read_frame(i_fmt_ctx, &i_pkt) <0 )
                break;
            /*
             * pts and dts should increase monotonically
             * pts should be >= dts
             */
            i_pkt.flags |= AV_PKT_FLAG_KEY;
            pts = i_pkt.pts;
            i_pkt.pts += last_pts;
            dts = i_pkt.dts;
            i_pkt.dts += last_dts;
            i_pkt.stream_index = 0;

            //printf("%lld %lld\n", i_pkt.pts, i_pkt.dts);
            static int num = 1;
            printf("frame %d\n", num++);
            av_interleaved_write_frame(o_fmt_ctx, &i_pkt);
            //av_free_packet(&i_pkt);
            //av_init_packet(&i_pkt);
        }
        last_dts += dts;
        last_pts += pts;

        av_close_input_file(i_fmt_ctx);
    }

    av_write_trailer(o_fmt_ctx);

    avcodec_close(o_fmt_ctx->streams[0]->codec);
    av_freep(&o_fmt_ctx->streams[0]->codec);
    av_freep(&o_fmt_ctx->streams[0]);

    avio_close(o_fmt_ctx->pb);
    av_free(o_fmt_ctx);

    return 0;
}

关于ffmpeg - 如何使用 libavformat 连接 2 个具有相同编解码器的视频文件(重新混合)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16282600/

相关文章:

c++ - 存在对 ffmpeg libav 的调用时静默崩溃

ffmpeg - 如何在终端中使用 ffprobe?

function - 编写一个函数来运行一系列命令,zsh

python - 我可以在 OpenCV 中将 ffmpeg 的 "stream copy"与 VideoWriter 类一起使用吗?

FFMPEG 找不到 H264 编码器

ios - iOS 上通过 RTMP 的 H264 视频流

c++ - FFMPEG RTSP 使用 libx264 流到 MPEG4/H264 文件

c++ - FFMPEG:av_rescale_q - time_base 差异

ffmpeg - 如何从 ffmpeg 中的流中检索 HTTP header ?

ffmpeg - 每秒帧数 概念细节