2015-02-24 482 views
0

我想播放使用libVLC和OpenCV從IP攝像頭(H264-codec)獲得的視頻文件,因此我從this post獲取了代碼,然後在VS中創建項目2010,並將我的mp4文件放入項目文件夾(「5.mp4」)。當我開始它 - 我得到這個錯誤:使用libVLC打開mp4並在OpenCV中播放C++

main error: open of `5.mp4' failed

main error: Your input can't be opened

main error: VLC is unable to open the MRL '5.mp4'. Check the log for details.

這裏是我的代碼:

#include <vlc/vlc.h> 
#include <opencv2/opencv.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <stdio.h> 

using namespace cv; 
using namespace std; 

struct VideoDataStruct { 
    int param; 
}; 

int done = 0; 
libvlc_media_player_t *mp; 
unsigned int videoBufferSize = 0; 
uint8_t *videoBuffer = 0; 

void cbVideoPrerender(void *p_video_data, uint8_t **pp_pixel_buffer, int size) { 
// Locking 
    if (size > videoBufferSize || !videoBuffer) 
    { 
    printf("Reallocate raw video buffer\n"); 
    free(videoBuffer); 
    videoBuffer = (uint8_t *) malloc(size); 
    videoBufferSize = size; 
    } 

    *pp_pixel_buffer = videoBuffer; 
} 

void cbVideoPostrender(void *p_video_data, uint8_t *p_pixel_buffer, int width, int height, int pixel_pitch, int size, int64_t pts) { 
    // Unlocking 
    //CloseHandle(hMutex); 
} 

static void handleEvent(const libvlc_event_t* pEvt, void* pUserData) { 
    libvlc_time_t time; 
    switch(pEvt->type) 
    { 
     case libvlc_MediaPlayerTimeChanged: 
      time = libvlc_media_player_get_time(mp); 
      printf("MediaPlayerTimeChanged %lld ms\n", (long long)time); 
      break; 
     case libvlc_MediaPlayerEndReached: 
      printf ("MediaPlayerEndReached\n"); 
      done = 1; 
     break; 
     default: 
      printf("%s\n", libvlc_event_type_name(pEvt->type)); 
    } 
} 

int main(int argc, char* argv[]) { 

// VLC pointers 
libvlc_instance_t *inst; 
libvlc_media_t *m; 
void *pUserData = 0; 
VideoDataStruct dataStruct; 

// VLC options 
char smem_options[1000]; 
// RV24 
sprintf(smem_options 
    , "#transcode{vcodec=RV24}:smem{" 
    "video-prerender-callback=%lld," 
    "video-postrender-callback=%lld," 
    "video-data=%lld," 
    "no-time-sync}," 
    , (long long int)(intptr_t)(void*)&cbVideoPrerender 
    , (long long int)(intptr_t)(void*)&cbVideoPostrender 
    , (long long int)(intptr_t)(void*)&dataStruct 
); 

const char * const vlc_args[] = { 
      "-I", "dummy",   // Don't use any interface 
      "--ignore-config",  // Don't use VLC's config 
      "--extraintf=logger",  // Log anything 
      "--verbose=1",   // Be verbose 
      "--sout", smem_options // Stream to memory 
      }; 

// We launch VLC 
inst = libvlc_new(sizeof(vlc_args)/sizeof(vlc_args[0]), vlc_args); 

/* Create a new item */ 
m = libvlc_media_new_location(inst, "5.mp4"); 

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

libvlc_event_manager_t* eventManager = libvlc_media_player_event_manager(mp); 
libvlc_event_attach(eventManager, libvlc_MediaPlayerTimeChanged, handleEvent, pUserData); 
libvlc_event_attach(eventManager, libvlc_MediaPlayerEndReached, handleEvent, pUserData); 
libvlc_event_attach(eventManager, libvlc_MediaPlayerPositionChanged, handleEvent, pUserData); 

libvlc_video_set_format(mp, "RV24", 1280, 720, 1280* 3); 

/* play the media_player */ 
libvlc_media_player_play (mp); 

while(1) 
{ 
    if(videoBuffer)   // Check for invalid input 
    { 
     // CV_8UC3 = 8 bits, 3 chanels 
     Mat img = Mat(Size(1280, 720), CV_8UC3, videoBuffer); 
     // cvtColor(img, img, CV_RGB2BGR); 
     namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display. 
     imshow("Display window", img);  // Show our image inside it. 
    } 
} 
libvlc_release (inst); 
} 

我猜,這很容易解決,但我便無法找到在互聯網的任何信息。我也嘗試把它放到「C:\ 5.mp4」中,但我得到了同樣的錯誤。謝謝你的幫助。

編輯: 好了,我修復了這個問題,我需要把文件:///「5.mp4」之前,現在我的視頻播放,但它看起來像這樣: enter image description here

EDIT02 好了,用「* .AVI」一切看起來都不錯,所以我想這個文件的問題 - 我記錄了它FROP IP攝像頭,使用VLC,並保存成* .MP4

+0

你試過類似'cvtColor(img,img,CV_YUV2BGR_I420);' – 2015-02-24 06:33:35

+0

我試過了,結果差不多 - 很多線條,但現在有不同的顏色。 – 2015-02-24 06:41:32

+0

爲什麼你評論這行'libvlc_video_set_format(mp,「RV24」,240,320,240 * 3);'?您需要指定視頻格式。你嘗試取消註釋嗎? – OpenMinded 2015-02-24 06:45:04

回答

0

好,所以我發現了這個問題,我在視頻分辨率上犯了一個錯誤,原始分辨率是2560x1536,所以當我把libvlc_video_set_format(mp,2560,1536,2560 *3);我的照片看起來很好,所以在我把它傳遞給OpenCV後,我可以調整它。