c++调用ffmpeg
在自己编译好ffmpeg库后,已经迫不及待的想尝试用vs2010来调用ffmpeg,在开始调用的时候遇到了些问题,但还是解决了。
配置vs
1.右键工程-属性,在然后选择 配置属性 -> C/C++ -> 常规 -> 附加包含目录,添加编译好的头文件;
// FFmpegOpenFile.cpp : 定义控制台应用程序的入口点。
//
// ffmpeg-example.cpp : Defines the entry point for the console application.
//
#include "stdafx.h" #define inline _inline
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif #ifdef __cplusplus
extern "C" {
#endif #include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endif #include <stdio.h> static void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame); int main (int argc, const char * argv[])
{
AVFormatContext *pFormatCtx = NULL;
int i, videoStream;
AVCodecContext *pCodecCtx;
AVCodec *pCodec;
AVFrame *pFrame;
AVFrame *pFrameRGB;
AVPacket packet;
int frameFinished;
int numBytes;
uint8_t *buffer; // Register all formats and codecs
av_register_all();
const char* filename = "F:\\开发笔记实例\\C++\\FFmpegOpenFile\\Wildlife.wmv";
// Open video file
//if(avformat_open_input(&pFormatCtx, argv[1], NULL, NULL)!=0)
int ii = avformat_open_input(&pFormatCtx, filename, NULL, NULL);
if(ii!=)
return -; // Couldn't open file // Retrieve stream information
if(av_find_stream_info(pFormatCtx)<)
return -; // Couldn't find stream information // Dump information about file onto standard error
av_dump_format(pFormatCtx, , argv[], false); // Find the first video stream
videoStream=-;
for(i=; i<pFormatCtx->nb_streams; i++)
if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
{
videoStream=i;
break;
}
if(videoStream==-)
return -; // Didn't find a video stream // Get a pointer to the codec context for the video stream
pCodecCtx=pFormatCtx->streams[videoStream]->codec; // Find the decoder for the video stream
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL)
return -; // Codec not found // Open codec
if(avcodec_open(pCodecCtx, pCodec)<)
return -; // Could not open codec // Hack to correct wrong frame rates that seem to be generated by some codecs
if(pCodecCtx->time_base.num> && pCodecCtx->time_base.den==)
pCodecCtx->time_base.den=; // Allocate video frame
pFrame=avcodec_alloc_frame(); // Allocate an AVFrame structure
pFrameRGB=avcodec_alloc_frame();
if(pFrameRGB==NULL)
return -; // Determine required buffer size and allocate buffer
numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
pCodecCtx->height); //buffer=malloc(numBytes);
buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t)); // Assign appropriate parts of buffer to image planes in pFrameRGB
avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
pCodecCtx->width, pCodecCtx->height); // Read frames and save first five frames to disk
i=;
while(av_read_frame(pFormatCtx, &packet)>=)
{
// Is this a packet from the video stream?
if(packet.stream_index==videoStream)
{
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet); // Did we get a video frame?
if(frameFinished)
{
static struct SwsContext *img_convert_ctx; #if 0
// Older removed code
// Convert the image from its native format to RGB swscale
img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24,
(AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width,
pCodecCtx->height); // function template, for reference
int sws_scale(struct SwsContext *context, uint8_t* src[], int srcStride[], int srcSliceY,
int srcSliceH, uint8_t* dst[], int dstStride[]);
#endif
// Convert the image into YUV format that SDL uses
if(img_convert_ctx == NULL) {
int w = pCodecCtx->width;
int h = pCodecCtx->height; img_convert_ctx = sws_getContext(w, h,
pCodecCtx->pix_fmt,
w, h, PIX_FMT_RGB24, SWS_BICUBIC,
NULL, NULL, NULL);
if(img_convert_ctx == NULL) {
fprintf(stderr, "Cannot initialize the conversion context!\n");
exit();
}
}
int ret = sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, ,
pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
#if 0
// this use to be true, as of 1/2009, but apparently it is no longer true in 3/2009
if(ret) {
fprintf(stderr, "SWS_Scale failed [%d]!\n", ret);
exit(-);
}
#endif
// Save the frame to disk
if(i++<=)
SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i);
}
} // Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
} // Free the RGB image
//free(buffer);
av_free(buffer);
av_free(pFrameRGB); // Free the YUV frame
av_free(pFrame); // Close the codec
avcodec_close(pCodecCtx); // Close the video file
av_close_input_file(pFormatCtx); return ;
} static void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame)
{
FILE *pFile;
char szFilename[];
int y; // Open file
sprintf(szFilename, "frame%d.ppm", iFrame);
pFile=fopen(szFilename, "wb");
if(pFile==NULL)
return; // Write header
fprintf(pFile, "P6\n%d %d\n255\n", width, height); // Write pixel data
for(y=; y<height; y++)
fwrite(pFrame->data[]+y*pFrame->linesize[], , width*, pFile); // Close file
fclose(pFile);
}
c++调用ffmpeg的更多相关文章
- bash shell,调用ffmpeg定期截图
#!/bin/bash #获取当前目录中所有m3u8文件,并 var=$(ls |grep '.m3u8'|cut -d '.' -f1) #死循环 = ] do #循环每个文件 for stream ...
- 在visual studio 2010中调用ffmpeg
转自:http://blog.sina.com.cn/s/blog_4178f4bf01018wqh.html 最近几天一直在折腾ffmpeg,在网上也查了许多资料,费了不少劲,现在在这里和大家分享一 ...
- NET 2.0(C#)调用ffmpeg处理视频的方法
另外:ffmpeg的net封装库 http://www.intuitive.sk/fflib/ NET 2.0 调用FFMPEG,并异步读取输出信息的代码...public void ConvertV ...
- Java调用FFmpeg进行视频处理及Builder设计模式的应用
1.FFmpeg是什么 FFmpeg(https://www.ffmpeg.org)是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它用来干吗呢?视频采集.视频格式转化.视频 ...
- PHP 调用ffmpeg
PHP 调用ffmpeg linux ffmpeg安装,tar文件安装一直出错,一直无语 php-ffmpeg安装, tar文件安装也一直出错,一直无语 最后直接在系统上安装ffmpeg sudo a ...
- ASP.NET下调用ffmpeg与mencoder实现视频转换截屏
最近要做一个视频播放的系统,用到了ffmpeg和mencoder两个工具,查了一些资料,发现这方面的资料还挺多的,但是就是乱了一点,我自己从头整理了一下,和大家分享一下: 1.ffmpeg实现视频(a ...
- javaCV入门指南:调用FFmpeg原生API和JavaCV是如何封装了FFmpeg的音视频操作?
通过"javaCV入门指南:序章 "大家知道了处理音视频流媒体的前置基本知识,基本知识包含了像素格式.编解码格式.封装格式.网络协议以及一些音视频专业名词,专业名词不会赘述,自行搜 ...
- c#调用ffmpeg嵌入srt/ass字幕提示Cannot load default config file...
c#调用ffmpeg嵌入srt/ass字幕提示 Fontconfig error: Cannot load default config file[Parsed_subtitles_0 @ 00000 ...
- c#调用ffmpeg嵌入srt/ass字幕提示Unable to open xxx.srt......
最近接触到c#调用ffmpeg嵌入srt/ass字幕,碰到一个错误困扰了很久 Unable to open xxx.srt Error initializing filter 'subtitles' ...
随机推荐
- linux 进程综合指令
1. 查询当前机器运行的进程总数: ps -ef | wc -l ps -ef | grep httpd | wc -l 2. ulimit命令 表 1. ulimit 参数说明 选项 [option ...
- 关于tomcat的思考
下载文件两种方式:绿色版的.安装版的(找到jre的环境变量.配置或修改端口8080→8070) 启动完tomcat之后: 既可以虚拟目录打开(如http://localhost:8070/mldn/) ...
- C++数据结构和算法每天一练(线性表)
#include <iostream> using namespace std; class ArrayLinerTable { public: void InitLine ...
- Shell变量之自定义变量、环境变量
1:环境变量 环境变量可以帮我们达到很多功能-包括家目录的变换啊.提示字符的显示啊.运行文件搜寻的路径啊等等的那么,既然环境变量有那么多的功能,问一下,目前我的 shell 环境中, 有 ...
- Linux删除文件Argument list too long问题的解决方案
方法一:使用find find . -name 文件 | xargs rm -f 但文件数量过多,find命令也会出现问题: -bash: /bin/find: Argument list too l ...
- springmvc使用@ResponseBody返回json乱码解决方法
1.springmvc 3.2以上的版本解决乱码的方法: 第一步:在配置中加入: <mvc:annotation-driven> <mvc:message-converters re ...
- (转)网站隔几天打不开,多次重启Apache解决办法
网站打不开了重启一下apache又好了,但过二天又打不开了,只有重启一下才好,基本上二天重起一次,本文主要解决这个问题 工具/原料 linux服务器 网站 方法/步骤 注意观察cpu占用情况{:s ...
- Oracle 11g-R2 SQL Developer连接MSSQL2008
操作系统环境:WINDOWS8.1 工具: Oracle 11g-R2 SQL Developer 网络资源:http://sourceforge.net/project/showfiles.php ...
- 如何生成publish windows app 用到的 pfx 文件
参考文章 https://msdn.microsoft.com/en-us/library/windows/desktop/jj835832(v=vs.85).aspx 1.在项目中查找.appxma ...
- ACM YTU 《挑战编程》第一章 入门 Problem E: Graphical Editor
Description Graphical editors such as Photoshop allow us to alter bit-mapped images in the same way ...