2014-12-03 782 views
2

我正在嘗試使用opencv videoWriter來獲取視頻文件。但我得到以下問題:OpenCV VideoWriter不起作用

>[libx264 @ 0x132b680] broken ffmpeg default settings detected 
>[libx264 @ 0x132b680] use an encoding preset (e.g. -vpre medium) 
>[libx264 @ 0x132b680] preset usage: -vpre <speed> -vpre <profile> 
>[libx264 @ 0x132b680] speed presets are listed in x264 --help 
>[libx264 @ 0x132b680] profile is optional; x264 defaults to high 
>Could not open codec 'libx264': Unspecified error!!! Output video could not be opened 

我有libx264在我的系統,所以我想這最後一行就是一個副作用

我試圖運行的代碼是從How to write video file in OpenCV 2.4.3採取的一個例子。

int main (int argc, char *argv[]){ 
// Load input video 
VideoCapture input_cap("testi.mp4"); 
if (!input_cap.isOpened()) 
{ 
     std::cout << "!!! Input video could not be opened" << std::endl; 
     return -1; 
} 

// Setup output video 
cv::VideoWriter output_cap("testo.mp4", 
       input_cap.get(CV_CAP_PROP_FOURCC), 
       input_cap.get(CV_CAP_PROP_FPS), 
       cv::Size(input_cap.get(CV_CAP_PROP_FRAME_WIDTH), 
       input_cap.get(CV_CAP_PROP_FRAME_HEIGHT))); 

if (!output_cap.isOpened()) 
{ 
     std::cout << "!!! Output video could not be opened" << std::endl; 
     return -1; 
} 


// Loop to read from input and write to output 
cv::Mat frame; 

while (true) 
{  
    if (!input_cap.read(frame))    
     break; 

    output_cap.write(frame); 
} 

input_cap.release(); 
output_cap.release(); 

return 0; 
} 

我發現類似的問題How to get stream info from opened file in ffmpeg?後,但還沒有人回答正確。
我發現人們被告知檢查opencv是否使用舊的fmmpeg而不是libav,它不是因爲它是新建版本而我的Ubuntu沒有ffmpeg。

回答

0

Dimazavr的答案並不完全正確。首先,您需要將輸出視頻文件擴展名從.mp4更改爲.avi。然後,如果你運行代碼,您會收到以下錯誤信息:

OpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend does not support this codec.) in CvVideoWriter_GStreamer::open, file /home/rwduzhao/store/opencv-2.4.13/modules/highgui/src/cap_gstreamer.cpp, line 1372 
terminate called after throwing an instance of 'cv::Exception' 
    what(): /home/rwduzhao/store/opencv-2.4.13/modules/highgui/src/cap_gstreamer.cpp:1372: error: (-210) Gstreamer Opencv backend does not support this codec. in function CvVideoWriter_GStreamer::open 

Aborted (core dumped) 

,這意味着無論是在cv::VideoWriteropencv2.4不支持libx264格式或avi擴展名是不是不符合libx264格式。我不建議使用libx264編解碼器。您可以嘗試通過CV_FOURCC支持以下編解碼器格式列表:

CV_FOURCC('P','I','M','1') = MPEG-1 codec 
CV_FOURCC('M','J','P','G') = motion-jpeg codec 
CV_FOURCC('M', 'P', '4', '2') = MPEG-4.2 codec 
CV_FOURCC('D', 'I', 'V', '3') = MPEG-4.3 codec 
CV_FOURCC('D', 'I', 'V', 'X') = MPEG-4 codec 
CV_FOURCC('U', '2', '6', '3') = H263 codec 
CV_FOURCC('I', '2', '6', '3') = H263I codec 
CV_FOURCC('F', 'L', 'V', '1') = FLV1 codec 

從我的經驗中,CV_FOURCC('D', 'I', 'V', 'X')質量優良。此外,如果您將cv_fourcc設置爲-1,則可以在GUI窗口中選擇系統中支持的編解碼器格式之一。您可以目睹運行過程here

2

VideoWriter不支持.mp4擴展名。使用.avi代替