Skip to content

Instantly share code, notes, and snippets.

@vierbergenlars
Last active December 18, 2015 00:59
Show Gist options
  • Save vierbergenlars/5700415 to your computer and use it in GitHub Desktop.
Save vierbergenlars/5700415 to your computer and use it in GitHub Desktop.
#include <glob.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#define NUM_WORKERS 3
void *job_exec(void *i);
void pr(char *s);
glob_t matches;
int main() {
int ret;
pthread_t workers[NUM_WORKERS];
ret = glob("*.{flv,mp4}", GLOB_BRACE|GLOB_NOSORT, NULL, &matches);
if(ret != 0) {
char s0[15];
snprintf(s0, sizeof(s0), "GLOB error: %i", ret);
pr(s0);
return 1;
}
unsigned int i;
for(i=0; i < NUM_WORKERS; i++) {
ret = pthread_create(&workers[i], NULL, job_exec, &i);
if(ret) {
pr("Thead misbehaved. Killing all");
exit(1);
}
}
for(i=0; i < NUM_WORKERS; i++) {
pthread_join(workers[i], NULL);
}
globfree(&matches);
return 0;
}
void *job_exec(void *worker_id_void_ptr) {
unsigned int *worker_id_ptr = (unsigned int *)worker_id_void_ptr;
unsigned int worker_id = *worker_id_ptr;
char s1[20];
snprintf(s1, sizeof(s1), "Worker %u online", worker_id);
pr(s1);
unsigned int start;
unsigned int end;
start = worker_id*matches.gl_pathc/NUM_WORKERS;
end = (worker_id+1)*matches.gl_pathc/NUM_WORKERS-1;
char s2[24];
snprintf(s2, sizeof(s2), "Allocated %u - %u", start, end);
pr(s2);
unsigned int i;
for(i=start; i < end; i++) {
unsigned int command_len = 46 + 2*strlen(matches.gl_pathv[i]);
char *command;
command = (char *)malloc(command_len);
if(command == NULL) {
pr("Fuck this shit!");
exit(2);
}
snprintf(command, command_len, "avconv -i \"%1$s\" -vn -y -f mp3 \"%1$s.mp3\" 2>/dev/null", matches.gl_pathv[i]);
system(command);
free(command);
char status[20];
snprintf(status, sizeof(status), "%u/%u [%u]", i-start+1, end-start, i);
pr(status);
}
pr("Done");
return NULL;
}
void pr(char *s) {
printf("[%li] %s\n", syscall(SYS_gettid), s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment