Ask Your Question

Video writer in java not saving

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

tgpsantos gravatar image

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

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()));"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

        Log.w("opened video", "OPENED");"saved_video.avi", VideoWriter.fourcc('M','J','P','G'),
                25.0D,  new Size(mOpenCvCameraView.getWidth(),mOpenCvCameraView.getHeight()));
    } else if(frameCounter<50) {
        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


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 -0600 )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 -0600 )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 -0600 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2020-10-22 00:54:59 -0600

check this blog... here is basic camera function

1) Resolution

2) Focus Mode

3) Flash Mode

4) Switch Camera -> for FRONT/RARE Camera View

5) Take Pictures

6) Record Video -> START/STOP >> "NEED TO WRITE CODE FOR MediaRecorder IN OPNECV 4.4.0 LIBRARY"

edit flag offensive delete link more


@sohamgandhi please stop posting same answer on several question

sturkmen gravatar imagesturkmen ( 2020-10-22 02:06:37 -0600 )edit

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

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

Question Tools

1 follower


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

Seen: 1,384 times

Last updated: Oct 22 '20