Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run ffmpeg from command line in android

I want to use ffmpeg using command line.I have saved ffmpeg.so in files directory in the project.But i am getting exception while doing so.This is the code:

public class MainActivity extends Activity {

    Process p;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Process p = Runtime.getRuntime().exec("/data/data/com.example.ffmpegnew/files/ffmpeg",null, new File("/data/data/com.example.ffmpegnew/files"));

        }
        catch(Exception e)
        {
            System.out.println("exception"+e);
        }

    }

}

This is the exception:

09-16 16:21:24.992: I/System.out(2103): exceptionjava.io.IOException: Error running exec(). Commands: [/data/data/com.example.ffmpegnew/files/ffmpeg] Working Directory: /data/data/com.example.ffmpegnew/files Environment: null

Please tell me what kind of mistake i am doing.Thanks.

like image 737
user1662334 Avatar asked Sep 16 '12 12:09

user1662334


People also ask

How do I run FFmpeg from command-line?

Verify FFmpeg path in Command PromptLaunch Command Prompt and type the command FFmpeg in the command prompt terminal and hit Enter. If the FFmpeg is added properly to Windows Path, the command prompt will display the details about FFmpeg like its version number, configuration, etc.

Does FFmpeg work on Android?

To integrate FFmpeg in your android application you can use pre-compiled libraries like FFmpeg, which provide multiple predefined multimedia operations(Queries) and which is easy to integrate by adding FFmpeg dependency in app module gradle file and sync project.

How do I combine two videos in android programmatically FFmpeg?

run(new String[] { "ffmpeg", "-i", "'concat:" + ts1 + "|" + ts2 + "'", "-vcodec", "copy", "-acodec", "copy", "-absf", "aac_adtstoasc", output });


2 Answers

The exception mentions the 'environment' is null...

maybe instead of 'exec', try using processbuilder as in the below sample of server-side code running ffmpeg wrapped in a shellscript(pars_submit)... note builder.environment

        public String getFfmpeg(@QueryParam("infil1") String infil1, 
                @QueryParam("infil2") String infil2, @QueryParam("otfil") String otfil,
                @QueryParam("t") String time) {         
        String outfil = "dummy.mp4";

          List<String> command = new ArrayList<String>();
            command.add("vendor/bin/pars_submit");

            command.add(infil1);     

            command.add(infil2);
            command.add(otfil);
            command.add(time);

System.out.println("Starting process " +command.toString());
            ProcessBuilder builder = new ProcessBuilder(command);
            Map<String, String> environ = builder.environment();
//          for(Entry<String, String> entry : environ.entrySet()){
    //          System.out.println("ENV " +entry.getKey() + " " +entry.getValue());
      //      }
//          builder.redirectErrorStream(true);
            Process process = null;
            try {
                process = builder.start();

            InputStream is = process.getInputStream();

            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {  
              //System.out.println(line);
                outfil=line;
            }

//          int exitVal = process.waitFor();
 //           System.out.println("Process exitValue: " + exitVal);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            finally {
                  if (process != null) {
                    process.destroy();
                    process = null;
                  }


                }           


            return outfil;


                }

IMO - android-ffmpeg is going to be more reliable using a full JNI interface and wrapper for the call to ffmpeg.main(). Look at all the git projects... search on 'android-ffmpeg' and look how they invoke ffmpeg. CLI is not used.

pars_submit

#!/bin/bash
shopt -s globstar
uri=$1
filnam="${uri##*/}"
uri2=$2
filnam2="${uri2##*/}"
otfil=$3
time=$4
curl -#LO  $uri
curl -#LO  $uri2
ffmpeg -y -loop 1 -i "$filnam" -i "$filnam2" -t "$time" -r 1/2 -pass 1 -vcodec libx264 -b:v 200k -bt 50k  -an -f mp4 -strict -2 -passlogfile mydummy /dev/null
# echo "ffmpegP1 Exit status" $?
ffmpeg -y -loop 1 -i "$filnam" -i "$filnam2" -t "$time" -r 1/2 -pass 2 -vcodec libx264 -b:v 200k -bt 50k  -f mp4 -strict -2 -passlogfile mydummy -ar 44100 "$otfil" 
# echo "ffmpegp2 Exit status" $?
# last test
json=$(curl  -X POST  -H "X-Parse-Application-Id: 3KxPB" -H "X-Parse-REST-API-Key: kVl5Z0CX"  -H "Content-Type: video/mp4"  --data-binary @"$otfil" https://api.parse.com/1/files/"$otfil")
# echo "parse POST Exit status" $?
echo $json
like image 26
Robert Rowntree Avatar answered Oct 14 '22 03:10

Robert Rowntree


So firstly, you cannot directly execute a shared object file. You need to build a ffmpeg executable that can be run using /system/bin

Kindly take this as reference FFmpeg on Android

After you are able to get it compiled, you can but the executable in your assets folder of the APK and extract it to /data/data/<your package>/ (ensure a chmod 755 too!)

Once that's done, you can run the executable by using building a command line string and passing it to ProcessBuilder instead of using the Runtime.exec()

like image 109
Royston Pinto Avatar answered Oct 14 '22 02:10

Royston Pinto