Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double quotes escaping in golang exec

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?

like image 255
Pavel K. Avatar asked Oct 20 '14 19:10

Pavel K.


1 Answers

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.

like image 180
James Henstridge Avatar answered Oct 05 '22 03:10

James Henstridge