ofFileDialog for video?

evening all,

im trying to load video using ofFileDialogResult, but am coming up dry here. ive batted it back and forth, trying different ways, but went back to the original.

now it does load a movie, but doesn’t process it completely to screen, it is glitchy. obviously missing something, but just cant think what it can be, since actually having not seen anything else on the forum.
but ofVideoPlayer doesn’t have things like resize and a few other that I’ve had to work around. more or less cutting things out

many thanks. this is just getting on my nipples thinking how to process it.

here is the code if someone can shed light on this
.h

vector<ofVideoPlayer> loadedVideos;
vector<ofVideoPlayer> processVideos;
string originalFileExtend;

main .cpp

//--------------------------------------------------------------
void ofApp::setup(){
    ofSetLogLevel(OF_LOG_VERBOSE);
}

//--------------------------------------------------------------
void ofApp::update(){

}

//--------------------------------------------------------------
void ofApp::draw(){
    ofDrawBitmapString("press space to open file", 20, 15);
    
    for (unsigned int i = 0; i < loadedVideos.size(); i++) {
        loadedVideos[i].draw(0, 20);
    }
    
    for (unsigned int i = 0; i < processVideos.size(); i++) {
        loadedVideos[i].draw(processVideos[i].getWidth(), 20);
    }
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    if (key == ' ') {
        ofFileDialogResult openFileResult = ofSystemLoadDialog("select video [.mp4/.mov");
        
        if (openFileResult.bSuccess) {
            ofLogVerbose("you have selected a file");
            processFile(openFileResult);
        } else {
            ofLogVerbose("you hit cancel");
        }
    }
}

//--------------------------------------------------------------
void ofApp::keyReleased(int key){

}

//--------------------------------------------------------------
void ofApp::processFile(ofFileDialogResult openFileResult){
    ofLogVerbose("getName():" + openFileResult.getName());
    ofLogVerbose("getPath():" + openFileResult.getPath());
    
    ofFile file(openFileResult.getPath());
    
    if (file.exists()) {
        processVideos.clear();
        loadedVideos.clear();
        
        ofLogVerbose("it exists - just checking extension");
        string fileExtension = ofToUpper(file.getExtension());
        
        if (fileExtension == "MP4" || fileExtension == "MOV") {
            originalFileExtend = fileExtension;
            
            ofVideoPlayer videoPlayer;
            videoPlayer.loadMovie(openFileResult.getPath());
            
            loadedVideos.push_back(videoPlayer);
            
            int w = videoPlayer.getWidth();
            int h = videoPlayer.getHeight();
            
            ofVideoPlayer processedVideo = videoPlayer;
            
            processVideos.push_back(processedVideo);
        }
    }
}

Hi,
you might be havving trouble because you are storing the ofVideoPlayers inside vectors.
Try not using vectors. If that was the problem do the following:
Replace this:

by this

if (fileExtension == "MP4" || fileExtension == "MOV") {
        originalFileExtend = fileExtension;

        loadedVideos.push_back(ofVideoPlayer());
       loadedVideos.back().loadMovie(openFileResult.getPath());
       
    }

To expand a bit on what @roymacdonald mentioned: it may be that the video player isn’t copying itself correctly, and the method Roy describes will get around that limitation.

In your method:

ofVideoPlayer videoPlayer;
videoPlayer.loadMovie(openFileResult.getPath());

loadedVideos.push_back(videoPlayer);

You’ll make a new ofVideoPlayer, load a video into it, and then add a copy of that video player to the loadedVideos vector. After the “if” statement ends, the ofVideoPlayer called videoPlayer will get destroyed, and is probably destroying some resources the copy needs.

In Roy’s method:

loadedVideos.push_back(ofVideoPlayer());
loadedVideos.back().loadMovie(openFileResult.getPath());

You’ll create an ofVideoPlayer in the vector, and then load the video into it (the .back() function on the vector will give you the last object in the vector). No copies.

Often times people will use a vector of shared_ptrs to a type of object instead of a vector of the object directly (so vector< shared_ptr<ofVideoPlayer> > instead of vector<ofVideoPlayer>). This means that whenever you trigger copies, the only thing that’s copied is the pointer and the actual object just sticks around.

wow, thanks for that guys. makes sense about the vectors. ill give it a try when i get to my computer.
ill make an example and post it. just so others can learn from it

thanks again

ofVideoPlayer should copy itself fine, insternally it uses a smart pointer to the implementation and it has shallow copy semantics meaning that a copy is just a reference which will work with a vector without problem, if it doesn’t it’s a bug : )

which platform are you in?

im on mac. lastest yosemite using public oF8.4

so got to try the changes, and still a problem. though it does work since it loads a movie.
would this be a codec thing? but would think not due to loadMovie being quite a universal thing.

here is the changed code for the process file, the rest is just the same from previously posted

void ofApp::processFile(ofFileDialogResult openFileResult){
    ofLogVerbose("getName():" + openFileResult.getName());
    ofLogVerbose("getPath():" + openFileResult.getPath());
    
    ofFile file(openFileResult.getPath());
    
    if (file.exists()) {
        processVideos.clear();
        loadedVideos.clear();
        
        ofLogVerbose("it exists - just checking extension");
        string fileExtension = ofToUpper(file.getExtension());
        
        if (fileExtension == "MP4" || fileExtension == "MOV") {
            originalFileExtend = fileExtension;
            
            loadedVideos.push_back(ofVideoPlayer());
            loadedVideos.back().loadMovie(openFileResult.getPath());
            
        }
    }
}

and here is a screenshot to show

Hi,
just tryed your original code and it works for me, no problem with the vectors.
the only thing that is missing is to call the update method of the video players within the ofApp update.
like this:

//--------------------------------------------------------------
void ofApp::update(){
 for (unsigned int i = 0; i < loadedVideos.size(); i++) {
    loadedVideos[i].update();
}

for (unsigned int i = 0; i < processVideos.size(); i++) {
    processVideos[i].update();
}
}

You were never updating the texture of the video player that’s why you were getting those glitches.
best

1 Like

ah ha ha ha.
that explains pretty much everything. it had to be something easy. and really, it was. just an oversight more than anything.
i did slap myself on the forehead when i read it. you may have heard it :wink:

tried that, and it works. of course :wink:

many thanks. this has just bothered me for days trying. ill post up some code later with all the results as a tutorial and something even for myself :wink:

my internet has been off a few days, but have just posted up the findings from this post and put into a github file with the source.

thanks again for the help, you are mentioned in the read me :wink:

https://github.com/lewislepton/openMovieDialog