1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include "config.h"
#define TRUE 1
#define FALSE 0
typedef struct FFArgs{
char name[20];
char value[1000];
} FFArgs;
static FFArgs ffargs[1000];
static int nffargs = 0;
/**
* exit with error code 1
*/
static void error_exit(const char *msg){
fprintf(stderr, "E: %s\n", msg);
exit(1);
}
/**
* print usage
*/
static void print_credits(int argc, char *argv[]){
printf("%s version %s\n", PACKAGE, VERSION);
printf("Usage: %s [OPTIONS] -- PRESET INFILE OUTFILE\n", argv[0]);
printf("\n");
}
/**
* set ffargs
*/
static void ffargs_set(const char *name, const char *value, int force){
/* check the length */
if(strlen(name) >= sizeof(ffargs[0].name))
error_exit("too long argument name detected");
if(strlen(value) >= sizeof(ffargs[0].value))
error_exit("too long argument value detected");
/* check if this argument is already added or not */
int index = nffargs;
for(int i=0; i<nffargs; i++){
if(strcmp(ffargs[i].name, name) == 0){
if(!force){
return;
}
index = i;
nffargs--;
break;
}
}
nffargs++;
if(index >= sizeof(ffargs))
error_exit("too match arguments specified");
/* set */
strcpy(ffargs[index].name, name);
strcpy(ffargs[index].value, value);
}
static int preset_read(char *filename){
char line[1000];
char name[1000];
char value[1000];
FILE *f = NULL;
f = fopen(filename, "r");
if(!f)
return FALSE;
for(;!feof(f);){
if(fscanf(f, "%999[^\n]\n", line) != 1)
error_exit("too match characters in one line");
if(line[0] == '#')
continue;
if(sscanf(line, "%999[^=]=%999[^\n]\n", name, value) != 2)
error_exit("invalid syntax");
ffargs_set(name, value, FALSE);
}
fclose(f);
return TRUE;
}
/**
* main function
*/
int main(int argc, char *argv[]){
print_credits(argc, argv);
/* parse options */
int extras = -1;
for(int i=0; i<argc; i++){
if(strcmp(argv[i], "--") == 0){
extras = i + 1;
break;
}
if(argv[i][0] == '-'){
if(i+1 >= argc)
break;
/* argument without value */
if(argv[i+1][0] == '-'){
ffargs_set(argv[i]+1, "", TRUE);
}else{
ffargs_set(argv[i]+1, argv[i+1], TRUE);
i++;
}
}
}
if(extras < 0)
error_exit("'--' was not found");
/* read from preset files */
char filename[1000];
if(extras + 3 > argc)
error_exit("OUTFILE is not specified");
do{
/* current directory */
char cwd[1000];
getcwd(cwd, sizeof(cwd));
snprintf(filename, sizeof(filename), "%s/%s.ffpreset", cwd, argv[extras]);
if(preset_read(filename)) break;
/* $HOME/.ffpreset/PRESET.ffpreset */
snprintf(filename, sizeof(filename), "%s/%s.ffpreset", getenv("HOME"), argv[extras]);
if(preset_read(filename)) break;
/* DATADIR/ */
snprintf(filename, sizeof(filename), "%s/%s/%s.ffpreset", DATADIR, PACKAGE, argv[extras]);
if(preset_read(filename)) break;
/* not found */
error_exit("preset file not found");
}while(FALSE);
/* output configuration */
char cmd[1000];
snprintf(cmd, sizeof(cmd), "ffmpeg -i \"%s\"", argv[extras+1]);
for(int i=0; i<nffargs; i++){
char tmp[1000];
strcpy(tmp, cmd);
snprintf(cmd, sizeof(cmd), "%s -%s %s", tmp, ffargs[i].name, ffargs[i].value);
}
{
char tmp[1000];
strcpy(tmp, cmd);
snprintf(cmd, sizeof(cmd), "%s \"%s\"", tmp, argv[extras+2]);
}
printf("Executing:\n%s\n", cmd);
system(cmd);
return 0;
}
|