Ffmpeg输出文件无法立即使用

时间:2018-03-31 09:58:19

标签: android ffmpeg

我在Android上执行拆分视频Ffmpeg命令,它会创建output_%d个文件到SD卡(外部存储)

执行并完成命令后,在我打开Gallery App

之前,文件不会立即显示

我尝试为新创建的目录运行媒体扫描程序但我看到该目录为空并且没有立即可用的文件。

2 个答案:

答案 0 :(得分:0)

你的意思是“不立即出现?”

1 /使用文件浏览器?

此问题是由于使用了MTP协议。您必须使用MediaScannerConnection类重新扫描文件。

如果您不想重新启动或开发某些内容,请使用google play中的媒体扫描应用程序。

2 /来自java?

您可以尝试列出该文件夹的所有文件,然后对每个文件执行媒体扫描程序(而不是在目录中执行)。

Context ctx = <your_context>;
File directory = new File("<your_path>");
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
{
    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(Uri.fromFile(files[i]));
    context.sendBroadcast(intent);
}

如果您在扫描完成后立即需要结果,则需要MediaScannerConnectionClient

private static final class Client implements MediaScannerConnectionClient
{   private final String path;
    private final String mimeType;
    public MediaScannerConnection connection;

    public Client(String path, String mimeType)
    {   this.path = path;
        this.mimeType = mimeType;
    }

    @Override
    public void onMediaScannerConnected()
    {   connection.scanFile(path, mimeType);
    }

    @Override
    public void onScanCompleted(String path, Uri uri)
    {   connection.disconnect();
    }
};

public static void scanFile(Context context, String path, String mimeType)
{   Client client = new Client(path, mimeType);
    MediaScannerConnection connection = new MediaScannerConnection(context, client);
    client.connection = connection;
    connection.connect();
}

理想情况下,这应该在ffmpeg拆分器应用程序中完成。

答案 1 :(得分:0)

试试这个......

我正在使用此

您正在使用完整示例代码示例record Video , Spilt image and take output l

如果您需要任何帮助,请尝试使用FFmpeg的命令让我知道

public class FrontCameraPreviewActivity extends AppCompatActivity implements CameraController.CameraRecordListener, FFMpegVideoGenerator.VideoGeneratorListener {
    public final static String TAG = "FrontCameraPreviewAct";

    private final static int CAMERA_PERMISSION_REQUEST_CODE = 50;

    private FrontCameraSurfaceView mPreview;
    private FrameLayout mPreviewFrame;
    private FloatingActionButton fab;
    private Animation hideCameraFab;
    private CameraController mCameraController;
    private ProgressBar mPbProcessing;
    SweetAlertDialog progressDialog;
    int mCount;
    View mView;
    ImageView btn_record_image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_front_camera_preview);
        mView=(View)findViewById(R.id.mView);
        btn_record_image=(ImageView) findViewById(R.id.btn_record_image);

        mView.setVisibility(View.GONE);

        fab = (FloatingActionButton) findViewById(R.id.btn_record);
        btn_record_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                btn_record_image.setVisibility(View.GONE);

                mCameraController.record();
                mCount=0;

                mView.setBackgroundColor(ContextCompat.getColor(FrontCameraPreviewActivity.this,R.color.white));
                mView.setVisibility(View.VISIBLE);

                new CountDownTimer(Constants.VIDEO_DURATION, 100) {

                    public void onTick(long millisUntilFinished) {

                        mCount++;

                        if(mCount%2 == 0)
                        {
                            mView.setVisibility(View.VISIBLE);

                        }
                        else
                        {
                            mView.setVisibility(View.GONE);
                        }
                        // mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
                        //here you can have your logic to set text to edittext
                    }

                    public void onFinish() {
                        mView.setVisibility(View.GONE);
                    }

                }.start();



            }
        });



        progressDialog = new SweetAlertDialog(FrontCameraPreviewActivity.this, SweetAlertDialog.PROGRESS_TYPE);
        mPbProcessing = (ProgressBar) findViewById(R.id.pb_processing);
        mPreviewFrame = (FrameLayout) findViewById(R.id.fl_camera_preview);

        View decorView = getWindow().getDecorView();
        // Hide the status bar.
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        );


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case CAMERA_PERMISSION_REQUEST_CODE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    this.initCamera();

                } else {
                    // TODO : display an error view
                }
            }
        }
    }

    @Override
    protected void onResume() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED&&
                ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {


            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE}, CAMERA_PERMISSION_REQUEST_CODE);


        } else {
            initCamera();
        }
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mCameraController != null) {
            mCameraController.release();
            mCameraController = null;
        }
    }

    private void initCamera() {
        if (mCameraController == null) {
            mCameraController = new CameraController(this);
            mCameraController.setCameraRecordListener(this);

            if (mCameraController.getCamera() == null) {
                Toast.makeText(this, R.string.camera_not_available, Toast.LENGTH_SHORT).show();
                // TODO : display an error view
            } else if (mPreview == null) {
                mPreview = new FrontCameraSurfaceView(this, mCameraController.getCamera(), CameraController.getFrontCameraInfo());
                mPreviewFrame.addView(mPreview);
            } else {
                // handle the onResume after background properly
                mPreview.setCamera(mCameraController.getCamera());
            }
        } else {
            mCameraController.getCamera();
        }
    }


    @Override
    public void onCameraRecordSuccess(final File file) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                fab.setVisibility(View.GONE);

                btn_record_image.setVisibility(View.GONE);
                mCameraController.release();
                mPreviewFrame.removeAllViews();
                mCameraController = null;
                mPbProcessing.setVisibility(View.GONE);

                progressDialog.getProgressHelper().setBarColor(ContextCompat.getColor(FrontCameraPreviewActivity.this, R.color.darkbutton));
                progressDialog.setTitleText("Processing...");
                progressDialog.setCancelable(false);
                progressDialog.show();
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG, "start");
                FFMpegVideoGenerator generator = new FFMpegVideoGenerator(FrontCameraPreviewActivity.this.getApplication());
                generator.setVideoGeneratorListener(FrontCameraPreviewActivity.this);
                generator.convert(file);
            }
        }).start();

    }

    @Override
    public void onCameraRecordFailure() {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(FrontCameraPreviewActivity.this, R.string.camera_not_available, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onVideoGenerated(String message, File generatedFile) {
        progressDialog.dismiss();
        Intent intent = new Intent(FrontCameraPreviewActivity.this, VideoPreviewActivity.class);
        intent.putExtra(VideoPreviewActivity.VIDEO_PATH, generatedFile.getAbsolutePath());


        startActivity(intent);

        recreate();
    }

    @Override
    public void onVideoGeneratedError(String message) {
        Log.e(TAG, message);
    }
}

分割视频和其他功能图像

<强> FFMpegVideoGenerator.java

import android.content.Context;

import android.util.Log;

import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException;

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Locale;

import wc.arjs.boomerang.controllers.VideoGenerator;
import wc.arjs.boomerang.utils.FileUtils;
import wc.arjs.boomerang.Constants;


public class FFMpegVideoGenerator extends Thread implements VideoGenerator {

    private static final String TAG = "VideoGenerator";
    private static final String TEMP_IMG_DIR = "imgs";
    private static final String METADATA_PREFIX = "met_";
    private static final String CROP_PREFIX = "crop_";

    private WeakReference<VideoGeneratorListener> mWeakListener;

    private String mWorkingDir;
    private String mFinalOutputDir;
    private FFmpeg mFFmpeg;

    public FFMpegVideoGenerator(Context ctx) {
        mFFmpeg = FFmpeg.getInstance(ctx);
        mWorkingDir = ctx.getCacheDir() + File.separator + TMP_DIR;
        mFinalOutputDir = ctx.getCacheDir() + File.separator + OUTPUT_DIR;
    }

    @Override
    public void setVideoGeneratorListener(VideoGeneratorListener listener) {
        mWeakListener = new WeakReference<>(listener);
    }

    @Override
    public void convert(final File inputFile) {
        FileUtils.createDirIfNeeded(mWorkingDir);
        FileUtils.createDirIfNeeded(mFinalOutputDir);

        try {
            mFFmpeg.loadBinary(new LoadBinaryResponseHandler() {
                @Override
                public void onSuccess() {
                    fixMetaData(inputFile);
                }

            });
        } catch (FFmpegNotSupportedException e) {
            Log.e(TAG, "not supported");
        }
    }


    private void fixMetaData(final File inputFile) {
        Log.d(TAG, "fixeMetaData");

        String c = "-y -i " + inputFile.getAbsolutePath() + " -metadata:s:v rotate=90 -codec copy "
                + mWorkingDir + File.separator + METADATA_PREFIX + inputFile.getName();

        String[] cmd = c.split(" ");
        try {
            mFFmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                @Override
                public void onFailure(String message) {
                    Log.e(TAG, message);
                    dispatchError(message);
                }

                @Override
                public void onSuccess(String message) {
                    Log.d(TAG, message);
                    File generated = new File(mWorkingDir +
                            File.separator + METADATA_PREFIX + inputFile.getName());
                    cropVideo(generated);
                }

            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            dispatchError(e.getMessage());
        }
    }

    private void cropVideo(final File inputFile) {
        String c = "-y -i " + inputFile.getAbsolutePath() + " -vf crop=" +
                Constants.VIDEO_ASPECT_RATIO + "*in_h:in_h -preset ultrafast " +
                mWorkingDir + File.separator + CROP_PREFIX + inputFile.getName();

        String[] cmd = c.split(" ");
        try {
            mFFmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                @Override
                public void onFailure(String message) {
                    Log.e(TAG, message);
                    dispatchError(message);
                }

                @Override
                public void onSuccess(String message) {
                    Log.d(TAG, message);
                    File generated = new File(mWorkingDir +
                            File.separator + CROP_PREFIX + inputFile.getName());
                    splitIntoImages(generated);
                }

            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            dispatchError(e.getMessage());
        }
    }

    private void splitIntoImages(final File inputFile) {
        Log.d(TAG, "splitIntoImages");

        final File tempImgsDir = new File(mWorkingDir + File.separator + TEMP_IMG_DIR);

        if (tempImgsDir.exists()) {
            FileUtils.deleteDirectory(tempImgsDir);
        }
        tempImgsDir.mkdir();

        String c = "-y -i " + inputFile.getAbsolutePath() +
                " -strict experimental -r 30 -qscale 1 -f image2 -vcodec mjpeg " +
                tempImgsDir.getAbsolutePath() + File.separator + "%03d.jpg";

        String[] cmd = c.split(" ");
        try {
            mFFmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                @Override
                public void onFailure(String message) {
                    Log.e(TAG, message);
                    dispatchError(message);
                }

                @Override
                public void onSuccess(String message) {
                    Log.d(TAG, message);
                    reverseImagesOrder(tempImgsDir);
                    assembleVideo(tempImgsDir);
                }

            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            dispatchError(e.getMessage());
        }
    }

    private void reverseImagesOrder(final File inputDirectory) {
        File[] files = inputDirectory.listFiles();
        Arrays.sort(files);
        int nbImages = files.length;
        if (nbImages <= 2) {
            dispatchError("Not enough images generated");
        }
        // start from before the last image and duplicate all the images in reverse order
        for (int i = nbImages - 2; i > 0; i--) {
            File img = files[i];
            if (img.exists()) {
                String copiedImg = inputDirectory.getAbsolutePath() + File.separator +
                        String.format(Locale.ENGLISH, "%03d", 2 * nbImages - i - 1) + ".jpg";
                Log.d(TAG, copiedImg);
                FileUtils.copyAndRenameInDirectory(img.getAbsolutePath(), copiedImg);
            } else {
                Log.e(TAG, "file not found : " + img.getAbsolutePath());
            }
        }
    }

    private void assembleVideo(final File inputDirectory) {
        Log.d(TAG, "assembleVideo");
        File containingFolder = new File(mFinalOutputDir);

        final File assembledVideo = new File(containingFolder.getAbsolutePath() + File.separator + FINAL_VIDEO_NAME);

        String[] cmdCreateMP4 = {
                "-framerate",
                "6",
                "-i",
                inputDirectory.getAbsolutePath() + File.separator +
                        "%03d.jpg",
                "-c:v",
                "libx264",
                "-profile:v",
                "high",
                "-crf",
                "20",
                "-pix_fmt",
                "yuv420p",
                assembledVideo.getAbsolutePath()};

        String c = "-framerate 70 -y -f image2 -loop 1 -i " + inputDirectory.getAbsolutePath() + File.separator +
                "%03d.jpg -r 30 -vcodec mpeg4 -b:v 2100k -t 4 " +
                assembledVideo.getAbsolutePath();

        String[] cmd = c.split(" ");
        try {
            mFFmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                @Override
                public void onFailure(String message) {
                    Log.e(TAG, message);
                    dispatchError(message);
                }

                @Override
                public void onSuccess(String message) {
                    Log.d(TAG, message);
                   dispatchSuccess(message, assembledVideo);

                    /*String root = Environment.getExternalStorageDirectory().toString();
                    File myDir = new File(root + "/boomerang/unsafe");
                    myDir.mkdirs();




                    String fname = "VID_"+ System.currentTimeMillis() +".mp4";
                    File file = new File (myDir, fname);
                    if (file.exists ())
                        file.delete ();
                    try {
                        InputStream in = new FileInputStream(assembledVideo.getAbsolutePath());
                        OutputStream out = new FileOutputStream(file);

                        // Copy the bits from instream to outstream
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        in.close();
                        out.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    File myDir1 = new File(root + "/boomerang/");





                    String fname1 = "VID_"+ System.currentTimeMillis() +".mp4";
                    File file1= new File (myDir1, fname1);
*/

                 //   concatenate(file.getAbsolutePath(),file.getAbsolutePath(),file1.getAbsolutePath());


                }

            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            dispatchError(e.getMessage());
        }
    }


    private static String generateList(String[] inputs) {
        File list;
        Writer writer = null;
        try {
            list = File.createTempFile("ffmpeg-list", ".txt");
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(list)));
            for (String input: inputs) {
                writer.write("file '" + input + "'\n");
                Log.d(TAG, "Writing to list file: file '" + input + "'");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "/";
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        Log.d(TAG, "Wrote list file to " + list.getAbsolutePath());
        return list.getAbsolutePath();
    }
    public void concatenate(String inputFile1, String inputFile2, final String outputFile) {
        Log.d(TAG, "Concatenating " + inputFile1 + " and " + inputFile2 + " to " + outputFile);
        String list = generateList(new String[] {inputFile1, inputFile2});

       /* String c = "ffmpeg -i "+inputFile1+" -qscale:v 1 intermediate1.mpg ffmpeg -i "+inputFile2+" -qscale:v 1 intermediate2.mpg cat intermediate1.mpg intermediate2.mpg > intermediate_all.mpg ffmpeg -i intermediate_all.mpg -qscale:v 2 "+
                outputFile;
*/


      /*  new String[] {
                "ffmpeg",
                "-f",
                "concat",
                "-i",
                list,
                "-c",
                "copy",
                outputFile
        }*/

        String c = "ffmpeg -i "+inputFile1+" -filter_complex [0]reverse[r];[0][r]concat,loop=5:250,setpts=N/25/TB " +
                outputFile;

        String[] cmd = c.split(" ");

        try {
            mFFmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

                @Override
                public void onFailure(String message) {
                    Log.e(TAG, message);
                    //dispatchError(message);
                }

                @Override
                public void onSuccess(String message) {
                    Log.d("@Success", "Ho gaya");
                    dispatchSuccess(message, new File(outputFile));



                }

            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            //dispatchError(e.getMessage());
        }

/*

        Videokit vk = new Videokit();
        vk.run(new String[] {
                "ffmpeg",
                "-f",
                "concat",
                "-i",
                list,
                "-c",
                "copy",
                outputFile
        });
*/


    }

    private void dispatchSuccess(String message, File file) {
        if (mWeakListener != null && mWeakListener.get() != null) {
            mWeakListener.get().onVideoGenerated(message, file);
        }
    }

    private void dispatchError(String message) {
        if (mWeakListener != null && mWeakListener.get() != null) {
            mWeakListener.get().onVideoGeneratedError(message);
        }
    }
}

您可以在此路径上拍摄拆分图像,即临时缓存(android / data / ....)保存您也可以根据需要更改路径,makedirs并保存要保存的图像

tempImgsDir.getAbsolutePath() + File.separator + "%03d.jpg"

<强> Cameracontroller

import android.content.Context;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.FileObserver;
import android.util.Log;

import java.io.File;
import java.io.IOException;

import wc.arjs.boomerang.Constants;

/**
 * Created by Sylvain on 26/11/2016.
 */

public class CameraController {
    private final static String TAG = "CameraController";


    public interface CameraRecordListener {
        void onCameraRecordSuccess(File file);
        void onCameraRecordFailure();
    }

    private CameraRecordListener mCameraRecordListener;
    private Camera mCamera;
    private MediaRecorder mMediaRecorder;
    private boolean isRecording;
    private Context mContext;

    private static Camera GetFrontCameraInstance() {
        Camera c = null;
        int cameraId = GetFrontCameraId();
        try {
            c = Camera.open(cameraId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return c; // returns null if camera is unavailable
    }

    private static int GetFrontCameraId() {
        Camera.CameraInfo info = new Camera.CameraInfo();
        int count = Camera.getNumberOfCameras();

        for (int i = 0; i < count; i++) {
            Camera.getCameraInfo(i, info);
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                return i;
            }
        }
        return -1;
    }

    public static Camera.CameraInfo getFrontCameraInfo(){
        Camera.CameraInfo info = new Camera.CameraInfo();
        int count = Camera.getNumberOfCameras();

        for (int i = 0; i < count; i++) {
            Camera.getCameraInfo(i, info);
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                return info;
            }
        }
        return info;
    }

    public CameraController(Context ctx){
        this.mContext = ctx;
        mCamera = GetFrontCameraInstance();

        Camera.Parameters params = mCamera.getParameters();
        params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
      /*  params.setExposureCompensation(params.getMaxExposureCompensation());

        if(params.isAutoExposureLockSupported()) {
            params.setAutoExposureLock(false);
        }*/

        params.setRecordingHint(true);
        mCamera.setParameters(params);
        isRecording = false;
    }

    public void setCameraRecordListener(CameraRecordListener cameraRecordListener){
        this.mCameraRecordListener = cameraRecordListener;
    }

    class RecordedFileObserver extends FileObserver {
        private File output;
        public RecordedFileObserver(File output, int mask) {
            super(output.getAbsolutePath(), mask);
            this.output  = output;
        }

        public void onEvent(int event, String path) {
            if(event == FileObserver.CLOSE_WRITE){
             if(mCameraRecordListener!=null){
                 mCameraRecordListener.onCameraRecordSuccess(output);
             }
            }
        }
    }

    public void record(){

        final File output = getOutputMediaFile();
        if (prepareVideoRecorder(output)) {
            // Camera is available and unlocked, MediaRecorder is prepared,
            // now you can start recording
            mMediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
                @Override
                public void onInfo(MediaRecorder mr, int what, int extra) {
                    if(what==MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED){
                        if (isRecording) {

                            RecordedFileObserver fb = new RecordedFileObserver(output, FileObserver.CLOSE_WRITE);
                            fb.startWatching();

                            // stop recording and release camera
                            mMediaRecorder.stop();  // stop the recording
                            releaseMediaRecorder(); // release the MediaRecorder object
                            mCamera.lock();         // take camera access back from MediaRecorder
                            isRecording = false;
                        }
                    }else{
                        if(mCameraRecordListener!=null){
                            mCameraRecordListener.onCameraRecordFailure();
                        }
                    }
                }
            });
            mMediaRecorder.start();
            isRecording = true;

        } else {
            releaseMediaRecorder();
            if(mCameraRecordListener!=null){
                mCameraRecordListener.onCameraRecordFailure();
            }
        }

    }

    public void release(){
        this.releaseMediaRecorder();
        this.releaseCamera();
    }

    private boolean prepareVideoRecorder(File output) {

        mMediaRecorder = new MediaRecorder();

        // store the quality profile required
        CamcorderProfile profile = CamcorderProfile.get(CameraController.GetFrontCameraId(), CamcorderProfile.QUALITY_480P);

        // Step 1: Unlock and set camera to MediaRecorder
        mCamera.unlock();
        mMediaRecorder.setCamera(mCamera);
        // Step 2: Set sources
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
        mMediaRecorder.setOutputFormat(profile.fileFormat);
        mMediaRecorder.setVideoEncoder(profile.videoCodec);
        mMediaRecorder.setVideoEncodingBitRate(profile.videoBitRate);
        mMediaRecorder.setVideoFrameRate(profile.videoFrameRate);
        mMediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
        mMediaRecorder.setVideoSize(640, 480);
       // recorder.setVideoSize(640, 480);
       // recorder.setVideoFrameRate(16); //might be auto-determined due to lighting
       // recorder.setVideoEncodingBitRate(3000000);
       // recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);// MPEG_4_SP
      //  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);


        // Step 4: Set output file
        mMediaRecorder.setOutputFile(output.toString());
        // Set the duration
        mMediaRecorder.setMaxDuration(Constants.VIDEO_DURATION);

        // Step 6: Prepare configured MediaRecorder
        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
            releaseMediaRecorder();
            return false;
        } catch (IOException e) {
            Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
            releaseMediaRecorder();
            return false;
        }
        return true;
    }

    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
    }

    private void releaseMediaRecorder() {
        if (mMediaRecorder != null) {
            mMediaRecorder.reset();   // clear recorder configuration
            mMediaRecorder.release(); // release the recorder object
            mMediaRecorder = null;
            mCamera.lock();           // lock camera for later use
        }
    }

    private File getOutputMediaFile() {
        return new File(mContext.getFilesDir().getPath() + File.separator + Constants.VIDEO_TEMP_NAME);
    }


    public Camera getCamera() {
        return mCamera;
    }
}

<强>常量

公共类常量{

public final static double VIDEO_ASPECT_RATIO = 0.6; // 1:5
public final static int VIDEO_DURATION = 2000;
public final static String VIDEO_TEMP_NAME = "VID_TEMP.mp4";

}