Simran Chawla
Simran Chawla

Reputation: 21

How to using ffmpeg overlay logo on video in android app while downloading?

I tried following the tutorial for FFmpeg Android Java and ffmpeg4android

But even the dependency is not resolved.Please help how to easily integrate ffmpeg in android studio for overlaying image on video.

Upvotes: 0

Views: 2087

Answers (1)

Jins Lukose
Jins Lukose

Reputation: 707

Try this i used this worked for me:

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;

public class Home extends Activity {

private static final String TAG = Home.class.getSimpleName();

FFmpeg ffmpeg;
File imageFile;
File f;
File myDirectory = new File(Environment.getExternalStorageDirectory() + "/Your_directory/");
File outputDirectory;
@Bind(R.id.command)
EditText commandEditText;
@Bind(R.id.command_output)
LinearLayout outputLayout;
String paths = "";
String path2 = "";
private ProgressDialog progressDialog;
String fileName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    ButterKnife.bind(this);
    isStoragePermissionGranted();
    if (getIntent().getStringExtra("name") != null) {
        fileName = getIntent().getStringExtra("name");
        outputDirectory = new File(Environment.getExternalStorageDirectory() + "/Your_dir/" + fileName + ".mp4");
    } else {
        outputDirectory = new File(Environment.getExternalStorageDirectory() + "/Your_dir/test" + System.currentTimeMillis() + ".mp4");

    }

    commandEditText.setEnabled(false);

    paths = "path of video";


    ffmpeg = FFmpeg.getInstance(this);
    loadFFMpegBinary();
    initUI();


    if (!myDirectory.exists()) {
        myDirectory.mkdirs();
    }

    f = new File(Environment.getExternalStorageDirectory() + "/your_dir/trial.mp3");
    if (!f.exists()) try {

        InputStream is = getAssets().open("trial.mp3");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();


        FileOutputStream fos = new FileOutputStream(f);
        fos.write(buffer);
        fos.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    imageFile = new File(Environment.getExternalStorageDirectory() + "/your_path/newlogo.png");
    if (!imageFile.exists()) try {

        InputStream is = getAssets().open("newlogo.png");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();


        FileOutputStream fos = new FileOutputStream(imageFile);
        fos.write(buffer);
        fos.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    //video watermark
    String[] cmdd = {"-i", "" + paths, "-i", "" + imageFile.getPath(), "-filter_complex", "overlay=10:main_h-overlay_h-10", outputDirectory.getPath()};
    String[] command = cmdd;
    execFFmpegBinary(command);
}
private void loadFFMpegBinary() {
    try {
        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
            @Override
            public void onFailure() {
                showUnsupportedExceptionDialog();
            }
        });
    } catch (FFmpegNotSupportedException e) {
        showUnsupportedExceptionDialog();
    }
}
 private void execFFmpegBinary(final String[] command) {
    try {
        ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
            @Override
            public void onFailure(String s) {
                addTextViewToLayout("FAILED with output : " + s);
            }

            @Override
            public void onSuccess(String s) {
                addTextViewToLayout("SUCCESS with output : " + s);
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(outputDirectory.getPath()));
                intent.setDataAndType(Uri.parse(outputDirectory.getPath()), "video/mp4");
                startActivity(intent);
                finish();
            }

            @Override
            public void onProgress(String s) {
                Log.d(TAG, "Started command : ffmpeg " + command);
                addTextViewToLayout("progress : " + s);
                progressDialog.setMessage("Processing\n" + s);
                //  Log.d(TAG, "progress still : ffmpeg " + progressDialog.getProgress());
            }

            @Override
            public void onStart() {
                outputLayout.removeAllViews();
                Log.d(TAG, "Started command : ffmpeg " + command);
                progressDialog.setMessage("Processing...");
                progressDialog.show();
            }

            @Override
            public void onFinish() {
                Log.d(TAG, "Finished command : ffmpeg " + command);
                progressDialog.dismiss();
                MediaScannerConnection.scanFile(Home.this,
                        new String[]{outputDirectory.toString()}, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, Uri uri) {
                                //      Toast.makeText(Home.this,path+""+uri,Toast.LENGTH_LONG).show();
                                Log.i("ExternalStorage", "Scanned " + path + ":");
                                Log.i("ExternalStorage", "-> uri=" + uri);
                            }
                        });
            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // do nothing for now
    }
}
private void addTextViewToLayout(String text) {
    TextView textView = new TextView(Home.this);
    textView.setText(text);
    outputLayout.addView(textView);
}

private void showUnsupportedExceptionDialog() {
    new AlertDialog.Builder(Home.this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setCancelable(false)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Home.this.finish();
                }
            })
            .create()
            .show();

}

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/background_video_color"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".Home"
android:orientation="vertical">

<EditText
    android:id="@+id/command"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/black"
    android:minLines="3"
    android:background="@null"
    android:gravity="start" />



<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/command_output"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"/>
</ScrollView>

USE THIS IT WORK FOR ME USE FFMPEG LIBRARY ,I HAVE PROVIDED THE IMPORTS AT THE TOP HERE IS THE LIBRARY https://github.com/WritingMinds/ffmpeg-android-java

Upvotes: 1

Related Questions