Ask Your Question
0

what's wrong with this

asked 2016-08-08 17:01:56 -0600

atv gravatar image
// Open a video file for writing (the MP4V codec works on OS X and Windows)
cv::VideoWriter out("Motion", 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);}

for (;;)
{ 

(timer>180) {
    // Close the old file
    //out.release();
    // Copy file
    cout << "Copying file" << endl;
    system("./copyfile.sh &");
    timer=0;
    // Move the old file to file with a timestamp suffix
    time(&stamp);
    char cmdbuf[128];
    string time_stamp=ctime(&stamp);
    time_stamp.pop_back();
    snprintf(cmdbuf,sizeof(cmdbuf),"mv bla 'bla_%s' &",time_stamp.c_str());
    if(system(cmdbuf)) {
    std::cout << "Error! Unable to execute file move" << std::endl;
    std::exit(-1);}
    system("touch bla &");
    // And open a new one
    //cv::VideoWriter out("bla", 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);}
    }

The for loop is the opencv loop. As you can see i tried doing it the simple way, i also tried the stuff that is commented out, but it keeps giving me problems. Eventually it either will not create the new file, and then it can't move it because it does not exist. The shell script just reads the file. Is there something grossly incorrect i am doing?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-08-08 21:08:12 -0600

berak gravatar image
  • you cannot move a file, which is still opened for writing, so you have to call out.release() before .
  • ctime() writes timestamps terminated with \n , you have to remove that before using it in a filename.
edit flag offensive delete link more

Comments

It seems to write the files fine though. Do you think this would cause the problem behind the scenes still? Thanks.

atv gravatar imageatv ( 2016-08-09 01:41:40 -0600 )edit

sorry, idk, what you're talking about. ?

berak gravatar imageberak ( 2016-08-09 01:48:23 -0600 )edit

In respect to your last comment, do you think not removing the \n would cause any problems with above code? As it is writing the files ok (but obviously still having a problem).

atv gravatar imageatv ( 2016-08-09 01:51:18 -0600 )edit

so, what is the problem ?

berak gravatar imageberak ( 2016-08-09 02:00:09 -0600 )edit

Hard to pinpoint. If you run the code in a simple opencv loop you'll see. Seems either it moves the file and then it can't write to it (but it should because i re-opened or touched the file) or some other weirdness. Maybe i should remove the ampersand & so it all runs in sequence.

atv gravatar imageatv ( 2016-08-09 02:08:51 -0600 )edit

the whole "moving" idea is broken, imho. why don't you give it the correct timestamp when opening the writer ?

berak gravatar imageberak ( 2016-08-09 02:19:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-08 17:01:56 -0600

Seen: 169 times

Last updated: Aug 08 '16