Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dirent.h>
- #include <stdbool.h>
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #define MAX_ARRAY 1000
- typedef struct fileDir {
- char *src;
- char *dst;
- char *op;
- } fileDir;
- int main(int argc, char *argv[]) {
- void help();
- fileDir cli(int argc, char *argv[]);
- void process(fileDir d);
- if (argc <= 2) {
- fprintf(stderr, "Please type a name for csv file.\n");
- help();
- }
- fileDir dir = cli(argc, argv);
- process(dir);
- return 0;
- }
- void process(fileDir d) {
- DIR *dir;
- bool isAudioFiles(char const *name);
- struct dirent *entry;
- FILE *file;
- char csvLocation[200] = "";
- strcat(csvLocation, d.dst);
- strcat(csvLocation, "/");
- strcat(csvLocation, d.op);
- file = fopen(csvLocation, "w+");
- FILE *fp;
- char time[1024];
- char cmd[100] = "soxi -D";
- if ((dir = opendir(d.src)) == NULL)
- fprintf(stderr, "could't open %s\n", d.src), exit(1);
- else {
- fprintf(file, "NOME,DURAÇÃO\n");
- while ((entry = readdir(dir)) != NULL) {
- if (isAudioFiles(entry->d_name) == true) {
- strcat(cmd, " ");
- strcat(cmd, d.src);
- strcat(cmd, "/");
- strcat(cmd, entry->d_name);
- fp = popen(cmd, "r");
- if (fp == NULL) {
- fprintf(stderr, "soxi command failed\n");
- exit(1);
- }
- if (fgets(time, 60, fp) != NULL)
- fprintf(file, "%s,%s",entry->d_name, time);
- }
- }
- pclose(fp);
- fclose(file);
- closedir(dir);
- }
- }
- bool isAudioFiles(char const *name) {
- size_t len = strlen(name);
- if (len > 4 &&
- (strcmp(name + len - 4, ".mp3") && strcmp(name + len - 4, ".mp4") &&
- strcmp(name + len - 4, ".wav")) == 0)
- return true;
- else
- return false;
- }
- fileDir cli(int argc, char *argv[]) {
- void help();
- int opt;
- fileDir dir = {".", ".", ""};
- while ((opt = getopt(argc, argv, "s:d:n:")) != -1) {
- switch (opt) {
- case 's':
- dir.src = optarg;
- break;
- case 'd':
- dir.dst = optarg;
- break;
- case 'n':
- dir.op = optarg;
- break;
- default:
- printf("Unknown option: %c\n", optopt);
- help();
- exit(1);
- }
- }
- return dir;
- }
- void help() {
- fprintf(stderr, "\nUSAGE: audio_length [OPTIOMS] -n <FILENAME.csv>\n"
- "Options:\n"
- " -s <path for source directory>\n"
- " -d <path for destination file>\n\n"
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement