I am using OpenCV 3.0.0 beta to write a video with
VideoWriter vw("out.avi",VideoWriter::fourcc('h','2','6','4'),24,frame.size());
This calls on the ffmpeg backend (avcodec version 56.26.100), which calls libx264 (build 142). However, apparently it is not possible to specify a libx264 profile from OpenCV, and the default bitrate is too low for my application.
Where can I change the default behaviour of ffmpeg or possibly libx264 to get higher quality video written from OpenCV?
Meanwhile, I found the answer myself. On line 1232 of modules/videoio/src/cap_ffmpeg_impl.hpp (on OpenCV 3.0.0 beta) the default CRF is defined as 23:
av_opt_set(c->priv_data,"crf","23", 0);
Change the number to the value of your liking. This requires recompiling OpenCV and it is a global setting until the videoio module properly implements codec options.
Related
I built OpenCV (with mingw 4.9.2 # Win7Prof64) with
CMake switches for WITH_FFMPEG and WITH_DSHOW turned on.
When I access a movie file with VideoCapture (fileName) works fine (it uses ffmpeg backend then).
But VideoCapture (fileName, cv::CAP_DSHOW) doesn't work
(isOpened() returns false).
What is missing/ what do I do wrong?
Are there any kind of dshow libs/ dlls needed?
As to my duplicate in opencv forum ... cv::CAP_DSHOW is not implemented for files.
I was unable to read video frames in OpenCV (python and c++) after using ffmpeg.
Specifically, I had the following problems:
1) unable to open the video file using VideoCapture.
2) able to open the video file using VideoCapture, but reading zero frames and/or receiving frames of size 0x0 pixels.
I am pasting my solution below, hoping this will help others.
These problems were encountered on Mac OS X Sierra 10.12.5, ffmpeg 3.2.4, Python 2.7.13, g++ 4.2.1
1) OpenCV cannot read as many video formats as ffmpeg. Therefore, it is often possible to play videos in VLC, but not to open them in OpenCV's VideoCapture. Hence, conversion with a tool such as ffmpeg is often required (see next point). One format that worked for me is h.264.
2) OpenCV seemed to require YUV420, whereas ffmpeg used YUV444 by default. Therefore, the following command solved my problems:
ffmpeg -i input.avi -c:v libx264 -vf format=yuv420p output.mp4
So I'm trying to write a video using the openCV videoWriter as such:
writer=cv.CreateVideoWriter(path+"test_output.avi",-1,fps,(W,H),1)
So instead of supplying the FOURCC I supplied -1 in order to see what codecs I have available.
Result was Microsoft RLE, Microsoft Video 1, Intel YUV, and Uncompressed.
The reason is that when configuring openCV using CMAKE for Visual Studio 10 x64, this is what I have in the video i/o:
Video I/O: DirectShow
Is there a way to switch this to FFMPEG? I know the ffmpeg dll is present in \3dparty\ffmpeg.
I looked for Cmake FFMPEG flags but found none whatsoever. The weird thing is in the CmakeLists.txt in the opencv root under the video section:
if(UNIX AND NOT APPLE)
<FFMPEG stuff>
elseif(WIN32)
status(" Video I/O:" HAVE_VIDEOINPUT THEN DirectShow ELSE NO)
endif()
So it seems to me that opencv automatically switched to DirectShow and gives no choice of using FFMpeg.
Or rather can one upgrade Driectshow to support other formats such as Divx or h264?
Any ideas?
The answer your looking for is here and it works for the 32 bit and 64 bit configurations. I used this build of FFMPEG. http://ffmpeg.zeranoe.com/builds/win64/dev/ffmpeg-git-1aeb88b-win64-dev.7z
1) Download OpenCV 2.3
2) Open the root CMakeLists.txt and insert the line set(HAVE_FFMPEG 1)
3) Download http://ffmpeg.zeranoe.com/builds/win64/dev/ffmpeg-git-1aeb88b-win64-dev.7z (or the 32 bit build if u fancy it)
4) Edit avformat.h found in the ffmpeg include dir with #define INT64_C
5) Edit cap_ffmpeg_impl.hpp from the highgui project with #define snprintf _snprintf
6) in your highgui project properties under C/C++>Additional Include Directories add path of the include directory of FFMPEG you just downloaded
7)On the same property page under Linker>General>Additional Library Dependencies add the path of the lib directory of FFMPEG you just downloaded
8)On the same property page under Linker>Input>Additional dependencies add ALL the names of the libraries of ffmpeg found in lib (avformat.lib, avscale.lib, avcore.lib etc)
9) build the highgui project
10) Add the path of the .dll files that came with FFMPEG to the System Path environment variable.
That's it! 10 easy steps ;)
If you're still unable to compile OpenCV with FFMpeg support, the library that is linked on this blogpost provides a pretty good starting point for creating a windows-compatible FFMpeg wrapper: Making OpenCV-2.3 work with ffmpeg library and Microsoft Visual Studio 2010
I'm trying to stream in video to my Linux machine. On the Linux side I'm using OpenCV. I tried to find a list of supported codecs but couldn't find any.
Where can I find a list of supported codecs that can be used with open CV?
Thanks,
Nahum
From here:
The mencoder video encoder is an open source video encoder which is
available on Mac OS X, Windows, and Linux. To convert existing videos
to the I420 format supported by OpenCV on all platforms, install the
mencoder binary and use:
$ mencoder in.avi -ovc raw -vf format=i420 -o out.avi
Looks like the I420 format is what you're looking for.
I am running Python2.7.1 and OpenCV 2.2 without problems in my WinXP laptop and wrote a tracking program that is working without a glitch. But for some strange reason I cannot get the same program to run in any other computer where I tried to install OpenCV and Python (using the same binaries or appropriate 64 bit binaries). In those computers OpenCV seems to be correctly installed (although I have only tested and CaptureFromCamera() in the webcam of the laptop), but CaptureFromFile() return 'None' and give "error: Array should be CvMat or IplImage" after a QueryFrame, for example.
This simple code:
import cv /
videofile = cv.CaptureFromFile('a.avi') /
frame = cv.QueryFrame(videofile) /
print type(videofile) /
print type(frame)
returns:
type 'cv.Capture' /
type 'NoneType'
OpenCV and Python are in the windows PATH...
I have moved the OpenCV site-packages content back and forth to the Pyhton27 Lib\Site-packages folder.
I tried different avi files (just in case it was some CODEC problem). This AVI uses MJPEG encoding (and GSpot reports that ffdshow Video Decoder is used for reading).
Images work fine (I think): the simple convert code:
im = cv.LoadImageM("c:\tests\colormap3.tif")
cv.SaveImage("c:\tests\colormap3-out.png", im)
opens, converts and saves the new image...
I have tested with AVI files in different folders, using "c:\", "c:/", "c:\" and "c://".
I am lost here... Anyone has any idea of what stupid and noob mistake may be the cause of this? Thanks
It may sound stupid, but I just had the same issue with the same symptoms for the same code snippet (Python 2.7.1, Win 7, OpenCV 2.2.0). I changed file path from
capture = cv.CaptureFromFile('C:\Misc\tree.avi')
to
capture = cv.CaptureFromFile('C:/Misc/tree.avi')
and voila
<type 'cv.Capture'>
<type 'cv.iplimage'>
I was having this problem, and here is how I fixed it. I took a look at the output of OpenCV's cmake command, and it had the following line:
...
-- FFMPEG: NO
...
In order to fix this, you might be able to get away with simply installing the following libraries:
sudo apt-get install libavformat-dev libavcodec-dev libavfilter-dev libswscale-dev
Re-running cmake will hopefully now say:
...
-- FFMPEG: YES
...
Re-compile OpenCV, re-install it, and hopefully you can now read videos. If you still have problems, you can try to compile ffmpeg using the --enable-shared option, using these as guides:
http://opencv.willowgarage.com/wiki/FFMPEG
http://ubuntuforums.org/showthread.php?t=786095
Hope that helps.
This must be an issue with the default codecs. OpenCV uses brute force methods to open video files or capture from camera. It goes by trial and error through all sources/codecs/apis it can find in some reasonable order. (at least 1.1 did so).
That means that on n different systems (or days) you may get n different ways of accessing the same video. The order of multiple webcams for instance, is also non-deterministic and may depend on plugging order or butterflies.
Find out what your laptop uses, (re)install that on all the systems and retry.
Also, in the c version, you can look at the capture's properties
look for cvGetCaptureProperty and cvSetCaptureProperty where you might be able to hint to the format.
[EDIT]
Just looked i tup in the docs, these functions are also available in Python. Take a look, it should help.