delphi - 创建进程: escape spaces in the application path

标签 delphi createprocess

我们需要在我的 delphi 应用程序的命令窗口中执行 ffmpeg。

我们找到了使用“ExtractShortPathName”函数来保护路径的解决方案。

但在某些计算机上我们无法获取 8.3 路径(HKLM\SYSTEM\CurrentControlSet\Control\FileSystem\NtfsDisable8dot3NameCreation 为 2),我们希望找到另一种方法来转义空格。

这是代码:

sParameters := '"C:\Users\[...]\input.wav" -r 12.03 -f image2 -i "C:\Users\[...]\delphilm%d.png" -vf "scale=1024:704" -ab 96k -r 24 -b 2000k  -pass 1 -vcodec libx264 -fpre "C:\[...]\libx264-normal.ffpreset" "C:\Users\[...]\export.mp4"';
sCommand := 'C:\Program Files\My application\utils\bin\ffmpeg.exe';
Handle := CreateProcess(nil, PChar('cmd.exe /C '+ProtectPath(sCommand)+' '+sParameters),nil, nil, True, 0, nil, nil, SI, PI);

使用 ProtectPath 功能:

function ProtectPath(sCommand:Widestring):Widestring;
begin
  Result := sCommand;
  // get the 8.3 path
  Result := ExtractShortPathName(sCommand);
  // if 8.3 path is not accessible
  if(Pos(' ', Result)>0)then begin
    //Result := '"'+sCommand+'"';    --> do not work
    //Result := StrReplace(sCommand, ' ','" "');  --> do not work
    //Result := StrReplace(sCommand, ' ','^ ');  --> do not work
    //Result := StrReplace(sCommand, ' ','\ ');  --> do not work
    //Result := StrReplace(sCommand, ' ','\\ ');   --> do not work
    //Result := StrReplace(sCommand, ' ','/ ');  --> do not work
    //Result := StrReplace(sCommand, ' ','// ');  --> do not work
  end;
end;

有什么想法吗?

最佳答案

您不需要检索 8.3 文件名。您所要做的就是用一对引号将长路径括起来(如果其中包含任何空格字符)(就像您已经对某些 FFMPEG 参数所做的那样)。然后,完全摆脱 cmd.exe 并直接调用 ffmpeg.exe 即可。

sCommand := '"C:\Program Files\My application\utils\bin\ffmpeg.exe"';

sParameters := '"C:\Users\[...]\input.wav" -r 12.03 -f image2 -i "C:\Users\[...]\delphilm%d.png" -vf "scale=1024:704" -ab 96k -r 24 -b 2000k  -pass 1 -vcodec libx264 -fpre "C:\[...]\libx264-normal.ffpreset" "C:\Users\[...]\export.mp4"';

Handle := CreateProcess(nil, PChar(sCommand + ' ' + sParameters), nil, nil, True, 0, nil, nil, SI, PI);

如果您想动态引用,请使用(Ansi)QuotedStr(),例如:

function ProtectParam(sParam: String): String;
begin
  if LastDelimiter(' "', sParam) <> 0 then
    Result := QuotedStr(sParam)
  else
    Result := sParam;
end;

FFMPEG := 'C:\Program Files\My application\utils\bin\ffmpeg.exe';
InputFile := 'C:\Users\[...]\input.wav';
PngFile := 'C:\Users\[...]\delphilm%d.png';
PresetFile := 'C:\[...]\libx264-normal.ffpreset';
ExportFile := 'C:\Users\[...]\export.mp4';

sCommand := ProtectParam(FFMPEG) + ' ' + ProtectParam(InputFile) + ' -r 12.03 -f image2 -i ' + ProtectParam(PngFile) + ' -vf "scale=1024:704" -ab 96k -r 24 -b 2000k  -pass 1 -vcodec libx264 -fpre ' + ProtectParam(PresetFile) + ' ' + ProtectParam(ExportFile);

Handle := CreateProcess(nil, PChar(sCommand), nil, nil, True, 0, nil, nil, SI, PI);

关于delphi - 创建进程: escape spaces in the application path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22306086/

相关文章:

c++ - 创建独立进程!

debugging - 调试器在启动他们的被调试者时如何绕过图像文件执行选项?

c++ - CreateProcess 和 ShellExecute 的区别

delphi - 如何在命令行中包含路径?

delphi - 是否可以将jpg/png图像加载到TbitBtn或TSpeedButton中?

Delphi XE2 FireMonkey 报告选项

delphi - Delphi中如何检查字符串是否是有效的日期时间格式字符串

delphi - 检查文件是否正在使用以及由哪个应用程序使用?

ios - 升级到 XE7 更新 1 后,在 iOS 下使用带有 OpenSSL 的 THTTPGet 访问冲突

c++ - Visual Studio 2008 中的 cl.exe 包装器