vlc 使用libvlc_video_set_callbacks 回调函数获取视频中帧的图片报错

aiyin434 2017-10-26 02:51:49

#include <Windows.h>
#include "vlc/vlc.h"
//#include <QWidget>
//#include <QtGui/QtGui>
#include <qmutex>
#include <sstream>
#include <qimage>
//#include<QApplication>
//#include<QPainter>

QMutex g_mutex;
//bool g_isInit = false;
int IMG_WIDTH = 640;
int IMG_HEIGHT = 480;
char out_buffer[640 * 480 * 4];
int frameNum = 0;

static void *lock(void *data, void **p_pixels)
{
try
{
g_mutex.lock();
*p_pixels = out_buffer; /*tell VLC to put decoded data to this buffer*/
return 0; /* picture identifier, not needed here */
}
catch (const std::exception&)
{

}
}

/*##get the argb picture AND save to file*/
static void unlock(void *data, void *id, void *const *p_pixels)
{
try
{
QImage image((unsigned char*)out_buffer, 640, 480, QImage::Format_ARGB32);
std::ostringstream oss;
oss << "d:/img"
<< frameNum
<< ".jpg";
frameNum++;
image.save(oss.str().c_str());
g_mutex.unlock();
}
catch (const std::exception&)
{

}
}

static void display(void *data, void *id)
{
/* do not display the video */
(void)data;
}

int main(int argc, char* argv[])
{
libvlc_instance_t * inst;
libvlc_media_player_t *mp;
libvlc_media_t *m;

libvlc_time_t length;
int width;
int height;
int wait_time=5000;
//libvlc_time_t length;

/* Load the VLC engine */
inst = libvlc_new (0, NULL);

//Create a new item
m = libvlc_media_new_path(inst, "shipin.mp4");



/* Create a media player playing environement */
mp = libvlc_media_player_new_from_media (m);


/* No need to keep the media now */
libvlc_media_release (m);


//取图片
libvlc_video_set_callbacks(mp, lock, unlock, display, 0);
libvlc_video_set_format(mp, "RGBA", IMG_WIDTH, IMG_HEIGHT, IMG_WIDTH * 4);


// play the media_player
libvlc_media_player_play (mp);



//wait until the tracks are created
_sleep (wait_time);
length = libvlc_media_player_get_length(mp);
width = libvlc_video_get_width(mp);
height = libvlc_video_get_height(mp);
printf("Stream Duration: %ds\n",length/1000);
printf("Resolution: %d x %d\n",width,height);

//Let it play
_sleep (length-wait_time);

// Stop playing
libvlc_media_player_stop (mp);

// Free the media_player
libvlc_media_player_release (mp);

libvlc_release (inst);

return 0;
}



...全文
1054 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-10-26
  • 打赏
  • 举报
回复
请检查每个函数调用的返回值。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧