简体   繁体   中英

Android - Recorded Video misoriented

I have a function in an app that is to record a certain length of video using Front-facing camera.

The recording is fine , however, the orientation is not correct. I have searched lots of time but many questions of this are mentioned only on picture

Part that related to Camera and MediaRecorder are provided below

public void onCreate(Bundle savedInstanceState) {
    // Some of the codes are not shown
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    recorder = new MediaRecorder();
    holder = cameraView.getHolder();

}
private void initRecorder() {
    recorder.setCamera(camera);
    recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    CamcorderProfile cpLow = CamcorderProfile.get(1,
            CamcorderProfile.QUALITY_LOW);
    recorder.setProfile(cpLow);
    mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
    mFileName += "/youraudiofile.mp4";
    recorder.setOutputFile(mFileName);
    recorder.setMaxDuration(50000); 
    recorder.setMaxFileSize(5000000);
}
private void prepareRecorder() {
    recorder.setPreviewDisplay(holder.getSurface());
    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        finish();
    } catch (IOException e) {
        e.printStackTrace();
        finish();
    }
}
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open(findFrontFacingCamera());
camera.unlock();
initRecorder();
prepareRecorder();

}   

The above codes work, and during the record I can get a screen like this 记录

Instead, the resulting video will display like this 结果

How can I correct this?

You should probably be using this:

recorder.setOrientationHint(CamOrientationDegrees);

where 'CamOrientationDegrees' is the angle of the device at start of recording. I've found this to work on most devices except the Samsung S3. The S3 ignores it and orientates the video according to the device orientation, which makes the resultant playback angle incorrect when playing back on (eg) VLC (I still haven't found out how to deal with this!).

If you're not using this on an S3, it should work fine, providing you correctly calculate 'CamOrientationDegrees' taking into account that fact that it's using the front-facing camera (you might need to experiment with it a bit).

CamOrientationDegrees should only take the values 0, 90, 180 or 270.

Video is always recorded as though the device is in landscape orientation, even when the device is in portrait.

There are ways that you can post-process the video to reorient it (eg, ffmpeg ), though I have not experimented with these, and they are third-party libraries, not part of the Android SDK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM