Advertisement
symdrome

lib.c

Sep 11th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. #include <dirent.h>
  2. #include <limits.h>
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include "audio_len.h"
  11.  
  12. bool isAudioFiles(char const *name) {
  13.   size_t len = strlen(name);
  14.   if (len > 4 &&
  15.       (strcmp(name + len - 4, ".mp3") && strcmp(name + len - 4, ".mp4") &&
  16.        strcmp(name + len - 4, ".wav")) == 0)
  17.     return true;
  18.   else
  19.     return false;
  20. }
  21.  
  22. void process(fileDir d) {
  23.   bool isAudioFiles(char const *name);
  24.   char *getTime(const char *s, char *n);
  25.  
  26.   DIR *dir;
  27.   struct dirent *entry;
  28.  
  29.   FILE *file;
  30.   char *path = (char *)malloc(sizeof(char));
  31.  
  32.   sprintf(path, "%s/%s", d.dst, d.op);
  33.  
  34.   file = fopen(path, "w+");
  35.  
  36.   if ((dir = opendir(d.src)) == NULL)
  37.     fprintf(stderr, "could't open %s\n", d.src), exit(1);
  38.   else {
  39.     fprintf(file, "NOME,DURAÇÃO\n");
  40.     while ((entry = readdir(dir)) != NULL) {
  41.       if (isAudioFiles(entry->d_name) == true) {
  42.         fprintf(file, "%s,%s", entry->d_name, getTime(d.src, entry->d_name));
  43.       }
  44.     }
  45.  
  46.     fclose(file);
  47.     closedir(dir);
  48.   }
  49. }
  50.  
  51. char *getTime(const char *s, char *n) {
  52.   FILE *fp;
  53.   char time[15];
  54.   char *result = (char *) malloc(CHAR_MAX * sizeof(char));
  55.  
  56.   char cmd[100];
  57.   sprintf(cmd, "soxi -D %s/%s 2>/dev/null", s, n);
  58.  
  59.   fp = popen(cmd, "r");
  60.  
  61.   if (fp == NULL)
  62.     fprintf(stderr, "soxi command failed\n");
  63.  
  64.   if (fgets(time, 15, fp) != NULL)
  65.     result = time;
  66.   else
  67.     fprintf(stderr, "fgets error\n");
  68.  
  69.   pclose(fp);
  70.  
  71.   return result;
  72.   free(result);
  73. }
  74.  
  75. void help() {
  76.   fprintf(stderr, "\nUSAGE: audio_length [OPTIOMS] -n <FILENAME.csv>\n"
  77.                   "Options:\n"
  78.                   "       -s <path for source directory>\n"
  79.                   "       -d <path for destination file>\n\n");
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement