I want to encode images to H264 video in OpenCV.
As part of my software for image tracking, I'm using VideoWriter in OpenCV 3.4 (64-bit) with Visual Studio 2017 C++. I use ffmpeg for manual encoding, and as it comes with OpenCV I assume this is the best option. My version of OpenCV indicates it has been compiled with ffmpeg, and I can verify it loads the opencv_ffmpeg340_64.dll.
Decoding H264 (AVC1) is absolutely fine, including when specifically using the ffmpeg API. But encoding anything other than MJPG or raw images doesn't work:
VideoWriter.Open() mostly returns false, for some cases it only writes an empty or small header but won't write frames. I've tried not only the ffmpeg API, but also any available API. Redirecting console/debug output to intermediate window in VC doesn't give any messages from OpenCV or ffmpeg.
There are numerous posts on previous versions of OpenCV using FFmpeg, including (Cisco) OpenH264 library and difficulties using this. I've tried many options, installing codecs pack including H264, ffmpeg static version, Cisco openH264 library, setting paths etc and even tried the low level ffmpeg library functions such as cvCreateVideoWriter exposed in OpenCV, and using different video containers (AVI, MPG, MP4) as well as fourcc strings.
I can't believe in 2018 the great OpenCV with FFmpeg support is unable to encode anything but arcane MJPG or raw images. If it uses FFmpeg surely a significant set of functionality should be available?
Though I think this should work, my next best option would be using a separate ffmpeg library, which would ironically add another ffmpeg.dll to my solution I imagine. Any suggestion appreciated!
Code fragment (using any video encoding API):
VideoWriter writer;
int codec = VideoWriter::fourcc('X', '2', '6', '4');
writer.open("test.avi", codec, 30, colorImage.size(), true);
writer.write(colorImage);
writer.release();
To answer my own question - thank you for the previous suggestions though - I've managed to create and write frames to H264 video now, without any recompiling needed. This is what I've used:
Using OpenCV version 3.4.1
In the same folder as the exe: opencv_ffmpeg341_64.dll (provided with OpenCV)
In the same folder: openh264-1.7.0-win64.dll (available from: https://github.com/cisco/openh264/releases)
Use OpenCV VideoWriter class, omitting a specific encoding API
Container: MP4
Codec fourcc string: "H264"
I formatted my machine (Windows 10 64bit) so I can't exclude any issues with potential codec-clashing - also I have not fully tested for other combinations. As this appears to be such a common problem (with many misconceptions) I hope this is useful.
Related
Referring to the opencv VideoCapture documentation here, there are a few properties that only work DC1394 v 2.x backend (specifically CV_CAP_PROP_BUFFERSIZE is what I want to change). Looking at the buildInformation for openCV on my machine I see it is compiled with support for DC1394 v 2.x, FFMPEG, Gstreamer. I cannot see to figure out how to set the video backend when initializing VideoCapture. Any ideas?
Looking at the VideoCapture source code there doesn't seem to be a way to open with a specific backend in opencv 2.4.
In fact it will just pick the first backend available it can find with a defined order in the code (for example lets suppose you have opencv compiled for ffmpeg and gstreamer, the order ffmpeg first and gstreamer second, if you distribute your binary to someone and they have gstreamer but not ffmpeg it will fail to open the video capture but continue to search for the next option, in this case gstreamer). So as far as I can see you have 3 choices to use the DC1394 backend:
Modify the source so it always tries to search for DC1394 first (or even further add a function to choose a backend)
Compile with just support for DC1394 as a video backend
Convert your code to opencv 3.x which has the option to set a backend when opening the video capturer.
I have created a Qt 5.8 web engine based application but unfortunately, the video is not playing in the application.
After coming across many forums I found that I need to build QT with flag proprietary-codec
But this solution comes with the warning: When distributing proprietary codec libraries, you must acquire licenses for them.
Unfortunately, I can't use these proprietary codec libraries.
If I use open source codec libraries do I need to compile QT again with some other flag.
Is there any other solution to this approach.
Unfortunately for mp4 file format using proprietary codecs is the only option (i.e. this includes acquiring licenses to distribute and configuring/building Qt with the option you specified already in your question).
From Qt docs:
Qt WebEngine supports the MPEG-4 Part 14 (MP4) file format only if the required proprietary audio and video codecs, such as H.264 and MPEG layer-3 (MP3), have been enabled.
I have a Qt 5.3 application that plays videos which works fine for some videos. Unfortunately when given some .mov files, I either get a QMediaPlayer::FormatError or I can hear audio but no video.
I am assuming this is due to missing codecs, is there a way to add support so that I can play the videos?
EDIT: This is on OS X
QMediaPlayer can play any format that the platform's media service supports. You should install the right codecs for a video type to be played successfully. Try to install GStreamer codecs on Linux. If you are on Windows install KLite Codecs.
QtMultimedia's implemention is platform dependent (uses direct show on windows, gstream on linux etc). May be it is good to consider using libraries like QtAV or libVLC.
They work for most platforms and no additional codecs and plugins are required to install.
As I've mentioned in my another question, I'm having trouble with Qt 5.1's multimedia module. The QMediaPlayer class can't play some MP3 files. I haven't tested other formats thoroughly.
I'm not sure if this is a codec related problem. But I'm interested in using codec directly in my program rather than relying on the backend support for playing media files. I'll be developing the application in Qt 5.1 for desktop Windows platform.
I have no idea about using codecs in programs. Any pointer will be greatly appreciated.
You can go to [FFMPEG]: http://www.ffmpeg.org/ to integrate the open-source codec to play your media file.
I'm trying to decode an MP4 file i've created using mp4creator library. I can't find a ffmpeg or libav example that actually works with the libraries, because the code is incompatible with the examples.
I need a simple way of reading the mp4 file and decoding it into raw audio and video frames. In needs to work on windows too.
If someone can point me towards a working and compatible ffmpeg/libav library + example source file that could work too.
If i could get the library version which was used in this tutorial that would work, since this guy seems to be the only one who wrote a decent tutorial on FFMPEG (albeit on some ancient version)
http://dranger.com/ffmpeg/
edit:
In the latest version of ffmpeg i found the example named: filtering_video.c
using a patch found: http://ffmpeg.org/pipermail/ffmpeg-devel/2012-June/126587.html I have gotten the program to do "something" It looks to be working so I will investigate further.
As edited into the question: In the latest version of ffmpeg i found the example named: filtering_video.c
using a patch found: http://ffmpeg.org/pipermail/ffmpeg-devel/2012-June/126587.html I have gotten the program to work.
Using the function avtoipl described here: http://www.digipedia.pl/usenet/thread/16949/6806/
I've gotten the data into an opencv RGB image which i can display.