double quotes escaping in golang exec

半世苍凉 提交于 2019-12-12 08:11:04

问题


i need to run following command:

ffmpeg -i input.jpg -vf scale="'if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'" output_320x240_boxed.png

so i execute:

cmd = exec.Command("ffmpeg", "-i", "input.jpg", "-vf", "scale=\"'if(gt(a,4/3),640,-1)':'if(gt(a,4/3),-1,300)'\"", "output_320x240_boxed.png")

it fails with following error:

Error when evaluating the expression 'if(gt(a,4/3),-1,300)"'.
Maybe the expression for out_w:'"if(gt(a,4/3),640,-1)' or for out_h:'if(gt(a,4/3),-1,300)"' is self-referencing.

Command works when executed in command line. Why does it happen and how can i escape those double quotes to prevent this error?


回答1:


When you execute the given ffmpeg command line, your shell parses it into a set of command line arguments that are essentially:

{
    "ffmpeg",
    "-i",
    "input.jpg",
    "-vf",
    "scale='if(gt(a,4/3),320,-1)':'if(gt(a,4/3),-1,240)'",
    "output_320x240_boxed.png",
}

The extra quotes in the scale=... argument interpreted by the shell, rather than being passed on to the underlying program. So when executing the same program with Go, where you are passing a list of arguments directly, you should leave out those extra quotes.



来源:https://stackoverflow.com/questions/26473674/double-quotes-escaping-in-golang-exec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!