码迷,mamicode.com
首页 > 其他好文 > 详细

ffmpeg 自定义数据来源, 可以是文件,可以是内存,可以是网络, 爱咋的咋的

时间:2015-08-06 13:40:35      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

// 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 自定义数据来源, 可以是文件,可以是内存,可以是网络, 爱咋的咋的

标签:

原文地址:http://my.oschina.net/mingyuejingque/blog/488572

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!