我尝试按照FFmpeg Android Java和ffmpeg4android的教程进行学习
但是即使依赖关系也无法解决。请帮助如何轻松将ffmpeg集成到android studio中,以便在视频上叠加图像。
答案 0 :(得分:1)
试试这个,我用这个对我有用:
import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;
导入com.github.hiteshsondhi88.libffmpeg.FFmpeg; 导入com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler; 导入com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException; 导入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>
使用此功能为我使用FFMPEG库,我已经在顶部提供了导入信息 这里是图书馆 https://github.com/WritingMinds/ffmpeg-android-java