Reading video files with OpenCV VideoCapture - c++

I am having trouble being able top open any video files in OpenCV besides those encoded in MJPEG.
I have installed OpenCV using this script (which should compile OpenCV with support for ffmpeg) and an testing using the sample provided here.
When running with a h264 encoded video I get:
[mov,mp4,m4a,3gp,3g2,mj2 # 0x123ed80] multiple edit list entries, a/v desync might occur, patch welcome
[h264 # 0x12465e0] A non-intra slice in an IDR NAL unit.
[h264 # 0x12465e0] decode_slice_header error
Could not open the output video for write: test.mp4
When running with an MPEG-2 encoded video I get:
[mpegts # 0x1e92d80] PES packet size mismatch
[mpegts # 0x1e92d80] PES packet size mismatch
[mpegts # 0x1e92d80] max_analyze_duration reached
[mpegts # 0x1e92d80] PES packet size mismatch
Could not open the output video for write: test.mpeg
I am running x64 Ubuntu 12.04.
EDIT: I tried OpenCV 2.4.8 on a Ubuntu 13.10 x86 VM, ffmpeg works fine, however the sample code still fails, this time with the following error:
[h264 # 0x849ff40] A non-intra slice in an IDR NAL unit.
[h264 # 0x849ff40] decode_slice_header error
Could not find encoder for codec id 28: Encoder not foundOpenCV Error: Unsupported format or combination of formats (Gstreamer Opencv backend doesn't support this codec acutally.) in CvVideoWriter_GStreamer::open, file /home/dan/Install-OpenCV/Ubuntu/2.4/OpenCV/opencv-2.4.8/modules/highgui/src/cap_gstreamer.cpp, line 505
terminate called after throwing an instance of 'cv::Exception'
what(): /home/dan/Install-OpenCV/Ubuntu/2.4/OpenCV/opencv-2.4.8/modules/highgui/src/cap_gstreamer.cpp:505: error: (-210) Gstreamer Opencv backend doesn't support this codec acutally. in function CvVideoWriter_GStreamer::open

I am not sure about the main reason for this. But I guess this problem is relate to the decoder installed on your system. According to the install script from github, it removes your ffmpg and x264 then rebuilds them from source code. Before testing your OpenCV code, try simple "ffmpeg" command on your test video.
Such as: ffmpeg -i inputfile.avi -f image2 image-%3d.jpeg

The script you have used is quite old and installs old version of OpenCV (2.4.2, while the latest stable is 2.4.8), try to use this script - https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/2.4/opencv2_4_8.sh or install OpenCV and ffmpeg on you own.
As rookiepig mentioned - check whether ffmpeg is working.
Try to use different codec - here http://www.fourcc.org/ is full list of options, of course testing all of them is useless - jsut try the most popular codecs.
I know that it's stupid, but on Windows some codecs works only in release mode(okay, probably they are working in both modes, but on my machine they used to work only in release mode). Try to compile you program in both modes and check whether there is some difference.
And show us you code, maybe there is something wrong in it.

Related

mplayer doesn't like popen suddenly?

Been stumped over a problem with popen for a few days.
The code:
int main(){
FILE *fp = popen("mplayer /home/linaro/Music/cp.mp3", "r");
char buffer[1028];
while (fgets(buffer, 1028, fp) != NULL)
{
std::cerr<<buffer;
}
pclose(fp);
fp = 0;
return 0;
}
was run at the shop on a linaro embedded controller. Ran fine, no errors. Even though the code is simple (Just plays a coldplay song on execution and then quits) it seems to be causing me more grief than one would think.
If I copy and paste the command itself into a normal bash shell, it runs fine. And what's worse, is it ran fine up until it got plugged in somewhere else. I'm not sure if it is now being connected to via a headphone jack versus before it was in a line out jack.
Regardless, the error I get when I run it is (summarized down to the relevant part)
MPlayer svn r34540 (Ubuntu), built with gcc-4.6 (C) 2000-2012 MPlaye4
Team mplayer: could not connect to socket mplayer: No such file or
directory Failed to open LIRC support. You will not be able to use
your remote control.
Playing /home/linaro/Music/cp.mp3 libavformat version 53.21.1
(external) Mismatching header version 53.19.0 Audio only file format
detected. Clip info: Title: The Scientist Artist: Coldplay Album: A
Rush Of Blood To The Head Year: 2002 Comment: Genre: Unknown Load
subtitles in /home/linaro/Music/ Requested audio codec family [mpg123]
(afm=mpg123) not available. Enable it at compilation. Opening audio
decoder: [ffmpeg] FFmpeg/libavcodec audio decoders libavcodec version
53.35.0 (external) Mismatching header version 53.32.2 AUDIO: 44100 Hz, 2
ch, floatle, 256.0 kbit/9.07% (ratio: 32002->352800) Selected audio
codec: [ffmp3float] afm: ffmpeg (FFmpeg MPEG layer-3 audio) Home
directory not accessible: Permission denied AO: [pulse] Init failed:
Connection refused Failed to initialize audio driver 'pulse' Home
directory not accessible: Permission denied [AO_ALSA] alsa-lib:
pcm_hw.c:1293:(snd_pcm_hw_open) open '/dev/snd/pcmC1D0p' failed (-22):
Invalid argument [AO_ALSA] Playback open error: Invalid argument
Failed to initialize audio driver 'alsa' [AO SDL] Samplerate: 44100Hz
Channels: Stereo Format floatle [AO SDL] using aalib audio driver. [AO
SDL] Unsupported audio format: 0x1d. [AO SDL] Unable to open audio: No
available audio device Failed to initialize audio driver 'sdl:aalib'
Could not open/initialize audio device -> no sound. Audio: no sound
Video: no video
Again, if I copy and paste the exact command it executes via popen into the console, it begins playing. It fails if pass -ao alsa, -ao pulse, -ao oss as well, which has me completely stumped. Any help would be appreciated!
Edit:
Linux is linaro, based on Ubuntu 12.04 using arm CPU
Issue wound up being as Jonas and alk suggested -- permissions. Something along the way changed how it ran, but at the end of the day the dirty fix was to just include
su - user -c ' mplayer ... '
To the line being called.

VideoCapture::read always returns false

I'm running OS X El Capitan and trying to use OpenCV VideoCapture to read an .avi file. I've tried it on both opencv 2.4.1 and 3, both of which have the same outcome.
cv::VideoCapture capture("filename.avi");
cv::Mat currentFrame, prevFrame;
bool capPrevSuccess = capture.read(prevFrame);
bool capCurrSuccess = capture.read(currentFrame);
I've verified that the filename.avi is in the current working directory and I don't see any errors in the console.
I'm beginning to wonder if it's something with my machine. I have a similar problem running VideoReader in MATLAB on the same machine. I believe MATLAB uses OpenCV VideoReader as well, perhaps they're connected.
Error using VideoReader/init (line 619) Failed to initialize internal
resources.
Error in VideoReader (line 172)
obj.init(fileName);
EDIT: Looks like this has something to do with the video files I'm using. I downloaded an mp4 video, and had no trouble with it. Unfortunately I need to use the video files I'm using (all .avi), yet they all seem to not work with VideoCapture.

OpenCV IP camera application crashes [h264 # 0xxxxx] missing picture in access unit

I have an opencv application in cpp.
It captures video stream and saves it to video files with the simple constructs from opencv.
It works perfectly with my webcam.
But, it crashes maybe after about ten seconds, while I run it to capture the stream from IP Camara.
My compile command is:
g++ -O3 IP_Camera_linux.cpp -o IP_Camera `pkg-config --cflags --libs opencv`
My Stream from IP cam is accessed like this:
const string Stream = "rtsp://admin:xxxx#192.168.0.101/";
It does run perfectly, shows video and saves it until the displayed video freezes and the application crashes. While the error message on the terminal is:
[h264 # 0x15e6f60] error while decoding MB 59 31, bytestream (-20)
[h264 # 0x15e8200] error while decoding MB 61 27, bytestream (-3)
[h264 # 0x109c880] missing picture in access unit
[h264 # 0x109c000] no frame!
To my understanding, the fist two lines in the above error message might have something to do but does not actually crash the application. The last two lines are probably the reasons or the cause?
Any help?
Got the solution after lots of hit and trial. Just changed the stream address a bit and it worked.
From:
const string Stream = "rtsp://admin:xxxx#192.168.0.101/";
To:
const string Stream = "rtsp://admin:xxxx#192.168.0.101/ch1-s1?tcp";
NO idea, what change it did make?
BUT IT WORKS PERFECTLY!!!
Even the pervasive warnings of the form:
[h264 # 0x15e6f60] error while decoding MB 59 31, bytestream (-20)
[h264 # 0x15e8200] error while decoding MB 61 27, bytestream (-3)
are gone.
Anyways would appreciate if some one could explain it with the logical reason behind.
CREDIT
It is an error from ffmpeg. Probably your ffmpeg is old version and you may want to update it. It solved the problem perfectly for my case by reinstalling the latest opencv and ffmpeg as follows:
Install latest ffmpeg
git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
./configure --enable-shared --disable-static
make
sudo make install
Install the latest opencv
git clone git#github.com:opencv/opencv.git
cd opencv
mkdir build
cd build
cmake ../ -DCMAKE_BUILD_TYPE=Release
make
sudo make install
As a citation to the original answer, adding ?tcp to the end forces the rtsp connection to run using the tcp protocol instead of the udp protocol which is useful if you do not actively check for any connection problem and therefore you can't afford to have any packet loss.
For robust running you can check for NULL image in you loop and if you get a NULL image, you can reset the camera connection:
IplImage *img = cvQueryFrame(camera);
if (img == NULL) {
printf("img == null ");
fflush(stdout);
camera = cvCreateFileCapture("rtsp://admin:xxxx#192.168.0.101/ch1-s1?tcp");
}

Video codec for H264 with opencv

I am trying to create a movie using opencv and I need that output file be in H264 format.
I used the following fourcc in opencv, but I am getting error that suitable codec is not installed.
fourcc=CV_FOURCC('H','2','6','4') ;
When I run my application I get the following error message:
Could not find encoder for codec id 28: Encoder not found
What do I need to be able to use this codec in OpenCV?
H264 is not a codec, but rather a standard, while for example x264 is an encoder that implements the H264 standard (CV_FOURCC('X','2','6','4') ;).
OpenCV can write videos using FFMPEG or VFW. You need to compile FFPMEG with x264-support (instructions can be found on FFMPEG's website)
If you are using VFW: Take a look at http://sourceforge.net/projects/x264vfw/

opencv highgui error

I installed the library opencv in my ubuntu pc and i wrote a program that takes a video from a webcam and it works.
Yesterday I installed the driver for video capture "media_build" to take a video from a video grabber, but the same program doesn't work, while if I open "video for linux 2" on VLC it works.
This is the error:
libv4l2: error set_fmt gave us a different result then try_fmt!
HIGHGUI ERROR: libv4l unable convert to requested pixfmt
HIGHGUI ERROR: V4L: device /dev/video0: Unable to query number of channels
ERROR: capture is NULL
The instruction is:
CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
any suggestion?
thanks
I think your program is crashing. If so you can add if(capture!=NULL)
{// your normal code here}else{// display some error message}. In this case the program wont crash. Probably your video driver isn't providing a interface known to openCv.
Maybe you can use this command:
sudo chmod 666 /dev/video0