2

I am executing the below ffmpeg command for trimming videos.The issue I am having is that if filepath contains spaces then the command fails.I tried many ways to handle spaces but none of them worked except moving file to a path that doesn't have space and then executing the command with new file path as source. Below is the command-

 execFFmpegBinary("-i " +  filepath   + " -ss " + startMs / 1000 + " -to " + endMs / 1000 + " -strict -2 -async 1 " + dest.getAbsolutePath());



private void execFFmpegBinary(final String command) {
        try {
            ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
                @Override
                public void onFailure(String s) {
                    Log.e("Previewragment", "FAILED with output : " + s);
                }

                @Override
                public void onSuccess(String s) {
                    Log.e("Previewragment", "SUCCESS with output : " + s);
                }

                @Override
                public void onProgress(String s) {
                    Log.e("Previewragment", "Started command : ffmpeg " + command);
                    Log.e("Previewragment", "progress : " + s);
                }

                @Override
                public void onStart() {
                    Log.e("Previewragment", "Started command : ffmpeg " + command);

                }

                @Override
                public void onFinish() {



                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            // do nothing for now
        }
    }

I saw this answer and tried

 String addQuotes(String in ) {
        return "\"" + in + "\"";
    }
execFFmpegBinary("-i " +  addQuotes(filepath)   + " -ss " + startMs / 1000 + " -to " + endMs / 1000 + " -strict -2 -async 1 " + dest.getAbsolutePath())

;

2
  • I assume you tried something like the this? (escaping the double quotes around filepath): execFFmpegBinary("-i \"" + filepath + "\" -ss " + ...
    – DRiFTy
    Oct 15, 2015 at 13:21
  • @DiscGolfer check updated question..i tried adding quotes..
    – user3904345
    Oct 15, 2015 at 13:24

2 Answers 2

0
//Process ffmpeg;
        public string exec(string input, string output, string parametri,string fileName) 
        {
        //Create the output and streamreader to get the output
        string output1 = null; StreamReader srOutput = null;
        input = addQuotes(input);
        Process ffmpeg = new Process();
        try
        {

            ffmpeg.StartInfo.Arguments = " -i " + input + " -ss 00:00:00.100 -vframes 1" + " " + output;
            ffmpeg.StartInfo.FileName = fileName;
            ffmpeg.StartInfo.UseShellExecute = false;
            ffmpeg.StartInfo.RedirectStandardOutput = true;
            ffmpeg.StartInfo.RedirectStandardError = true;
            ffmpeg.StartInfo.CreateNoWindow = true;
            ffmpeg.Start();
            ffmpeg.WaitForExit();
            //get the output
            srOutput = ffmpeg.StandardError;
            //now put it in a string
            output1 = srOutput.ReadToEnd();
            ffmpeg.Close();
        }
        catch
        {
            ffmpeg.Close();
            output = string.Empty;
        }
        finally
        {
            //now, if we succeded, close out the streamreader
            if (srOutput != null)
            {
                srOutput.Close();
                srOutput.Dispose();
            }
        }
        return output;
    }


    public string addQuotes(string s)
    {
        return "\"" + s + "\"";
    }
1
  • 2
    You should consider adding some form of comment so that the OP can understand their problem and how your solution helps. Just pasting code is not considered a "good answer."
    – leigero
    Sep 28, 2016 at 14:58
0

You must switch to string-array variant of Runtime.exec(), as described in ffmpeg-android-java.

For the adventurous, there is a hidden trick in ffmpeg, but IMHO working with String[] is both easier and more robust.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.