0

Despite many searches, I haven't been able to develop a Android prototype able to film a video before extracting its audio as .wav in a separate activity.

I have developed so far a simple filming activity which relies on Android's Camera application. My strategty was to put the video's Uri as Extra to the next activity, before using FFMPEG, but I can't make the transition between Uri and FFMPEG. Indeed, I'm a fresh Android Studio beginner, so I still am not sure about what concept to use.

Here's my code for the video recording activity.

import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; 
import java.nio.channels.FileChannel;

import static java.security.AccessController.getContext;


public class RecordActivity extends Activity{

static final int REQUEST_VIDEO_CAPTURE = 0;

VideoView mVideoView = null;
Uri videoUri = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mVideoView = (VideoView) findViewById(R.id.videoVieww);
    setContentView(R.layout.activity_record);

    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

    Toast.makeText(RecordActivity.this,         String.valueOf(Build.VERSION.SDK_INT) , Toast.LENGTH_SHORT).show();

    takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
    if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
    }

}


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
            videoUri = intent.getData();

            Intent intentForFilterActivity = new Intent(RecordActivity.this, FilterActivity.class);
            intentForFilterActivity.putExtra("VideoToFilter", videoUri.getPath());
            startActivity(intentForFilterActivity);

        }
    }
}

Here's the the code for the audio extraction activity. It is called "FilterActivity", as its final aim is to filter outdoor noise using additional functions. I'm using WritingMinds' implementation of FFMPEG. https://github.com/WritingMinds/ffmpeg-android-java

 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.test.ActivityUnitTestCase;
 import android.widget.Toast;

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



public class FilterActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_filter);

    Intent intentVideo = getIntent();
    String pathIn = intentVideo.getStringExtra("VideoToFilter");

    FFmpeg ffmpeg = FFmpeg.getInstance(FilterActivity.this);
    try {
        String[] cmdExtract = {"-i " + pathIn + " extracted.wav"};
        ffmpeg.execute(cmdExtract, new ExecuteBinaryResponseHandler() {

            @Override
            public void onStart() {}

            @Override
            public void onProgress(String message) {}

            @Override
            public void onFailure(String message) {
                Toast.makeText(FilterActivity.this, "Failure !", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSuccess(String message) {}

            @Override
            public void onFinish() {}
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
    }
}


}

and I always get the "Failure !" message.

Some parts of the code may look extremely bad. As as written previously, I'm a real Android Studio beginner.

Do you have any correction that could work ? Or even just a strategy ?

Thank you in advance !

1 Answers1

0

The way that the WritingMinds ffmpeg implementation works is that it provides a warper around the FFMPEG command line tool.

This means that you can use the same syntax as the standard ffmpeg command line to manipulate video. The command you use is set in the cmdExtract string in your code above.

In your case you have I think just taken the example command which is simply '-i' and which will only get info for the video. You need to add the correct ffmpeg syntax to extract audio here.

You can search quite easily for the ffmpeg syntax you need - this is the advantage of the wrapper approach as it allows you leverage the same syntax and wide knowledge around the cmd line tool. For example, this Stackoverflow question (ffmpeg to extract audio from video) has some examples, such as:

ffmpeg -i input-video.avi -vn -acodec copy output-audio.aac

In your case you to use this above syntax you would set cmdExtract (substituting your input and output file paths:

String[] cmdExtract = {"-i " + pathIn + " -vn -acodec copy " + pathOut};

You also seem to be getting a failure message before you even get to this point - given how simple your code is this may be just a file path issue, but the first step to help you figure this out would be to print the message that the library returns you which is being hidden above - for example, change it to this:

            @Override
            public void onFailure(String message) {
                Toast.makeText(FilterActivity.this, "Failure: " + message, Toast.LENGTH_LONG).show();
            }
Community
  • 1
  • 1
Mick
  • 24,231
  • 1
  • 54
  • 120