gpt4 book ai didi

android - FFmpeg4Android : How to select videos from gallery?

转载 作者:行者123 更新时间:2023-12-04 23:16:30 25 4
gpt4 key购买 nike

我使用 FFmpeg4Android 库创建了一个应用程序。我想使用 ffmpeg 在视频中添加水印。添加水印的命令工作正常,但编码在命令中具有预先固定的视频名称。我想从图库文件夹中选择视频,然后为视频添加水印。我不知道如何添加使用 FFmpeg 选择视频的 Intent 。我试图添加这样的 Intent ,用 PICK_FROM_GALLERY Intent 替换 in.mp4:

 private void runTranscodingUsingLoader() {
Log.i(Prefs.TAG, "runTranscodingUsingLoader started...");

PowerManager powerManager = (PowerManager)ProgressBarExample.this.getSystemService(Activity.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VK_LOCK");
Log.d(Prefs.TAG, "Acquire wake lock");
wakeLock.acquire();

Intent intent = new Intent();

intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_GALLERY);


String[] complexCommand = {"ffmpeg","-y" ,"-i", String.valueOf(PICK_FROM_GALLERY),"-strict","experimental",
"-vf", "movie=/sdcard/videokit/watermark.png [watermark];" +
" [in][watermark] overlay=main_w-overlay_w-10:10 [out]","-s",
"320x240","-r", "30", "-b", "15496k", "-vcodec", "mpeg4","-ab",
"48000", "-ac", "2", "-ar", "22050", "/sdcard/videokit/out1.mp4"};

它会打开图库,但水印命令不会在所选视频中执行。有人可以帮我解决这个问题。我找不到有关此的适当引用/示例。我的完整编码如下。
public class ProgressBarExample extends Activity  {

public ProgressDialog progressBar;

String workFolder = null;
String demoVideoFolder = null;
String demoVideoPath = null;
String vkLogPath = null;
LoadJNI vk;
private final int STOP_TRANSCODING_MSG = -1;
private final int FINISHED_TRANSCODING_MSG = 0;
private boolean commandValidationFailedFlag = false;
private static final int PICK_FROM_GALLERY = 1;

private void runTranscodingUsingLoader() {
Log.i(Prefs.TAG, "runTranscodingUsingLoader started...");

PowerManager powerManager = (PowerManager)ProgressBarExample.this.getSystemService(Activity.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VK_LOCK");
Log.d(Prefs.TAG, "Acquire wake lock");
wakeLock.acquire();




String[] complexCommand = {"ffmpeg","-y" ,"-i", "/sdcard/videokit/in.mp4","-strict","experimental",
"-vf", "movie=/sdcard/videokit/watermark.png [watermark];" +
" [in][watermark] overlay=main_w-overlay_w-10:10 [out]","-s",
"320x240","-r", "30", "-b", "15496k", "-vcodec", "mpeg4","-ab",
"48000", "-ac", "2", "-ar", "22050", "/sdcard/videokit/out1.mp4"};
///////////////////////////////////////////////////////////////////////


vk = new LoadJNI();
try {
// running complex command with validation
vk.run(complexCommand, workFolder, getApplicationContext());

// running without command validation
//vk.run(complexCommand, workFolder, getApplicationContext(), false);

// running regular command with validation
//vk.run(GeneralUtils.utilConvertToComplex(commandStr), workFolder, getApplicationContext());

Log.i(Prefs.TAG, "vk.run finished.");
// copying vk.log (internal native log) to the videokit folder
GeneralUtils.copyFileToFolder(vkLogPath, demoVideoFolder);

} catch (CommandValidationException e) {
Log.e(Prefs.TAG, "vk run exeption.", e);
commandValidationFailedFlag = true;

} catch (Throwable e) {
Log.e(Prefs.TAG, "vk run exeption.", e);
}
finally {
if (wakeLock.isHeld()) {
wakeLock.release();
Log.i(Prefs.TAG, "Wake lock released");
}
else{
Log.i(Prefs.TAG, "Wake lock is already released, doing nothing");
}
}

// finished Toast
String rc = null;
if (commandValidationFailedFlag) {
rc = "Command Vaidation Failed";
}
else {
rc = GeneralUtils.getReturnCodeFromLog(vkLogPath);
}
final String status = rc;
ProgressBarExample.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(ProgressBarExample.this, status, Toast.LENGTH_LONG).show();
if (status.equals("Transcoding Status: Failed")) {
Toast.makeText(ProgressBarExample.this, "Check: " + vkLogPath + " for more information.", Toast.LENGTH_LONG).show();
}
}
});
}


@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(Prefs.TAG, "onCreate ffmpeg4android ProgressBarExample");

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

demoVideoFolder = Environment.getExternalStorageDirectory().getAbsolutePath() + "/videokit/";
demoVideoPath = demoVideoFolder + "in.mp4";

Log.i(Prefs.TAG, getString(R.string.app_name) + " version: " + GeneralUtils.getVersionName(getApplicationContext()) );

Button invoke = (Button)findViewById(R.id.invokeButton);
invoke.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Log.i(Prefs.TAG, "run clicked.");
runTranscoding();
}
});

workFolder = getApplicationContext().getFilesDir() + "/";
Log.i(Prefs.TAG, "workFolder (license and logs location) path: " + workFolder);
vkLogPath = workFolder + "vk.log";
Log.i(Prefs.TAG, "vk log (native log) path: " + vkLogPath);
GeneralUtils.copyLicenseFromAssetsToSDIfNeeded(this, workFolder);
GeneralUtils.copyDemoVideoFromAssetsToSDIfNeeded(this, demoVideoFolder);
int rc = GeneralUtils.isLicenseValid(getApplicationContext(), workFolder);
Log.i(Prefs.TAG, "License check RC: " + rc);
}



private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(Prefs.TAG, "Handler got message");
if (progressBar != null) {
progressBar.dismiss();

// stopping the transcoding native
if (msg.what == STOP_TRANSCODING_MSG) {
Log.i(Prefs.TAG, "Got cancel message, calling fexit");
vk.fExit(getApplicationContext());


}
}
}
};

public void runTranscoding() {
progressBar = new ProgressDialog(ProgressBarExample.this);
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setTitle("FFmpeg4Android Direct JNI");
progressBar.setMessage("Press the cancel button to end the operation");
progressBar.setMax(100);
progressBar.setProgress(0);

progressBar.setCancelable(false);
progressBar.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.sendEmptyMessage(STOP_TRANSCODING_MSG);
}
});

progressBar.show();

new Thread() {
public void run() {
Log.d(Prefs.TAG,"Worker started");
try {
//sleep(5000);
runTranscodingUsingLoader();
handler.sendEmptyMessage(FINISHED_TRANSCODING_MSG);

} catch(Exception e) {
Log.e("threadmessage",e.getMessage());
}
}
}.start();

// Progress update thread
new Thread() {
ProgressCalculator pc = new ProgressCalculator(vkLogPath);
public void run() {
Log.d(Prefs.TAG,"Progress update started");
int progress = -1;
try {
while (true) {
sleep(300);
progress = pc.calcProgress();
if (progress != 0 && progress < 100) {
progressBar.setProgress(progress);
}
else if (progress == 100) {
Log.i(Prefs.TAG, "==== progress is 100, exiting Progress update thread");
pc.initCalcParamsForNextInter();
break;
}
}

} catch(Exception e) {
Log.e("threadmessage",e.getMessage());
}
}
}.start();
}

}

任何帮助/指导都会非常有帮助。谢谢你。

最佳答案

FFmpeg 不用于从画廊或任何地方提取视频。但它用于处理给定路径的视频,因此您需要通过 Intent 使用默认的android视频选择器根据用户选择来选择视频。在 onActivityResult(...) 之后方法您将获得该选定视频的路径,然后在获得此视频路径后,您可以将此路径传递给适当的 ffmpeg 命令进行视频处理。

关于android - FFmpeg4Android : How to select videos from gallery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34082867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com