Android 视频在上传到网站时不保持方向

标签 android video upload orientation

我在我的 Android 应用程序中创建了自己的 Activity 来录制视频,它似乎工作正常:视频被保存到手机中的指定文件夹中,每当我在图库中查看它们时,它们的方向都是正确的,它们甚至可以毫无问题地上传到 amazon s3 服务器。

唯一的问题发生在我从服务器查看视频时,它们没有根据我录制的位置正确旋转。

每当我录制视频时,我都会设置视频的方向提示,这样可以在我的设备上正确播放,但对上传的视频没有影响。

这是我用来录制视频的代码

private boolean prepareVideoRecorder() {
    // It is very important to unlock the camera before doing setCamera
    // or it will results in a black preview
    if (camera == null) 
    {
        camera = getCameraInstance();
    }

    if (recorder == null){
        recorder = new MediaRecorder();
    }
    //Have to stop preview before starting to record
    camera.stopPreview();
    // Step 1: Unlock and set camera to MediaRecorder
    camera.unlock();
    recorder.setCamera(camera);

    // Step 2: Set sources
    recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));

    // Step 4: Set output file
    recorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).getAbsolutePath());

    // No limit. Don't forget to check the space on disk.
    recorder.setMaxDuration(50000);
    recorder.setVideoFrameRate(30);
    recorder.setVideoEncodingBitRate(3000000);
    recorder.setAudioEncodingBitRate(8000);

    // Step 5: Set the preview output
    recorder.setPreviewDisplay(cameraPreview.getHolder().getSurface());

    //Setting the camera's orientation
    int degree = 0;
    // do not rotate image, just put rotation info in
    switch (mOrientation) {
        case ORIENTATION_LANDSCAPE_INVERTED:
            degree = 180;
            break;
        case ORIENTATION_PORTRAIT_NORMAL:
            degree = 90;
            break;
        case ORIENTATION_LANDSCAPE_NORMAL:
            degree = 0;
            break;
        case ORIENTATION_PORTRAIT_INVERTED:
            degree = 270;
            break;
    }

    recorder.setOrientationHint(degree);

    // Step 6: Prepare configured MediaRecorder
    try {
        recorder.prepare();
    } catch (IllegalStateException e) {
        // This is thrown if the previous calls are not called with the
        // proper order
        e.printStackTrace();
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        releaseMediaRecorder();
        e.printStackTrace();
        return false;
    }        
    //Everything went successfully
    return true;
}

然后开始录制:

private void startRecording(){
    //TODO: Draw stuff to let user know when they are recording
    // initialize video camera
    if (prepareVideoRecorder()) {
        //Assuring the app doesn't go to sleep while recording
        timerView.setKeepScreenOn(true);

        //"Adding" timer whenever we start to record
        timerView.setVisibility(View.VISIBLE);

        // Camera is available and unlocked, MediaRecorder is prepared,
        // now you can start recording
        recorder.start();

        // inform the user that recording has started
        shutter.setBackgroundResource(R.drawable.custom_button_red);
        isRecording = true;

        //TODO:Testing timer stuff
        startTimer();
    } else {
        // prepare didn't work, release the camera
        releaseMediaRecorder();
        // inform user
    }
}

以及上传:

@Override
    protected HttpEntity doInBackground(Object... params){
        DefaultHttpClient client = new DefaultHttpClient();
        String stepID = (String)params[0];
        filepath = (String)params[1];
        String auth_token = (String)params[2];
        String projectID = (String)params[3];

        String filename = Uri.parse(filepath).getLastPathSegment().toString();
        String videoType = filename.substring(filename.lastIndexOf("."), filename.length());

        String url = VIDEO_URL+"?auth_token=" + auth_token;

        HttpPost post = new HttpPost(url);
        HttpContext localContext = new BasicHttpContext();
        MultipartEntity imageMPentity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        try{

            imageMPentity.addPart("project_id", new StringBody(""+projectID));
            imageMPentity.addPart("step_id", new StringBody(stepID));
            imageMPentity.addPart("content_type", new StringBody("video/"+videoType));
            imageMPentity.addPart("filename", new StringBody(filename));
            imageMPentity.addPart("video_path", new FileBody(new File(filepath)));       
            post.setEntity(imageMPentity);                

        } catch(Exception e){
            Log.e(StepDisplay.class.getName(), e.getLocalizedMessage(), e);
        }
        HttpResponse response = null;

        try {
            response = client.execute(post, localContext);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpEntity result = null;

        if(response != null){
             result = response.getEntity();
        }
        return result;
    }

我的一种方法是在应用程序中手动旋转视频,但我听说这样做成本很高,而且会大大降低应用程序的速度。我想知道是否有一种简单的方法可以将方向元数据发送到网站并处理服务器端的旋转。

我使用 ruby​​ on rails 网站和 carrierwave gem 来上传视频。我想知道是否有一种简单的方法或某种类型的信息可以传递给上传,以便它识别视频已旋转。因为我可以从视频中提取此信息,并在我从手机发送时简单地将其 bundle 在 MultipartEntity 中发送。

最佳答案

如果您使用的是 Ruby on Rails 网站,您可以使用 FFMPEG 将您的视频转换为正确的方向。 您需要在您的服务器上安装 FFMPEG。安装因操作系统而异,但非常简单。

这里是如何根据您发送到服务器的方向转换所有视频。只需为旋转 90 、 180 、 270 、 360 设置条件并运行命令。 如果你没有得到你发送的方向,你可以使用 MiniExif 工具来获取方向

ffmpeg -i in.mov -vf "transpose=1"out.mov

0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip

关于Android 视频在上传到网站时不保持方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21151851/

相关文章:

ios - 将图像数组转换为 MP4 文件时遇到问题

javascript - 反转此特定 html5 视频播放器的静音/取消静音功能

video - 如何使用 ffmpeg 将图像帧转换为视频?

使用经典 ASP 上传图片

Android UI 滞后于设置图像

android - 具有可扩展项目的 RecyclerView

安卓屏幕动画

android - MainActivity 使用leakcanary 泄露

php - UPLOAD_ERR_INI_SIZE 错误,但 post_max_size 和 upload_max_filesize 值看起来正确

ios - 错误 ITMS-90502 App Store 上传 - Xcode