FFmpeg 简单合成 MP4 :视频解封与重新封装

我们知道,将编码数据封装到 Mp4 中,需要知道音视频编码相关的参数,比如编码格式,视频的宽高,音频通道数,帧率,比特率等,下面就先看看如何初始化它们。

初始化封装参数

首先,定一个打包器 FFRepacker

// ff_repack.h

class FFRepack {
private:
    const char *TAG = "FFRepack";

    AVFormatContext *m_in_format_cxt;

    AVFormatContext *m_out_format_cxt;

    int OpenSrcFile(char *srcPath);

    int InitMuxerParams(char *destPath);

public:
    FFRepack(JNIEnv *env,jstring in_path, jstring out_path);
};

初始化过程分为两个步骤:打开原视频文件、初始化打包参数。

// ff_repack.cpp

FFRepack::FFRepack(JNIEnv *env, jstring in_path, jstring out_path) {
    const char *srcPath = env->GetStringUTFChars(in_path, NULL);
    const char *destPath = env->GetStringUTFChars(out_path, NULL);

    // 打开原视频文件,并获取相关参数
    if (OpenSrcFile(srcPath) >= 0) {
        // 初始化打包参数
        if (InitMuxerParams(destPath)) {
            LOGE(TAG, "Init muxer params fail")
        }
    } else {
        LOGE(TAG, "Open src file fail")
    }
}

打开原视频,获取原视频参数

代码很简单,在使用 FFMpeg 解码的文章中就已经讲解过。如下:

// ff_repack.cpp

int FFRepack::OpenSrcFile(const char *srcPath) {
    // 打开文件
    if ((avformat_open_input(&m_in_format_cxt, srcPath, 0, 0)) < 0) {
        LOGE(TAG, "Fail to open input file")
        return -1;
    }

    // 获取音视频参数
    if ((avformat_find_stream_info(m_in_format_cxt, 0)) < 0) {
        LOGE(TAG, "Fail to retrieve input stream information")
        return -1;
    }

    return 0;
}

初始化打包参数

初始化打包参数稍微复杂一些,主要过程是:

查找原视频中有哪些音视频流,并为目标视频(即重打包视频文件)添加对应的流通道和初始化对应的编码参数。

接着,使用已经初始化完毕的上下文,打开目标存储文件。

最后,往目标文件中,写入视频头部信息。

代码如下, 主要流程请查看注释:

//ff_repack.cpp

int FFRepack::InitMuxerParams(const char *destPath) {
    // 初始化输出上下文
    if (avformat_alloc_output_context2(&m_out_format_cxt, NULL, NULL, destPath) < 0) {
        return -1;
    }

    // 查找原视频所有媒体流
    for (int i = 0; i < m_in_format_cxt->nb_streams; ++i) {
        // 获取媒体流
        AVStream *in_stream = m_in_format_cxt->streams[i];

        // 为目标文件创建输出流
        AVStream *out_stream = avformat_new_stream(m_out_format_cxt, NULL);
        if (!out_stream) {
            LOGE(TAG, "Fail to allocate output stream")
            return -1;
        }

        // 复制原视频数据流参数到目标输出流
        if (avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar) < 0) {
            LOGE(TAG, "Fail to copy input context to output stream")
            return -1;
        }
    }

    // 打开目标文件
    if (avio_open(&m_out_format_cxt->pb, destPath, AVIO_FLAG_WRITE) < 0) {
        LOGE(TAG, "Could not open output file %s ", destPath);
        return -1;
    }

    // 写入文件头信息
    if (avformat_write_header(m_out_format_cxt, NULL) < 0) {
        LOGE(TAG, "Error occurred when opening output file");
        return -1;
    } else {
        LOGE(TAG, "Write file header success");
    }

    return 0;
}

以上,初始化已经完毕,下面就可以进行音视频的解封和重新封装了。

原视频解封装

新增一个 Start 方法,用于开启重打包

// ff_repack.h

class FFRepack {
    // 省略其他...

public:
    // 省略其他...
    
    void Start();
};

具体实现如下:

// ff_repack.cpp

void FFRepack::Start() {
    LOGE(TAG, "Start repacking ....")
    AVPacket pkt;
    while (1) {
        // 读取数据
        if (av_read_frame(m_in_format_cxt, &pkt)) {
            LOGE(TAG, "End of video,write trailer")

            // 释放数据帧
            av_packet_unref(&pkt);

            // 读取完毕,写入结尾信息
            av_write_trailer(m_out_format_cxt);

            break;
        }

        // 写入一帧数据
        Write(pkt);
    }

    // 释放资源
    Release();
}

解封依然很简单,在之前的解码文章同样介绍过,主要是将数据读取到 AVPacket 中。然后调用 Write 方法,将帧数据写入目标文件中。下面就来看看 Write 方法。

目标视频封装

增加一个 Write 方法。

// ff_repack.h

class FFRepack {
    // 省略其他...

public:
    // 省略其他...
    
    void Write(AVPacket pkt);
};
// ff_repacker.cpp

void FFRepack::Write(AVPacket pkt) {

    // 获取数据对应的输入/输出流
    AVStream *in_stream = m_in_format_cxt->streams[pkt.stream_index];
    AVStream *out_stream = m_out_format_cxt->streams[pkt.stream_index];

    // 转换时间基对应的 PTS/DTS
    int rounding = (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
    pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base,
                               (AVRounding)rounding);
    pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base,
                               (AVRounding)rounding);

    pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);

    pkt.pos = -1;

    // 将数据写入目标文件
    if (av_interleaved_write_frame(m_out_format_cxt, &pkt) < 0) {
        LOGE(TAG, "Error to muxing packet: %x", ret)
    }
}

流程很简单,将该帧数据的 pts 和 dts 、 duration 进行转换以后,将数据写入即可。

在写入数据之前,先获取了该帧数据所在的流和写入的数据流。这是因为,在写入之前,需要对数据的时间进行转换。

FFmpeg 中的时间单位

我们知道,每一帧音视频数据都有其对应的时间戳,根据这个时间戳就可以实现对音视频播放的控制。

FFmpeg 中的时间戳并不是我们实际中的时间,它是一个特别的数值。并且在 FFmpeg 中,还有一个叫 时间基 的概念,时间基 是 FFmpeg 中的时间单位。

[时间戳的值] 乘以 [时间基],才是[实际的时间],并且单位为秒。

换而言之,FFmpeg 的时间戳的值,是随着 时间基 的不同而变化的。

而 FFmpeg 在不同的阶段和不同的封装格式下也有着不同的时间基,因此,在进行帧数据的封装时,需要根据各自的时间基进行 “时间戳” 转换,以保证最终计算得到的实际时间是一致的。

当然了,为了方便转换 FFmpeg 为我们提供了转换的方法,并且处理了数据的溢出和取整问题。

av_rescale_q_rnd(int64_t a, AVRational bq,AVRational cq,enum AVRounding rnd)

其内部原理很简单:return (a × bq / cq)。

即:

x(目标时间戳值) * cq(目标时间基)= a(原时间戳值) * bq(原时间基)

=》=》=》=》=》=》

x = a * bq / cq

当所有数据帧都读取完毕之后,需要通过 av_write_trailer 写入结尾信息,这样文件才算完整,视频才能正常播放。

释放资源

最后,需要将之前打开的资源进行关闭,避免内存泄漏。

增加一个 Release 方法:

// ff_repack.h

class FFRepack {
    // 省略其他...

public:
    // 省略其他...
    
    void Release();
};
//ff_repack.cpp

void FFRepack::Release() {
    LOGE(TAG, "Finish repacking, release resources")
    // 关闭输入
    if (m_in_format_cxt) {
        avformat_close_input(&m_in_format_cxt);
    }

    // 关闭输出
    if (m_out_format_cxt) {
        avio_close(m_out_format_cxt->pb);
        avformat_free_context(m_out_format_cxt);
    }
}

调用重打包

新增 JNI 接口

在 native-lib.cpp 中,新增 JNI 接口

// native-lib.cpp

extern "C" {

    // 省略其他 ...
    
    JNIEXPORT jint JNICALL
    Java_com_cxp_learningvideo_FFRepackActivity_createRepack(JNIEnv *env,
                                                           jobject  /* this */,
                                                           jstring srcPath,
                                                           jstring destPath) {
        FFRepack *repack = new FFRepack(env, srcPath, destPath);
        return (jint) repack;
    }
    
    JNIEXPORT void JNICALL
    Java_com_cxp_learningvideo_FFRepackActivity_startRepack(JNIEnv *env,
                                                           jobject  /* this */,
                                                           jint repack) {
        FFRepack *ffRepack = (FFRepack *) repack;
        ffRepack->Start();
    }
}

新增页面

// FFRepackActivity.kt

class FFRepackActivity: AppCompatActivity() {

    private var ffRepack: Int = 0

    private val srcPath = Environment.getExternalStorageDirectory().absolutePath + "/mvtest.mp4"
    private val destPath = Environment.getExternalStorageDirectory().absolutePath + "/mvtest_repack.mp4"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_ff_repack)

        ffRepack = createRepack(srcPath, destPath)
    }

    fun onStartClick(view: View) {
        if (ffRepack != 0) {
            thread {
                startRepack(ffRepack)
            }
        }
    }

    private external fun createRepack(srcPath: String, destPath: String): Int

    private external fun startRepack(repack: Int)

    companion object {
        init {
            System.loadLibrary("native-lib")
        }
    }
}

以上,就是使用 FFmpeg 解封和封装的过程,比较简单,主要是为后面视频编辑、编码、封装做好准备。

本文来自作者投稿,版权归原作者所有。如需转载,请注明出处:https://www.nxrte.com/jishu/17944.html

(0)

相关推荐

发表回复

登录后才能评论