-
Notifications
You must be signed in to change notification settings - Fork 0
/
ffqueued.c
198 lines (169 loc) · 4.06 KB
/
ffqueued.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <openssl/md5.h>
#define QUEUE_FILE "queue"
#define SWAP_FILE ".queue.swp"
#define RUN_LOG_PREFIX "running/"
#define DONE_LOG_PREFIX "finished/"
#define LINE_LENGTH 1024
#define ARGS_LENGTH 1536
#define CMD_LENGTH 1664
#define MAX_PROCESSES 4
// {{{ Function Definitions
char *parse_args(char *line);
void ffqueued_init();
char *queue_pop();
void read_queue();
void update_progress();
void check_exits();
// }}}
// {{{ Variable Definitions
int running; ///< Number of running ffmpeg processes
char *line; ///< urlencoded line read from queue file
char *args; ///< Assembled arguments as ffmpeg expects them
char *cmd; ///< Final buffer to send to system()
// }}}
// {{{ int main(int argc, char **argv)
int main(int argc, char **argv)
{
ffqueued_init();
while(1)
{
read_queue();
update_progress();
check_exits();
sleep(10);
}
}
// }}}
// {{{ void ffqueued_init()
/**
* Set up our environment
*/
void ffqueued_init()
{
line = malloc(LINE_LENGTH);
args = malloc(ARGS_LENGTH);
cmd = malloc(CMD_LENGTH);
}
// }}}
// {{{ char *parse_args(char *line)
/**
* Turns a urlencoded array into an ffmpeg command.
* @param line The string to decode.
*
* The first argument should be the desired output file, the rest should be standard ffmpeg arguments.
*
* @example "output_file.m4v?i=input_file.avi&vcodec=h264&b=1536"
*
* @return A command that can be passed to the shell to execute
*/
char *parse_args(char *line)
{
if(line == NULL) return NULL;
int len = strlen(line);
// We can't possible have a command shorter than 5 chars. (a?i=b)
if(len < 5) return NULL;
memset(args, 0, ARGS_LENGTH);
char *buf; // Use this to build individual arguments
int i, wstart = 0;
for(i = 0;i <= len;i++)
{
switch(line[i])
{
case '?': // Last word was output file
line[i] = 0;
args = strcat(args, &line[wstart]);
args = strcat(args, " ");
wstart = i + 1;
break;
case '=': // Last word was a key
line[i] = 0;
buf = malloc(strlen(&line[wstart]) + 3);
sprintf(buf, "-%s ", &line[wstart]);
args = strcat(args, buf);
free(buf);
wstart = i + 1;
break;
case '\n':
case 0:
case '&': // Last word was a value
line[i] = 0;
buf = malloc(strlen(&line[wstart]) + 3);
sprintf(buf, "\"%s\" ", &line[wstart]);
args = strcat(args, buf);
free(buf);
wstart = i + 1;
break;
}
}
return args;
}
// }}}
// {{{ char *queue_pop()
/**
* Pop the top line off the queue
*/
char *queue_pop()
{
FILE *qfp = fopen(QUEUE_FILE, "r");
if(qfp && fgets(line, LINE_LENGTH, qfp))
{
// Write the rest of the file somewhere temporary
FILE *tfp = fopen(SWAP_FILE, "w");
while(tfp && !feof(qfp))
fputc(fgetc(qfp), tfp);
if(tfp) fclose(tfp);
fclose(qfp);
// move truncated file onto QUEUE_FILE
unlink(QUEUE_FILE);
rename(SWAP_FILE, QUEUE_FILE);
return line;
}
return NULL;
}
// }}}
// {{{ void read_queue()
/**
* Read in commands from the queue and start executing them.
*/
void read_queue()
{
char *hash;
while(NULL != queue_pop())
{
args = parse_args(line);
memset(cmd, 0, CMD_LENGTH);
hash = MD5(line, strlen(line), NULL);
snprintf(
cmd
, CMD_LENGTH
, "ffmpeg %s& 2> %s%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx &"
, args
, RUN_LOG_PREFIX, hash[ 0], hash[ 1], hash[ 2], hash[ 3], hash[ 4], hash[ 5], hash[ 6], hash[ 7]
, hash[ 8], hash[ 9], hash[10], hash[11], hash[12], hash[13], hash[14], hash[15]
);
system(cmd);
}
}
// }}}
// {{{ void update_progress()
/**
* Scrape ffmpeg output and update progress files
*/
void update_progress()
{
}
// }}}
// {{{ void check_exits()
/**
* wait() on ffmpeg processes that have finished
*/
void check_exits()
{
}
// }}}