Record Frame by Frame into a byteArrayOutputStreamVideoData and Save to a File

21 Views Asked by At

I am facing an issue with my APP.

Objective: It allows configuring the URL of an IP camera and displays what that camera captures on the screen. I need to create a functionality where, when the user presses the button, the application starts recording the video and then saves it to the gallery.

What I have achieved so far: The user can press the button to start recording. The camera is being managed by an external library, and it has a function that calls a callback for each new frame, loading the bytes of that frame. So, I created this code:

@Override
public void onCameraFrame(CameraFrame frame) {

    if (this.mCameraRecordingInProgress) {
        try {
            byteArrayOutputStreamVideoData.write(frame.getData());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
    }
}

After 15 seconds of recording, it stops and calls this piece of code:

public void endRecording() {
    if (!this.mCameraRecordingInProgress) {
        return;
    }

    this.mCameraRecordingInProgress = false;

    try {
        byteArrayOutputStreamVideoData.close();
        byte[] videoBytes = byteArrayOutputStreamVideoData.toByteArray();
        this.reCreateVideoByBytes(videoBytes);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

Here's the code for the reCreateVideoByBytes(videoBytes) function:

public void reCreateVideoByBytes(byte[] videoBytes) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SEREDUtils.DATE_FORMAT_FILE_NAME);
    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append(mTextViewCamName.getText().toString()).append(SEREDUtils.PREFIX_DATE_INITIAL_FILE).append(simpleDateFormat.format(this.recordingInitDate)).append(SEREDUtils.PREFIX_DATE_END_FILE).append(simpleDateFormat.format(new Date())).append(SEREDUtils.SUFFIX_TYPE_VIDEO);

    File inputFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), stringBuilder.toString());

    try {
        FileOutputStream fos = new FileOutputStream(inputFile);
        fos.write(videoBytes);
        fos.close();
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    }
}

Problem: It is saving the recording to the local file, but the recording seems to be playing at 2x speed, and it always cuts about 8 seconds of the recording. It only shows half of it, even though it's writing all the frames.

0

There are 0 best solutions below