1、前言
本文不涉及音频和视频的编解码操作,仅仅是转换视频的封装格式,比如mp4转flv,mp4转mkv等,也可以认为仅仅是为文件重命名,更改了文件后缀而已。
由于不涉及音视频的转码操作,因此此程序处理速度极快。需要知道的是,音视频编解码的算法非常复杂,占用了很多的CPU,这部分也消耗了是视频转码的绝大部分时间。关于视频转码的API使用方法将在之后的博客中详细介绍,本文只关注于转封装。
只转封装的工作原理如下图所示:
2、转封装流程
3、代码
- 类头文件代码(remuxing.h)
// Copyright (c) 2021 LucasNan <nanche@mail.hfut.edu.cn> <www.kevinnan.org.cn>
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#ifndef REMUXING_H
#define REMUXING_H
#include <iostream>
#include <string>
extern "C"{
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
}
class ReMuxing
{
public:
ReMuxing();
~ReMuxing();
//@brief: 得到输入文件
//@param: input_file: 输入文件
//@ret : void
//@birth: created by LucasNan on 20210220
void getInputFile(std::string input_file){
this->input_file_ = input_file;
}
//@brief: 得到输出文件
//@param: output_file: 输出文件
//@ret : void
//@birth: created by LucasNan on 20210220
void getOutputFile(std::string output_file){
this->output_file_ = output_file;
}
//@brief: 视频转封装
//@param: void
//@ret : void
//@birth: created by LucasNan on 20210220
void reMuxing();
private:
//输出文件AVFormatContext
AVFormatContext* ifmt_ctx_;
//输出文件AVFormatContext
AVFormatContext* ofmt_ctx_;
std::string input_file_;
std::string output_file_;
};
#endif // REMUXING_H
- 类实现代码(remuxing.cpp)
// Copyright (c) 2021 LucasNan <nanche@mail.hfut.edu.cn> <www.kevinnan.org.cn>
// Permission is hereby granted, free of char