Ibrahim El_Khatib
Ibrahim El_Khatib

Reputation: 483

Convert mp4 videos to support streaming in android

I am developing android application that record a video in mp4 format and upload it to a server, then other users can view that video.

My problem is that the mp4 is not supporting streaming which I need in my app, instead it needs to download all the video to start playing it.

I googled the issue and I found that mp4 that supports streaming have the faststart property which is putting the moov atom in the beginning of the video file.

My question is how to achieve that in android if it can be?? if not can I do it in the server and how?

My code to record video is :

if (requestCode == REQUEST_VIDEO_CAPTURE ) {


        try
        {
            Log.e("videopath","videopath");
        AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
        FileInputStream fis = videoAsset.createInputStream();

        File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
        if (!root.exists()) {
              root.mkdirs();
        }
        File file;
        file=new File(root,"android_"+System.currentTimeMillis()+".mp4" );
        videoUri = Uri.fromFile(file);
        FileOutputStream fos = new FileOutputStream(file);

        byte[] buf = new byte[1024];
        int len;
        while ((len = fis.read(buf)) > 0) {
            fos.write(buf, 0, len);
        }       
        fis.close();
        fos.close();
      } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        return;
    }

Upvotes: 3

Views: 3702

Answers (1)

Mick
Mick

Reputation: 25491

If you have the choice for your scenario I would suggest doing the modification to the video on the server, simply because you generally have more 'horsepower' on the server and it has less impact on your users.

I think you'll also probably find it easier - while you can integrate ffmpeg into your Android application, it is not trivial and you will have to be careful to keep it updated with new releases of your app.

One relatively simple way on the server side to do this is to run a job or task which invokes ffmpeg using the command line program to do the conversion - it is maybe not as efficient as using the ffmpeg C libraries directly but it had the advantage of being easy to update when ffmpeg itself changes, and also the advantage that you will generally find much more discussion and help on using ffmpeg from the command line.

The options etc can change over time so this can go out of date but the following format (from this stackOverflow answer: https://stackoverflow.com/a/14172629/334402) worked for me previously:

ffmpeg -i <input> -c:v libx264 -profile:v baseline -c:a libfaac -ar 44100 -ac 2 -b:a 128k -movflags faststart output.mp4

If you do want to do it on the Android app you can use the same format if you use one of the Android wrapper libraries, or you can write your own fffmpeg wrapper library. It is also be possible to use ffmpeg C libraries directly in Android, although I have not personally down this.

The following are a good place to start if you want to use an Android ffmpeg wrapper:

In the second link above, make sure in particular you note the comments about calling ffmpeg twice. This can be tricky as it seems to work initially but will crash with multiple runs.

Upvotes: 2

Related Questions