ffmpeg进行转封装

#if 0
//2018-12-19 ffmpeg进行转封装不进行转码
//ffmpeg库4.0.2

#include
#include
extern "C"
{
#include 
#include 
}
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"swscale.lib")
using namespace std;
int Open_In_fine(const char*  infile,int& videoidx,int& audioidx, AVFormatContext* &ic)
{
	avformat_open_input(&ic, infile, 0, 0);
	if (!ic)
	{
		//cout << "avformat_open_input failed!" << endl;
		return -1;
	}
	for (int i = 0; i < ic->nb_streams; i++)
	{
		if (ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
			videoidx = i;
		if (ic->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
			audioidx = i;
	}
	if (videoidx == -1)
	{
		//cout << "Codec find failed!" << endl;
		return -1;
	}
	av_dump_format(ic, 0, infile, 0);
	return 0;
}

int Open_out_put_file(const char*  outfile, int& videoidx, int& audioidx, AVStream *videoStream, AVStream *audioStream , AVFormatContext* &ic, AVFormatContext* &oc)
{
	avformat_alloc_output_context2(&oc, NULL, NULL, outfile);
	if (!oc)
	{
		//cerr << "avformat_alloc_output_context2 " << outfile << " failed!" << endl;
		return -1;
	}
	if (videoidx != -1) {
		videoStream = avformat_new_stream(oc, NULL);
		avcodec_parameters_copy(videoStream->codecpar, ic->streams[videoidx]->codecpar);
		videoStream->codecpar->codec_tag = 0;
	}
	if (audioidx != -1) {
		audioStream = avformat_new_stream(oc, NULL);
		avcodec_parameters_copy(audioStream->codecpar, ic->streams[audioidx]->codecpar);
		audioStream->codecpar->codec_tag = 0;
	}
	
	
	//oc->streams[videoidx]->codec->flags |= AV_CODEC_FLAG2_LOCAL_HEADER;

	//cout << "================================================" << endl;
	av_dump_format(oc, 0, outfile, 1);
	int ret = avio_open(&oc->pb, outfile, AVIO_FLAG_WRITE);
	if (ret < 0)
	{
		//cerr << "avio open failed!" << endl;
		return -1;
	}
	ret = avformat_write_header(oc, NULL);
	if (ret < 0)
	{
		//cerr << "avformat_write_header failed!" << endl;
		//getchar();
	}
	return 0;
}
void Time_base(AVPacket * pkt, AVFormatContext* &ic, AVFormatContext* &oc)
{
	pkt->pts = av_rescale_q_rnd(pkt->pts,
		ic->streams[pkt->stream_index]->time_base,
		oc->streams[pkt->stream_index]->time_base,
		(AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
	);
	pkt->dts = av_rescale_q_rnd(pkt->dts,
		ic->streams[pkt->stream_index]->time_base,
		oc->streams[pkt->stream_index]->time_base,
		(AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
	);
	pkt->pos = -1;
	pkt->duration = av_rescale_q_rnd(pkt->duration,
		ic->streams[pkt->stream_index]->time_base,
		oc->streams[pkt->stream_index]->time_base,
		(AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)
	);
}

void write_url_file(AVFormatContext *ic, AVFormatContext *oc, int& videoidx, int& audioidx) {
	AVPacket *pkt = av_packet_alloc();
	while ((av_read_frame(ic,pkt)>=0) ){
		Time_base(pkt,ic,oc);
		if (pkt->stream_index== videoidx) {
			av_write_frame(oc,pkt);
			cout << "+";
			continue;
		}
		else if(pkt->stream_index == audioidx)
		{
			av_write_frame(oc, pkt);
			cout << "-";
			continue;
		}
		av_packet_unref(pkt);
		av_packet_free(&pkt);	
	}
	av_write_trailer(oc);
	return;
}

void close_ffmpeg(AVFormatContext * &ic, AVFormatContext * oc)
{
	avformat_close_input(&ic);
	avformat_free_context(oc);
}

int main() {
	char path[] = "11.mp4";
	char outfile[] = "Test.mp4";
	int videoidx = -1;
	int audioidx = -1;
	AVFormatContext *ic = nullptr;

	AVFormatContext *oc = nullptr;
	AVStream *videoStream = nullptr;
	AVStream *audioStream = nullptr;

	Open_In_fine(path,videoidx,audioidx,ic);
	Open_out_put_file(outfile, videoidx, audioidx, videoStream, audioStream, ic, oc);
	
	write_url_file(ic,oc,videoidx,audioidx);
	

	close_ffmpeg(ic, oc);
	cin.get();
	return 0;
}
#endif // 0

你可能感兴趣的:(ffmpeg)