video writer does not allow a string variable ?

asked 2016-07-27 08:50:18 -0600

atv gravatar image

Video writer gives me empty containers messages and such on mac if i do not specify a constant literal myself, i.e. "test". If i give it a string variable, it won't write.

This is making it difficult for me to add timestamps and such.

Any ideas?

edit retag flag offensive close merge delete

Comments

a string where ?

berak gravatar imageberak ( 2016-07-27 09:03:58 -0600 )edit

Sorry, in the first argument (the path). For example if i do format("%s",variable) it won't work. If i put in "filename" it works fine.

atv gravatar imageatv ( 2016-07-27 09:46:09 -0600 )edit

could you show, what you tried ? (examples, that did not work) ?

it really should work ..

berak gravatar imageberak ( 2016-07-27 09:52:36 -0600 )edit

did you try something like ctime ?

those are terminated with \n , which is a bummer to have in any filename (it's not just the videoWriter)

berak gravatar imageberak ( 2016-07-27 10:22:06 -0600 )edit

Hmm yes I did. But I also experienced it with any word I Put in a string variable. So maybe I should chop of the last character and retry. I'll post my code in a bit.

atv gravatar imageatv ( 2016-07-27 12:35:18 -0600 )edit

yes, exactly. cut it off.

int len = strlen(ctime_string)
ctime_string[len-1] = 0; // chop
berak gravatar imageberak ( 2016-07-27 12:39:10 -0600 )edit

Ok i'll try that. Thanks!

My code was:

time(&stamp);
// Open a video file for writing (the MP4V codec works on OS X and Windows)
string time_stamp=format("%s",ctime(&stamp));
cv::VideoWriter out(time_stamp, CV_FOURCC('m','p', '4', 'v'), FPS, cv::Size(width, height));
if(!out.isOpened()) {
std::cout <<"Error! Unable to open video file for output." << std::endl;
std::exit(-1);}
atv gravatar imageatv ( 2016-07-27 13:11:17 -0600 )edit

Hmm i don't think c++ has strlen? (wait it does in cstring but for some reason although i use std namespace it doesn't recognise strlen..) I tried this though, also didn't work:

  time(&stamp);
// Open a video file for writing (the MP4V codec works on OS X and Windows)
string time_stamp=format("%s",ctime(&stamp));
time_stamp.pop_back();
cv::VideoWriter out(time_stamp, CV_FOURCC('m','p', '4', 'v'), FPS, cv::Size(width, height));
if(!out.isOpened()) {
std::cout <<"Error! Unable to open video file for output." << std::endl;
std::exit(-1);}
atv gravatar imageatv ( 2016-07-27 13:28:50 -0600 )edit

Ok i don't get it. So i started using size or length to get the size of the string, and do as follows.

time(&stamp);
// Open a video file for writing (the MP4V codec works on OS X and Windows)
string time_stamp=format("%s",ctime(&stamp));
int len=time_stamp.size();
std::cout << len << time_stamp << std::endl;
time_stamp[len-1]=0;
cv::VideoWriter out(time_stamp, CV_FOURCC('m','p', '4', 'v'), FPS, cv::Size(width, height));

But still no go, even though i'm chopping the newline terminator. I keep getting this: 25Wed Jul 27 20:17:09 2016

WARNING: Could not create empty movie file container. Didn't successfully update movie file.

atv gravatar imageatv ( 2016-07-27 14:18:53 -0600 )edit

ah, sorry for the confusion, you were meant to chop the c char* , not the c++ std::string. so:

int len = strlen(ctime_str) ctime_str[len-1] = 0; // chop string filename = ctime_str

berak gravatar imageberak ( 2016-07-28 00:08:13 -0600 )edit