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>
- 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) {
- bool isAudioFiles(char const *name);
- char *getTime(const char *s, char *n);
- DIR *dir;
- struct dirent *entry;
- FILE *file;
- char *path = (char *)malloc(100 * sizeof(char));
- strcat(path, d.dst);
- strcat(path, "/");
- strcat(path, d.op);
- file = fopen(path, "w+");
- 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) {
- fprintf(file, "%s,%s", entry->d_name, getTime(d.src, entry->d_name));
- }
- }
- fclose(file);
- closedir(dir);
- }
- }
- char *getTime(const char *s, char *n) {
- FILE *fp;
- char time[15];
- char *result = (char *)malloc(sizeof(size_t));
- char cmd[100] = "soxi -D";
- strcat(cmd, " ");
- strcat(cmd, s);
- strcat(cmd, "/");
- strcat(cmd, n);
- fp = popen(cmd, "r");
- if (fp == NULL)
- fprintf(stderr, "soxi command failed\n");
- if (fgets(time, 15, fp) != NULL)
- result = time;
- else
- fprintf(stderr, "fgets error\n");
- pclose(fp);
- return result;
- }
- 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