Getting exception while trying to run ffmpeg via command line in android - android

I want to use ffmpeg via command line arguments in android application.For this purpose:
I have cross-compiled the ffmpeg lib and got the libffmpeg.so
I have stored libffmpeg.so in files directory of the app.
This is the code i am using:
public class MainActivity extends Activity {
Process p;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] cmd =new String[4];
cmd[0]="/data/data/com.example.ffmpegnew/files/libffmpeg";
cmd[1]="-i";
cmd[2]="mnt/sdcard/music/baba.mp4";
cmd[3]="mnt/sdcard/music/outfile.mp4";
p = Runtime.getRuntime().exec(cmd,null, new File("/data/data/com.example.ffmpegnew/files"));
}
catch(Exception e)
{
System.out.println("exception"+e);
}
}
}
This is the exception i am getting:
09-17 13:47:01.679: I/System.out(3752): exceptionjava.io.IOException: Error running exec(). Command: [/data/data/com.example.ffmpegnew/files/libffmpeg.so, -i, mnt/sdcard/music/baba.mp4, mnt/sdcard/music/outfile.mp4] Working Directory: /data/data/com.example.ffmpegnew/files Environment: null
Please tell me how to solve this problem.Thanks in advance.

I guess you are trying to run an .so file with this Linux command:
/data/data/com.example.ffmpegnew/files/libffmpeg.so -i mnt/sdcard/music/baba.mp4 mnt/sdcard/music/outfile.mp4
Did you actually mean running ffmpeg executable file (see man ffmpeg)?
ffmpeg -i mnt/sdcard/music/baba.mp4 mnt/sdcard/music/outfile.mp4
I think the first step might be making your command, then your code snippet run on regular Linux, then moving it to Android.
PS. In any event, the leading "/" is missing from "mnt/..."; should be "/mnt/..."
PPS. And other discussions might be helpful 1

Related

Jenkins pipeline job executing commands in tmp folder

MacOS Monterey version 12.4
I'm trying to run a simple pipeline script on my jenkins job
pipeline {
agent any
stages {
stage('Build') {
steps {
sh('emulator -list-avds')
}
}
}
But it throws an error:
/Users/<my_username>/.jenkins/workspace/<my_job_name>#tmp/durable-22217e91/script.sh: line 1: emulator: command not found
My question is: why is it executing commands in the tmp folder? Anything "emulator" related does work when I run commands via terminal.
Following this answer, I've confirmed I'm in the correct dir
Why Jenkins mounts a temporary volume in addition to the workspace?
You are getting this error because the emulator executable is not set in the PATH. Try something like the below.
Try setting your PATH variable to add emulator executable.
environment {
PATH = "/PATH_EMULATOR/bin:${env.PATH}"
}
or something like the below.
withEnv(["PATH+EMULATOR=/PATH_EMULATOR/bin"]) {
sh('emulator -list-avds')
}
or you can also use the full qualified path to the executable
sh('/PATH_TO_EMULATOR/bin/emulator -list-avds')

Android FFMPEG: Could not execute the ffmpeg from Java code

I am working on an Android app where I want to create a video from a
list of static images. After doing some search on internet, it made me
realized that using "FFMPEG" is the way to go in getting this thing
done. So I got hold of this site:
https://github.com/guardianproject/android-ffmpeg-java from where I
downloaded the C library and the Java wrapper. I was able to compile the
C library - of course not the way the instruction was laid out - but
still I was able to get "ffmpeg" executable under
/external/android-ffmpeg/ffmpeg directory. I copied that executable in
my current directory and then copied it to a directory under Android
where my app can access it. Then I called the provided Java wrapper but
I am seeing some errors in the log file like follows:
08-13 11:55:37.848: D/FFMPEG(29598): /data/data/com.sample/app_bin/ffmpeg -y -loop 1 -i /storage/emulated/0/usersnapshot/ffmpeg/image%03d.jpg -r 25 -t 2 -qscale 5 /storage/emulated/0/video/snapshot-video.mp4
08-13 11:55:37.898: I/ShellCallback : shellOut()(29598): /data/data/com.sample/app_bin/ffmpeg[1]: syntax error: '(' unexpected
08-13 11:55:37.938: I/ShellCallback : processComplete()(29598): 1
And following is the code snippet (where targetDirectoryForFFMPEG = directory where the images are stored):
FfmpegController ffmpegController = new FfmpegController(this, targetDirectoryForFFMPEG);
String out = videoOutPutFile.getPath();
MediaDesc mediaIn = new MediaDesc();
mediaIn.path = targetDirectoryForFFMPEG+"/image%03d.jpg";
mediaIn.videoFps = "25";
ffmpegController.convertImageToMP4(mediaIn, 2, out,new ShellCallback() {
#Override
public void shellOut(String shellLine) {
Log.i("ShellCallback : shellOut()", shellLine);
}
#Override
public void processComplete(int exitValue) {
Log.i("ShellCallback : processComplete()", exitValue+"");
}
});
Has anybody implemented this before? If yes, can you point me to what am I doing incorrect? I will provide more information if needed.
Do you have root on the device?
Mount '/data' and then enter your same 'ffmpeg' command in the shell and see whether the error is the same.
Try using the shell to test out different command expressions.
Try 'ffmpeg' alone and with just one input file. See whether those commands produce expected output.
My wild guess would be that there is an issue with calling 'ffmpeg.main()' that relates to the details of your build.
Your ffmpeg might not be compiled properly for the arm. I was getting the same error when not using a correctly compiled ffmpeg.

bambuser ffmpeg on Android

Basically, I need to combine(append) audio files in android. This is required to perform pause / resume function for a voice recorder.
I have successfully compiled bambuser ffmpeg on Android with the following.
1. Oracle Virtual Box
2. Ubuntu 12.04 x86
3. android-ndk r8
4. Archive for client versions 1.3.7 to 1.6.6
(from bambuser http://bambuser.com/opensource)
I have changed the package name in build.sh to suit my package name.
After building, I have got the ffmpeg from build folder with the following structure
ffmpeg
-armeabi
-armeabi-v7a
I have copied the ffmpeg folder into my project/jni folder in my windows machine.
Created a native.c file to include the necessary libs.
Made ndk-build. Got the .so file.
When I try to do this in android activity,
try {
System.loadLibrary("FFmpegTest");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
File dir = new File("/mnt/sdcard");
Process p=Runtime.getRuntime().exec("ffmpeg -i test.wav test1.wav",null,dir);
try {
p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
I get the following error.
05-31 11:57:53.532: D/dalvikvm(278): Trying to load lib /data/data/com.example.ffmpegtest/lib/libFFmpegTest.so 0x43e37a88
05-31 11:57:53.532: D/dalvikvm(278): Added shared lib /data/data/com.example.ffmpegtest/lib/libFFmpegTest.so 0x43e37a88
05-31 11:57:53.532: D/dalvikvm(278): No JNI_OnLoad found in /data/data/com.example.ffmpegtest/lib/libFFmpegTest.so 0x43e37a88, skipping init
05-31 11:57:53.562: W/System.err(278): java.io.IOException: Error running exec(). Command: [ffmpeg, -i, test.wav test1.wav] Working Directory: /mnt/sdcard Environment: null
Can anyone guide me the procedure ?
I think you do no need any *.so.
You , probably, should compile ffmpeg binary for Android.
Then put the binary into you res/raw folder.
Then, upon first execution, your program should copy it from the res/raw to the app's dir and set chmod +x on it.
Then you can call ffmpeg binary with System.exec or the like.
It worked for me fine. I have used the code here https://github.com/halfninja/android-ffmpeg-x264 as an example.

Using FFMPEG via command line in android

I want to use FFMPEG via COMMAND LINE in my android application.For this purpose:
I have cross-compiled the ffmpeg lib and got the libffmpeg.so
I have stored libffmpeg.so and the ffmpeg exectable in files directory of the my project.
This is the code i am using:
public class FFMPEGActivity extends Activity {
Process p;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] cmd =new String[4];
cmd[0]="/data/data/com.example.ffmpegnew/files/libffmpeg";
cmd[1]="-i";
cmd[2]="mnt/sdcard/music/baba.mp4";
cmd[3]="mnt/sdcard/music/outfile.mp4";
p = Runtime.getRuntime().exec(cmd,null, new File("/data/data/com.example.ffmpegnew/files"));
}
catch(Exception e)
{
System.out.println("exception"+e);
}
}
}
This is the exception i am getting:
09-17 13:47:01.679: I/System.out(3752): exceptionjava.io.IOException: Error running exec(). Command: [/data/data/com.example.ffmpegnew/files/libffmpeg.so, -i, mnt/sdcard/music/baba.mp4, mnt/sdcard/music/outfile.mp4] Working Directory: /data/data/com.example.ffmpegnew/files Environment: null
Please tell me how to solve this problem.Thanks in advance.
I think this won't work, unless you somehow manage to compile ffmpeg executable, place it somewhere in the file system , then access it through your native layer. Normally, though, they use libffmpeg API in android such as in Dolphin Player
Your code seems to try to run the library, not the ffmpeg executable. Note that even after fixing your cmd you will need extra tricks to load the libffmpeg.so, because Android loader does not load shared libs from ./
I would suggest to build a statically linked ffmpeg executable to save hassle.
Place ffmpeg and all the files it accesses on the internal card (obtain through context.getDir("", 0).
After you do this, you will be able to run ffmpeg through exec().
Some models, however, will refuse running this too.

executing ffmpeg command with getRuntime().exec() command

i want to run ffmepg command directly on android.
a simple command
ffmpeg -i vid.mp4 out.mp4
now the issue is that i have searched the internet and found the best android ffmpeg can be found here
http://bambuser.com/opensource
I have downloaded it and read the readme file and compiled it. the folder is ffmpeg. I have kept it in <--projectfolder-->/ffmpeg/
there is a ffmpeg executeable file in ffmpeg folder called ffmpeg folder
i have copied it in files folder and run this command
try {
Toast.makeText(this, "Working", Toast.LENGTH_SHORT).show();
Process p = Runtime.getRuntime().exec("/data/data/com.koder.testffmpeg/files/ffmpeg -i /sdcard/vid.mp4 /sdcard/out.mp4");
} catch (IOException e) {
txt.setText(e.toString());
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
according to this link
How do I reduce the video size captured by the default camera using FFMPEG in Android?
but still it does not work always exception i dont know what is going wrong
can someone plz help me with this
java.io.IOException: Error running exec(). Command:[/data/data/com.koder.testffmpeg/files/ffmpeg -i /sdcard/vid.mp4 /sdcard/out.mp4] Working Directory: null Environment:null
You should use getBaseContext().getApplicationInfo().nativeLibraryDir instead of "/data/data/com.example.ffmpegnew/files/"
Give executable permission to ffmpeg like so:
chmod 700 ffmpeg
Try using this:
Process proc = null;
ProcessBuilder pb = new ProcessBuilder();
proc = pb.command("String...yourCommand")
.redirectErrorStream(true).start();
BufferedReader bReader = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
This code work for me for get system wakelock details in android.may be this will useful to you.
You could combine above answers like this:
Process p = Runtime.getRuntime().exec("chmod 700 "+getBaseContext().getApplicationInfo().nativeLibraryDir + "/ffmpeg ...");

Categories

Resources