Android MediaRecorder with FileDescriptor: save a mp4 video on sd card
<p>I want to record a video and save it on the sd card. Because I want to do some modification on the video (later! just now I want to save the mp4 video) I decided to work with an FileDescriptor.
Here I set up the MediaRecorder:</p>

<pre><code> mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
 mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
 mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
        mNextVideoAbsolutePath = getVideoFilePath(getActivity());
    }

//the output file is a FileDescriptor
  mMediaRecorder.setOutputFile(getStreamFd());

 mMediaRecorder.setVideoEncodingBitRate(10000000);
 mMediaRecorder.setVideoFrameRate(30);
 mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
 mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
 mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
</code></pre>

<p>the target where the video is stored:</p>

<pre><code>private String getVideoFilePath(Context context) {
    return context.getExternalFilesDir(null).getAbsolutePath() + "/"
           + System.currentTimeMillis() + ".mp4";
}
</code></pre>

<p>FileDescriptor:</p>

<pre><code>private FileDescriptor getStreamFd() {
    ParcelFileDescriptor[] pipe=null;

    try {
        pipe=ParcelFileDescriptor.createPipe();
        new TransferThread(new ParcelFileDescriptor.AutoCloseInputStream(pipe[0]),
        new FileOutputStream(mNextVideoAbsolutePath)).start();
    }
    catch (IOException e) {
        Log.e(getClass().getSimpleName(), "Exception opening pipe", e);
    }

    return(pipe[1].getFileDescriptor());
}
</code></pre>

<p>In the transfer thread the video is saved:</p>

<pre><code>static class TransferThread extends Thread {
    InputStream in;
    FileOutputStream out;

    TransferThread(InputStream in, FileOutputStream out) {
        this.in=in;
        this.out=out;
    }

    @Override
    public void run() {
        byte[] buf=new byte[8192];
        int len;

        try {
            while ((len=in.read(buf)) &gt; 0) {
                out.write(buf, 0, len);
            }

            in.close();

            out.flush();
            out.getFD().sync();
            out.close();
        }
        catch (IOException e) {
            Log.e(getClass().getSimpleName(),
                    "Exception transferring file", e);
        }

    }
}
</code></pre>

<p>When I run the application, then a video is created. The video has a mp4 extentions and the size of the video seems to be alright. </p>

<p><strong>But I can not play the video.</strong> </p>

<p>I tried different VideoEncoders or OutputFormats, but without any success. </p>

<p>Theres a similar question: <a href="https://stackoverflow.com/questions/12894976/anyone-have-mediarecorder-working-with-parcelfiledescriptor-and-createpipe">Anyone Have MediaRecorder Working with ParcelFileDescriptor and createPipe()?</a>
But it does not match my problem. I have set all necessary permissions in the manifest.</p>

<p>Are there any problems with an incorrect header of the video?</p>