double quotes escaping in golang exec

Pavel K. picture Pavel K. · Oct 20, 2014 · Viewed 9.2k times · Source

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?

Answer

James Henstridge picture James Henstridge · Oct 20, 2014

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.