ffmpeg 自定义数据来源, 可以是文件,可以是内存,可以是网络, 爱咋的咋的
2014-08-07 12:18
435 查看
ffmpeg 自定义数据来源, 可以是文件,可以是内存,可以是网络, 爱咋的咋的
// ffmpeg_custom_context.cpp : Defines the entry point for the console application.//
#include "stdafx.h"
extern "C"
{
#include <libavformat/avformat.h>
};
#pragma comment(lib, "avformat.lib")
int ReadFunc(void* ptr, uint8_t* buf, int buf_size)
{
FILE* fp = (FILE*)ptr;
size_t size = fread(buf, 1, buf_size, fp);
int ret = size;
return ret;
} //这个函数你可以自由发挥。一定要自由发挥。我没有网络流就从文件读了, 一般来说这里不能返回-1 或者0, 否则ffmpeg认为结束、或失败了。 你不想读文件,就直接从内存拷一段数据给buf即可。 接受的网络流、底层上采集卡上来的流都可以在这里,,,,
//一定要注意这个函数,ffmpeg的实现就是一个坑,一不小心就中招了。
int64_t seek_func(void *opaque, int64_t offset, int whence)
{
int64_t ret;
FILE *fp = (FILE*)opaque;
if (whence == AVSEEK_SIZE) {
return -1;
}
fseek(fp, offset, whence);
return ftell(fp);
}
int _tmain(int argc, _TCHAR* argv[])
{
av_register_all();
int ret = 0;
FILE* fp = fopen("./BoyGirls.mp4", "rb");
int nBufferSize = 1024 * 1024;
unsigned char* pBuffer = new unsigned char[nBufferSize];
AVFormatContext* pFormatCtx = NULL;
AVInputFormat *piFmt = NULL;
AVCodecContext *vcodecCtx = NULL, *acodecCtx = NULL;
AVCodec *vdec = NULL, *adec = NULL;
int ai = -1, vi = -1;
// Allocate the AVIOContext:
AVIOContext* pIOCtx = avio_alloc_context(pBuffer, nBufferSize,
0,
fp,
ReadFunc,
0,
seek_func);
//step2:探测流格式
ret = av_probe_input_buffer(pIOCtx, &piFmt, "", NULL, 0, 0);
if (ret < 0) {
fprintf(stderr, "probe failed!\n");
goto quit;
}
else {
fprintf(stdout, "probe success!\n");
fprintf(stdout, "format: %s[%s]\n", piFmt->name, piFmt->long_name);
}
// Allocate the AVFormatContext:
pFormatCtx = avformat_alloc_context();
// Set the IOContext:
pFormatCtx->pb = pIOCtx;
pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;
//step4:打开流
if (avformat_open_input(&pFormatCtx, "", piFmt, NULL) < 0) {
fprintf(stderr, "avformat open failed.\n");
goto quit;
}
else {
fprintf(stdout, "open stream success!\n");
}
if (avformat_find_stream_info(pFormatCtx, NULL)<0)
{
printf("av_find_stream_info error \n");
goto quit;
}
//接下来该做什么??? 喂,你要做什么?
quit:
avformat_close_input(&pFormatCtx);
delete[]pBuffer;
return 0;
}
本来已经有一篇了:http://hi.baidu.com/mingyuejingque/item/34db89a7d16fc9706cd4559d
可是新乞丐王子说版式不方便阅读,特别是在上面url的回帖我已经贴了代码,猥琐的百度默认没打开回帖,,,所以新贴了这篇方便大家。感谢祖国!
相关文章推荐
- ffmpeg 自定义数据来源
- ffmpeg 自定义数据来源
- proc文件系统中cpu,内存,网络数据使用情况获取(附检测网速源码)
- 数组和字典的writeToFile方法——在项目开发中处理网络数据的时候,可以把请求获得的网络数据保存为plist文件,这样更方便开发
- 什么是序列化?一句话:就是将java对象固化成文件存起来,这样就java对象就被固话了,可以任意的存储和网络传输了,而java对象是暂时存在内存里的,是没办法传输的,因为是虚拟的,并不是实实在在的文件
- 进程间通过内存映射文件共享数据
- 一个简单的xml类,可以解析和生成xml文件数据
- 如何在内存中自定义表用于添加和删除数据
- net控件中数据导到Excel的格式 首先,我们了解一下excel从web页面上导出的原理。当我们把这些数据发送到客户端时,我们想让客户端程序(浏览器)以excel的格式读取它,所以把mime类型设为:application/vnd.ms-excel,当excel读取文件时会以每个cell的格式呈现数据,如果cell没有规定的格式,则excel会以默认的格式去呈现该cell的数据。这样就给我们提供了自定义数据格式的空间,当然我们必须使用excel支持的格式。下面就列出常用的一些格式: 1) 文本
- 数据驱动在QTP中的运用(一) - 自定义TXT文件
- windows笔记-使用内存映射文件在进程之间共享数据
- 一个操作共享内存的类!可以方面的添加,删除数据,包括各种结构体!
- 单个文件上传类(可以自定义配置)
- ORACLE热备复制数据库全记录(可以修改数据库名和数据文件路径)
- 网络、磁盘、内存哪种方式更适合保存数据?
- 大文件内数据排序问题:采用文件映射内存技术
- 5G网络硬盘,100M文件大小,联想研究院发布联想社区化数据服务
- 有没有一种方法可以将文件和数据从硬盘完全清除
- jsp内存数据下载到本地--弹出的文件保存框时 两次调用action中下载方法问题
- 通过内存映射文件实现进程间数据共享