1,流程图
2,代码
#include <iostream>
using namespace std;
extern "C"
{
#include <libavformat/avformat.h>
}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avutil.lib")
int main()
{
av_register_all();
AVFormatContext *ic = nullptr;
const char* iPath = "./file/test.mp4";
const char* oPath = "./file/testOut.wmv";
avformat_open_input(&ic, iPath, NULL, NULL);
if (!ic)
{
cout << "avformat_open_input" << endl;
return -1;
}
cout << "1,打开文件成功" << endl;
AVFormatContext *oc = nullptr;
avformat_alloc_output_context2(&oc, NULL, NULL,oPath);
if (!oc)
{
cout << "avformat_alloc_output_context2" << endl;
return -1;
}
cout << "2,输出文件成功" << endl;
AVStream *videoStream = avformat_new_stream(oc, NULL);
AVStream *audioStream = avformat_new_stream(oc, NULL);
avcodec_parameters_copy(videoStream->codecpar, ic->streams[0]->codecpar);
avcodec_parameters_copy(audioStream->codecpar, ic->streams[1]->codecpar);
videoStream->codecpar->codec_tag = 0;
audioStream->codecpar->codec_tag = 0;
cout << "-----------------------------------------------------------------------------" << endl;
av_dump_format(ic, 0, iPath, 0);
cout << "-------------------------------------------------------------------------------" << endl;
av_dump_format(oc, 0, oPath, 1);
int ret = avio_open(&oc->pb, oPath, AVIO_FLAG_WRITE);
if (ret)
{
cerr << "avio_open" << endl;
getchar();
return -1;
}
ret = avformat_write_header(oc,NULL);
if (ret)
{
cerr << "avformat_write_header" << endl;
getchar();
return -1;
}
AVPacket pkt;
for (;;)
{
int re = av_read_frame(ic, &pkt);
if (re)
break;
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));
av_write_frame(oc, &pkt);
av_packet_unref(&pkt);
int k = 0;
cout << k++ << endl;
}
av_write_trailer(oc);
avio_close(oc->pb);
cout << "------------endl_________________" << endl;
getchar();
return 0;
}