1.读取本地视频流
// An highlighted block
#include<iostream>
using namespace std;
extern "C"
{
#include<libavcodec\avcodec.h>
#include<libavformat\avformat.h>
}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")
int main()
{
printf("%s\n", avcodec_configuration());
av_register_all();
AVFormatContext *pFormat = NULL;
const char* path = "D:/vswork/ffmpeg/work/11.mp4";
int ret=avformat_open_input(&pFormat,path,NULL,NULL); //该函数用于打开多媒体数据并且获取一些信息
//int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);
if (ret)
{
printf("avformat_open_input is faild\n");
return -1;
}
printf("avformat_open_input succeed\n");
ret=avformat_find_stream_info(pFormat,NULL); //它可以从读取到的包中获得到流的信息
//int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
if (ret)
{
printf("avformat_find_stream_info is faild\n");
return -1;
}
int time = pFormat->duration;
int mbittime = (time / 1000000)/60;
int mmintime = (time / 1000000) % 60;
printf("%d分%d秒\n", mbittime, mmintime);
av_dump_format(pFormat, NULL, path, 0); //最后一个参数 0:打印输入流 1:打印输出流
//void av_dump_format(AVFormatContext *ic,int index,const char *url,int is_output);
system("pause");
return 0;
}
2.读取网络视频流
// An highlighted block
#include<iostream>
using namespace std;
extern "C"
{
#include<libavcodec\avcodec.h>
#include<libavformat\avformat.h>
#include<libavutil\opt.h>
}
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
int main()
{
//测试动态链接库是否成功
printf("%s\n", avcodec_configuration());
//注册dll
av_register_all();
//网络
avformat_network_init();
AVFormatContext *pFormat = NULL;
//const char* path = "D:/vswork/ffmpeg/work/11.mp4";
//http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8
const char *path = "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8";
AVDictionary* opt = NULL;
av_dict_set(&opt,"rtsp_transport","tcp",0);
av_dict_set(&opt, "max_delay", "550", 0);
int ret=avformat_open_input(&pFormat,path,NULL,&opt); //该函数用于打开多媒体数据并且获取一些信息
//int avformat_open_input(AVFormatContext **ps, const char *url, AVInputFormat *fmt, AVDictionary **options);
if (ret)
{
printf("avformat_open_input is faild\n");
return -1;
}
printf("avformat_open_input succeed\n");
ret=avformat_find_stream_info(pFormat,NULL); //它可以从读取到的包中获得到流的信息
//int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options);
if (ret)
{
printf("avformat_find_stream_info is faild\n");
return -1;
}
int time = pFormat->duration;
int mbittime = (time / 1000000)/60;
int mmintime = (time / 1000000) % 60;
printf("%d分%d秒\n", mbittime, mmintime);
av_dump_format(pFormat, NULL, path, 0); //最后一个参数 0:打印输入流 1:打印输出流
//void av_dump_format(AVFormatContext *ic,int index,const char *url,int is_output);
system("pause");
return 0;
}