【问题标题】:FFMPEG command fails for file path with white spaces带有空格的文件路径的 FFMPEG 命令失败
【发布时间】:2016-01-13 23:23:18
【问题描述】:

我正在执行以下ffmpeg 命令来修剪视频。我遇到的问题是,如果文件路径包含空格,则命令失败。我尝试了多种处理空格的方法,但除了将文件移动到路径之外,它们都不起作用没有空间,然后以新文件路径作为源执行命令。 下面是命令-

 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())

;

【问题讨论】:

  • 我假设您尝试过这样的事情? (转义 filepath 周围的双引号):execFFmpegBinary("-i \"" + filepath + "\" -ss " + ...
  • @DiscGolfer 检查更新的问题..我尝试添加引号..

标签: android ffmpeg string-formatting


【解决方案1】:

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

对于喜欢冒险的人,在 ffmpeg 中有一个 hidden trick,但恕我直言,使用 String[] 更容易且更健壮。

【讨论】:

    【解决方案2】:
    //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 + "\"";
        }
    

    【讨论】:

    • 您应该考虑添加某种形式的评论,以便 OP 了解他们的问题以及您的解决方案如何提供帮助。仅粘贴代码不被视为“好答案”。
    猜你喜欢
    • 2021-09-08
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多