Ask Your Question
0

Video writer in java not saving

asked 2019-06-19 04:50:59 -0500

tgpsantos gravatar image

updated 2019-06-20 02:37:50 -0500

Hello all,

I want to record the frames I'm processing in my android smarthpone but nothing is being saved. I'm not getting any error either...

First I created the object in the MainActivity:

private VideoWriter videoWriter;

In the method onCreate I've initialized it and opened

    videoWriter = new VideoWriter("saved_video.avi", VideoWriter.fourcc('M','J','P','G'),
            25.0D, new Size(mOpenCvCameraView.getWidth(),mOpenCvCameraView.getHeight()));
    videoWriter.open("saved_video.avi", VideoWriter.fourcc('M','J','P','G'),
            25.0D,  new Size(mOpenCvCameraView.getWidth(),mOpenCvCameraView.getHeight()));

In the method onCameraFrame I've put the following

    if(!videoWriter.isOpened()){
        Log.w("opened video", "OPENED");
        videoWriter.open("saved_video.avi", VideoWriter.fourcc('M','J','P','G'),
                25.0D,  new Size(mOpenCvCameraView.getWidth(),mOpenCvCameraView.getHeight()));
        videoWriter.write(frame);
    } else if(frameCounter<50) {
        videoWriter.write(frame);
        Log.w("saved frame", "SAVED");
    }
    }

Every frame I run returns videoWriter.isOpened()==false. I'm not using any ".release" method because I didn't know where to put it.

Thanks in advance!

edit retag flag offensive close merge delete

Comments

In the method onCreate I've initialized it and opened

once would have been enough. * if it did not open the 1st time, guess what -- retrying with the same params won't work * please check isOpened() or the load() return value * "saved_video.avi" -- where would that be ? * 'm' 'j' 'p' 'g' is not a valid fourcc (uppercase !). please don't guess, but look those things up.

berak gravatar imageberak ( 2019-06-19 05:29:49 -0500 )edit

Hello berak,

(1) thanks,

(2) isOpened() is returning false

(3) I was hoping it would simply save it to the smartphone root. Am I doing it incorrectly? Previously I've tried to save it to the download folder "/Phone/Download/saved_video.avi"

(4) I had 'M' 'J' 'P' 'G' at first and it didn't work. Later I found a solution that someone was using 'm' 'j' 'p' 'g', so I decided to try it also and forgot to switch it back to MJPG. (edited the Question)

Thank you for your patience

tgpsantos gravatar imagetgpsantos ( 2019-06-20 02:40:02 -0500 )edit

Some more tweaks in the parameters that unfortunately also didn't work: - adding "apiPreference"= -1 and "isColour"=true in the parameters - removing character '_' from the name of the video - changing fps parameter to an integer (25)

any additional suggestions?

tgpsantos gravatar imagetgpsantos ( 2019-06-21 11:23:39 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-06-26 03:20:02 -0500

tgpsantos gravatar image

Finally understood what was wrong. I was just giving the name of the video whereas I should be stating the full path, like the following

    pathSavedVideoFolder = getExternalFilesDir(null).getPath(); 
    filenameRawVideo = pathSavedVideoFolder + "/SavedVideo.avi";

The path should be something like "/storage/emulated/0/Android/data/YOUR PROJECT/files". In order for you to check the saved video you'll need to 'refresh' the content on your device (or simply unplug and plug the phone back to the computer). Afterwards you should easily find the file inside the Android/data/YOUR PROJECT/files folder.

Another mistake I made was that I started to use getFilesDir but in order to have access to it you'd need user permission which I didn't require.

Maybe all of this is obvious for Android developers but coming from Python I still wanted to share! Take care

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-06-19 04:50:59 -0500

Seen: 68 times

Last updated: Jun 26