我已经在我的Android应用中创建了自己的活动来录制视频,它似乎工作正常:视频正在保存到手机中的指定文件夹,每当我在图库中查看它们时,它们的方向正确,它们甚至上传到亚马逊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;
}
我这样做的方法就是手动旋转应用程序中的视频,但我听说它非常昂贵并且会使应用程序变得太慢。我想知道是否有一种简单的方法将方向元数据发送到网站并处理轮换服务器端。
我在rails网站上使用带有carrierwave gem的ruby来上传视频。我想知道是否有一种简单的方法或某种类型的信息我可以传递给上传,以便让它识别出视频是旋转的。我可以从视频中提取这些信息,只要我从手机发送,就可以将它简单地发送到MultipartEntity中。
答案 0 :(得分:1)
如果您使用的是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