I’m using the following code to read a video from file, apply the canny edge algorithm and write the modified video to a file. The code compiles and runs perfectly. But, the video is not written! I’m utterly confused. Please tell me what the error is.
The file is not created at all! OS: Ubuntu 12.10
Code for writing to the output file
Opening the output file
bool setOutput(const std::string &filename, int codec=0, double framerate=0.0, bool isColor=true) {
outputFile= filename;
extension.clear();
if (framerate==0.0)
framerate= getFrameRate(); // same as input
char c[4];
// use same codec as input
if (codec==0) {
codec= getCodec(c);
}
// Open output video
return writer.open(outputFile, // filename
codec, // codec to be used
framerate, // frame rate of the video
getFrameSize(), // frame size
isColor); // color video?
}
Writing the frame
void writeNextFrame (Mat& frame)
{
writer.write (frame);
}
And there’s a separate run method which executes these
Whenever I encounter strange behaviors in my applications, I write a short, self contained, correct (compilable), example to help me understand what’s going on.
I wrote the code below to illustrate what you should be doing. It’s worth noting that it works perfectly on my Mac OS X:
Inspecting the input file with FFmpeg reveals (
ffmpeg -i Wildlife.avi):and the output:
So the only significant change between the two files is that the output generated by OpenCV doesn’t have an audio stream, which is the correct behavior since OpenCV doesn’t deal with audio.
Make sure your user has the proper permission to read/write/execute in the directory you are running the application. Also, the debugs I added in the code will probably assist you to find problems related to input/output capture.