/*
* 一笑奈何
* cn-yixiaonaihe.blog.csdn.net
*/#include <iostream>
#include <thread>
extern "C" {
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
}
using namespace std;static double r2d(AVRational r)
{return r.den == 0 ? 0 : (double)r.num / (double)r.den;
}
void XSleep(int ms)
{//c++ 11chrono::milliseconds du(ms);this_thread::sleep_for(du);
}
int main(int argc, char *argv[])
{cout << "Test Demux FFmpeg.club" << endl;const char *url = "TWO.mp4";const char *outfile = "out.mov";//初始化封装库av_register_all();//初始化网络库 (可以打开rtsp rtmp http 协议的流媒体视频)avformat_network_init();//注册解码器avcodec_register_all();AVFormatContext *ic = nullptr;avformat_open_input(&ic,url,0,0);if (!ic){cout << "avformat_open_input is not!" << endl;return -1;}cout << "avformat_open_input is yes!" << endl;//第二步,创建输出文件AVFormatContext* oc = nullptr;avformat_alloc_output_context2(&oc,NULL,NULL,outfile);if (!ic){cout << "avformat_alloc_output_context2 is not!" << endl;return -1;}cout << "avformat_alloc_output_context2 is yes!" << 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;av_dump_format(ic,0, url,0);cout << "==============================" << endl;av_dump_format(oc,0,outfile,1);//写入文件头信息int ret = avio_open(&oc->pb,outfile,AVIO_FLAG_WRITE);//打开输出文件IOif (ret < 0){cout << "avio_open failed" << endl;getchar();return -1;}ret=avformat_write_header(oc,NULL);if (ret < 0){cout << "avformat_write_header failed" << endl;getchar();}AVPacket pkt;while (true){int re=av_read_frame(ic,&pkt);if (re < 0)break;//输入time_base转输出time_base;关于pts和dts的;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);cout << "." << endl;}av_write_trailer(oc);//主动关闭后才可以把缓冲区的内容写到文件avio_close(oc->pb);cout << "==============end===============" << endl;return 0;
}
本文配套项目