1

a few months back I wrote a batch (windows batch file .bat) code that fetches some .exe files and use their commands to do different things. e.g encoding audio, video etc... Now I want the same thing but want to do it in C language.

set var = "Video"
ffmpeg -i %var%.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of %var%-Audio.aac

This code works fine in windows batch file (given that I have specified files in the same folder.)

But now I want to do this via C language. Well I know using

system("ffmpeg -i Video.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of Video-Audio.aac");

will work for C language, but the drawback is I can't use variable while exploiting ffmpeg's and neroAacEnc's commands / parameters. So is there anyway to get around it?
(Also, I'm gonna use other .exe files as well like x264.exe and mkvmerge.exe, so i'd appreciate it if someone could tell me how to use external .exe files parameters freely in C language.)

Light
  • 25
  • 4
  • 2
    Use [`snprintf`](http://en.cppreference.com/w/c/io/fprintf) to create the command string? If you mean *environment* variables, then also see [`getenv`](http://en.cppreference.com/w/c/program/getenv). – Some programmer dude Dec 05 '13 at 09:05

3 Answers3

1
char *var="Video"
char cmd[512];
sprintf(cmd, "ffmpeg -i Video.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of %s-Audio.aac", var);
system(cmd);

you can make more than 1 variable

char *var="Video"
char *app="ffmpeg";
char cmd[512];
sprintf(cmd, "%s -i Video.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of %s-Audio.aac", app, var);
system(cmd);
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • Nice. Simple and easy to understand. Just one query, though. I assume that you made the size of array `cmd[512]` arbitrarily big enough to accumulate the command line, right? or was there any specific reason to choose `512`? – Light Dec 05 '13 at 09:24
  • 1
    @Light yes right, I choosed 512 arbitrarily big enough to accumulate the command line – MOHAMED Dec 05 '13 at 09:26
  • Good follow-up question! In theory: [Maximum Length of Command String](http://stackoverflow.com/questions/3205027/maximum-length-of-command-line-string). In practice: "long enough". The sample command line in the original post is 89 characters long, and I don't think you'd use one of even twice that length. 512 is almost *six* times longer :) – Jongware Dec 05 '13 at 09:31
0

Try this:

char var[50];
char command[256];
sprintf(var,"%s","Video");
sprintf(command,"ffmpeg -i %s.mkv -f wav -| neroAacEnc -ignorelength -lc -q 0.4 -if - -of %s-Audio.aac",var,var);
system(command);
HAL9000
  • 3,562
  • 3
  • 25
  • 47
0

Use snprintf. It is safer alternative to sprintf.

/* Construct command */
#define MAX_CMD_LEN 32
char command[MAX_CMD_LEN];
const char * param1 = "abc";
int param2 = 6;
int len = snprintf(command, MAX_CMD_LEN, "ffmpeg %s %d", param1, param2);
/* Run command if all went well */
if(len > 0 && len < MAX_CMD_LEN) {
    system(command);  /* Runs 'ffmpeg abc 6' */
}
else {
    /* Command didn't fit our buffer */
}
user694733
  • 15,208
  • 2
  • 42
  • 68