Search code examples
androidandroid-cameraandroid-mediaplayerandroid-mediarecordervideo-recording

Record video by from camera without mirror it like snap chat


I'm working on social mobile app. It have option to record video for profile. The issue with my recorder is that it mirror the actual video. I know it is default behaviour, but want it work like sanpchat.

Preview

Video when preview

After recording and playback

After recording and playback

Here is my code

// return camera instance when activity open first time
private Camera getCameraInstance() {
    // TODO Auto-generated method stub
    releaseCamera();
    releaseMediaRecorder();
    Camera c = null;
    try {
        cameraId = findFrontFacingCamera();
        if (cameraId < 0) {
            cameraId = findBackFacingCamera();
        }
        c = Camera.open(cameraId);

        // setCameraDisplayOrientation(this,cameraId,c);
        //setCameraDisplayOrientation(this, cameraId, c);
        c.setDisplayOrientation(90);
    } catch (Exception e) {
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}
// return __cameraPreview Id 1 to open front camera
private int findFrontFacingCamera() { 
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            setOrientationHint = 270 ;
            cameraFront = true;
            break;
        }
    }
    return cameraId;
}
// prepare and start recording
private boolean prepareMediaRecorder() {
    mediaRecorder = new MediaRecorder();
    try {
        myCamera.unlock();
    } catch (Exception e) {
        e.printStackTrace();
    }
    mediaRecorder.setCamera(myCamera);

    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    //mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    if (cameraPreview.getVideoSize() != null) {
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setOutputFile(saveUrl);

        mediaRecorder.setVideoEncodingBitRate(10000000);
        mediaRecorder.setVideoFrameRate(30);
        mediaRecorder.setVideoSize(cameraPreview.getVideoSize().width, cameraPreview.getVideoSize().height);

        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    } else {
        CamcorderProfile cp = CamcorderProfile.get(cameraId, CamcorderProfile.QUALITY_HIGH);
        mediaRecorder.setProfile(cp);
    }
    Log.e(TAG, "Video profile CamcorderProfile.QUALITY_HIGH: " + "cp.quality:" + cp.quality
            + ", cp.videoFrameWidth:" + cp.videoFrameWidth
            + ", cp.videoFrameHeight:" + cp.videoFrameHeight);

    mediaRecorder.setMaxDuration(MAX_VIDEO_LENGTH);
    mediaRecorder.setOrientationHint(setOrientationHint);
    try {
        mediaRecorder.prepare();

    } catch (IllegalStateException | IOException e) {
        e.printStackTrace();
        releaseMediaRecorder();
        return false;
    }
    mediaRecorder.start();
    return true;
}

I want it should be same as preview in playback. Sanpchat is already doing the same, thanks in advance.


Solution

  • Nothing work for me finally i need to do it on api side where i submit the video. After upload video i use ffmpeg to vertical flip it and set angle in metadata. here is the command for this.

    ffmpeg -i VIDEO_PATH -vf "vflip" -metadata:s:v rotate=180 DEST_PATH