【开源项目】基于FFmpeg的封装格式转换
生活随笔
收集整理的這篇文章主要介紹了
【开源项目】基于FFmpeg的封装格式转换
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*
* 一笑奈何
* 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";//初始化封裝庫(kù)av_register_all();//初始化網(wǎng)絡(luò)庫(kù) (可以打開rtsp rtmp http 協(xié)議的流媒體視頻)avformat_network_init();//注冊(cè)解碼器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;//第二步,創(chuàng)建輸出文件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;//創(chuàng)建一個(gè)新的流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轉(zhuǎn)輸出time_base;關(guān)于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;//經(jīng)過(guò)多長(zhǎng)時(shí)間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);//主動(dòng)關(guān)閉后才可以把緩沖區(qū)的內(nèi)容寫到文件avio_close(oc->pb);cout << "==============end===============" << endl;return 0;
}
本文配套項(xiàng)目
總結(jié)
以上是生活随笔為你收集整理的【开源项目】基于FFmpeg的封装格式转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 个中翘楚什么意思 个中翘楚是何意思
- 下一篇: 【开源项目】基于FFmpeg的RGB格式