ffmpeg 自定义数据来源, 可以是文件,可以是内存,可以是网络, 爱咋的咋的
来源:互联网 发布:js里的点击事件 编辑:程序博客网 时间:2024/04/30 21:52
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 自定义数据来源, 可以是文件,可以是内存,可以是网络, 爱咋的咋的
- 希望是可以的,
- 数据还可以是这样的...
- 数据恢复原来是可以免费的!
- 真正爱一个人,是可以无私的付出
- 星星之火是可以燎原的
- 星星之火是可以燎原的
- 命运是可以改变的:
- 期望值是可以商量的
- 容貌是可以改变的
- 可以改变的是态度
- 卓有成效是可以学会的
- 学习是可以拿来主义的
- 优秀是可以锻炼的
- 成功是可以复制的
- 抽象方法是否可以是静态的,是否可以是native,是否可以是synchronized
- 相同的字符串常量是可以共享内存的
- Dojo 的网络引用,前提是可以连入 Internet
- eclispe升级adt后无ant视图解决
- mysql---用户管理
- STM32之位绑定
- apache虚拟主机配置详解
- HDU1325 Is It A Tree? 【并查集】
- ffmpeg 自定义数据来源, 可以是文件,可以是内存,可以是网络, 爱咋的咋的
- 第十七讲:对话框 Android Dialog
- FZU_Problem 2168 防守阵地 I
- (七)8天快速掌握Android视频教程_查看应用输出的错误信息与如何部署应用到真实手机
- 注册表重定向
- 此三层非彼三层——MVC&UBD
- 二叉树的镜像
- oracle 游标
- 网络通信的工作原理