简体   繁体   English

下载时如何在Android应用中的视频上使用ffmpeg叠加徽标?

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

I tried following the tutorial for FFmpeg Android Java and ffmpeg4android 我尝试按照FFmpeg Android Javaffmpeg4android的教程进行操作

But even the dependency is not resolved.Please help how to easily integrate ffmpeg in android studio for overlaying image on video. 但即使依赖关系也无法解决。请帮助如何轻松将ffmpeg集成到android studio中以在视频上叠加图像。

Try this i used this worked for me: 试试这个,我用它为我工作:

import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;

import com.github.hiteshsondhi88.libffmpeg.FFmpeg; 导入com.github.hiteshsondhi88.libffmpeg.FFmpeg; import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler; 导入com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler; import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException; 导入com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException; import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException; 导入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 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 使用此功能为我使用FFMPEG库,我已经在顶部提供了导入,这里是库https://github.com/WritingMinds/ffmpeg-android-java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM