Source

dlaudiotools / wav_to_ogg.c

/*
  Simple script for WAV to Vorbis conversion via oggenc
  David Duncan Ross Palmer <www.daybologic.co.uk/mailddrp>
*/

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#define QUALITY (5)

static void Help(const char* Program);
static char* Wavify(const char* FileName);
static char* myitoa(const int Value);

int main(int argc, char* argv[])
{
  int argi;
  unsigned int count = 0U;

  for ( argi = 1; argi < argc; argi++ ) {
    if ( strcmp(argv[argi], "--help" ) == 0 ) {
      Help(argv[0]);
      break;
    }
    else {
      char command[4096];
      char* wavified = Wavify(argv[argi]);
      command[0] = '\0';

      if ( wavified ) {
        strncat(command, "oggenc -q ", sizeof(command)-1);
        strncat(command, myitoa(QUALITY), (sizeof(command)-1) - strlen(command));
        strncat(command, " -o ", (sizeof(command)-1) - strlen(command));
        strncat(command, wavified, (sizeof(command)-1) - strlen(command));
        strncat(command, " ", (sizeof(command)-1) - strlen(command));
        strncat(command, argv[argi], (sizeof(command)-1) - strlen(command));
        printf("\n\n%s\n", command);
        system(command);
        count++;
        free(wavified);
      }
    }
  }
  printf("\n%u file(s) converted.\n", count);
  return EXIT_SUCCESS;
}

static void Help(const char* Program)
{
  printf("%s [--help] [MP3 filenames...]\n", Program);
}

static char* Wavify(const char* FileName)
{
  char* copy = (char*)malloc(strlen(FileName)+16); /* 16 for safely */
  if ( copy ) {
    char* ptr;
    strcpy(copy, FileName);
    ptr = strrchr(copy, '.');
    if ( ptr )
      sprintf(ptr, ".%d.ogg", QUALITY);
  }
  return copy;
}

static char* myitoa(const int Value)
{
  static char crappyBuffer[18]; /* Not thread safe, don't copy this function to anywhere else */
  sprintf(crappyBuffer, "%d", Value);
  return crappyBuffer;
}