c++调用ffmpeg

时间:2022-05-04 08:32:10

在自己编译好ffmpeg库后,已经迫不及待的想尝试用vs2010来调用ffmpeg,在开始调用的时候遇到了些问题,但还是解决了。

配置vs

1.右键工程-属性,在然后选择 配置属性 -> C/C++ -> 常规 -> 附加包含目录,添加编译好的头文件;

2. 设置ffmpeg的lib文件位置 
鼠标右键点击工程名,选择属性, 然后选择 配置属性 -> 链接器 -> 常规 -> 附加库目录,添加编译好的lib目录。
3. 设置ffmpeg的所引用的lib文件  鼠标右键点击工程名,选择属性, 然后选择 配置属性 -> 链接器 -> 输入 -> 附加依赖项,添加编译好的lib文件
然后把编译好的dll文件拷贝到debug文件下
另外,C99中添加了几个新的头文件,Visual Studio中没有,所以需要你自己下载。并放至相应目录。对于VS2010来说通常是:C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include。
代码如下:
注意:编译成功后,F5遇到avformat_open_input报错,可能是因为第一个参数没有初始化。
// 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的更多相关文章

  1. bash shell,调用ffmpeg定期截图

    #!/bin/bash #获取当前目录中所有m3u8文件,并 var=$(ls |grep '.m3u8'|cut -d '.' -f1) #死循环 = ] do #循环每个文件 for stream ...

  2. 在visual studio 2010中调用ffmpeg

    转自:http://blog.sina.com.cn/s/blog_4178f4bf01018wqh.html 最近几天一直在折腾ffmpeg,在网上也查了许多资料,费了不少劲,现在在这里和大家分享一 ...

  3. NET 2&period;0&lpar;C&num;&rpar;调用ffmpeg处理视频的方法

    另外:ffmpeg的net封装库 http://www.intuitive.sk/fflib/ NET 2.0 调用FFMPEG,并异步读取输出信息的代码...public void ConvertV ...

  4. Java调用FFmpeg进行视频处理及Builder设计模式的应用

    1.FFmpeg是什么 FFmpeg(https://www.ffmpeg.org)是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.它用来干吗呢?视频采集.视频格式转化.视频 ...

  5. PHP 调用ffmpeg

    PHP 调用ffmpeg linux ffmpeg安装,tar文件安装一直出错,一直无语 php-ffmpeg安装, tar文件安装也一直出错,一直无语 最后直接在系统上安装ffmpeg sudo a ...

  6. ASP&period;NET下调用ffmpeg与mencoder实现视频转换截屏

    最近要做一个视频播放的系统,用到了ffmpeg和mencoder两个工具,查了一些资料,发现这方面的资料还挺多的,但是就是乱了一点,我自己从头整理了一下,和大家分享一下: 1.ffmpeg实现视频(a ...

  7. javaCV入门指南:调用FFmpeg原生API和JavaCV是如何封装了FFmpeg的音视频操作?

    通过"javaCV入门指南:序章 "大家知道了处理音视频流媒体的前置基本知识,基本知识包含了像素格式.编解码格式.封装格式.网络协议以及一些音视频专业名词,专业名词不会赘述,自行搜 ...

  8. c&num;调用ffmpeg嵌入srt&sol;ass字幕提示Cannot load default config file&period;&period;&period;

    c#调用ffmpeg嵌入srt/ass字幕提示 Fontconfig error: Cannot load default config file[Parsed_subtitles_0 @ 00000 ...

  9. c&num;调用ffmpeg嵌入srt&sol;ass字幕提示Unable to open xxx&period;srt&period;&period;&period;&period;&period;&period;

    最近接触到c#调用ffmpeg嵌入srt/ass字幕,碰到一个错误困扰了很久 Unable to open xxx.srt Error initializing filter 'subtitles' ...

随机推荐

  1. oracle sql优化

    整理一下网上所看到sql优化方法 1.使用大写字母书写sql,因为oracle解释器会先将sql语句转换成大写后再解释 2    减少访问数据库的次数,多数情况下一条sql可以达到目的的,就不要使用多 ...

  2. java web面试题,收集

    java面试题: http://www.codeceo.com/article/java-interview-question.html(很多题都很废) http://www.php100.com/h ...

  3. strcpy之代码的健壮性与可维护性

    strcpy   函数的原型是: char * strcpy(char * strDest,const char * strSrc);    功能:把从strSrc地址开始且含有NULL结束符的字符串 ...

  4. c&sol;c&plus;&plus;字符串处理大集合

    rember this strncpy(a,b,5); a[5]='\0'; char a[10]; memset(a,'#',sizeof(a)); a[10]='\0'; 刚开始学C/C++时,一 ...

  5. linux命令行计算器 &lt&semi;转&gt&semi;

    转自 http://blog.chinaunix.net/uid-26959241-id-3207711.html 详细文档请 man bc 在windows下,大家都知道直接运行calc,(c:\w ...

  6. solr7&period;4 tomcat环境下搭建(windows)

    -版本solr-7.4.0 -环境 Windows  jdk1.8 -启动方式:部署在apache-tomcat-8.5.28,以下简称Tomcat 1. 将solr-7.4.0\server\sol ...

  7. Linux 操作系统文件略解

    1.使用tree命令查看根目录的树结构 # tree -L 1 如果没有tree命令,可以使用yum进行安装 # yum -y install tree 执行命令后,即可看到根下一共有19个目录 . ...

  8. systemd 之 journalctl

    Systemd 日志系统 一.前言 昨天写了一篇文章,内容为:Systemd 常规操作与彩蛋,参考了 ArchLinux 官方文档并结合培训中的思路进行了部分修改补充.如果你懂得了基础的管理,那必然还 ...

  9. NYOJ----蛇形填数

    蛇形填数 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为: 10 11 12 1 9 16 ...

  10. 在阿里云服务器中安装配置mysql数据库完整教程

    阿里云ECS服务器CentOS7上安装MySql服务 (可选)1.确保服务器系统处于最新状态 [root@localhost ~]# yum -y update如果显示以下内容说明已经更新完成 Rep ...