对于带有空格的文件路径,FFMPEG命令失败

时间:2015-10-15 13:16:44

标签: android ffmpeg string-formatting

我正在执行以下ffmpeg命令来修剪视频。我遇到的问题是,如果filepath包含空格,那么命令就会失败。我尝试了很多方法来处理空格,但除了将文件移动到没有空间的路径,然后以新文件路径作为源执行命令。 以下是命令 -

 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
        }
    }

我看到了这个answer并尝试了

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

2 个答案:

答案 0 :(得分: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 :(得分:0)

您必须切换到Runtime.exec()的字符串数组变体,如ffmpeg-android-java中所述。

对于喜欢冒险的人来说,ffmpeg中有一个hidden trick,但使用String []的IMHO更容易且更健壮。