libdvdnav
A first libdvdnav program

Compiling a libdvdnav program

Below is a simple libdvdnav program. Type/copy it and save it into the file 'dvdtest.c'.

#include <stdio.h>
#include <dvdnav/dvdnav.h>
#include <dvdnav/dvdnav_events.h>
#include <sys/types.h>

int main(int argc, char **argv) {
  dvdnav_t *dvdnav;
  int finished, len, event;
  uint8_t buf[2050];

  /* Open the DVD */
  dvdnav_open(&dvdnav, "/dev/dvd");

  fprintf(stderr, "Reading...\n");
  finished = 0;
  while(!finished) {
    int result = dvdnav_get_next_block(dvdnav, buf,
                                       &event, &len);

    if(result == DVDNAV_STATUS_ERR) {
      fprintf(stderr, "Error getting next block (%s)\n",
              dvdnav_err_to_string(dvdnav));
      exit(1);
    }

    switch(event) {
     case DVDNAV_BLOCK_OK:
        /* Write output to stdout */
        fwrite(buf, len, 1, stdout);
      break;
     case DVDNAV_STILL_FRAME:
       {
        fprintf(stderr, "Skipping still frame\n");
        dvdnav_still_skip(dvdnav);
       }
      break;
     case DVDNAV_STOP:
       {
        finished = 1;
       }
     default:
      fprintf(stderr, "Unhandled event (%i)\n", event);
      finished = 1;
      break;
    }
  }

  dvdnav_close(dvdnav);

  return 0;
}

If you have correctly installled libdvdnav, you should have the command 'dvdnav-config' in your path. If so you can compile this program with

  gcc -o dvdtest dvdtest.c `dvdnav-config --cflags --libs`

If all goes well, this should generate the 'dvdtest' program in your current working directory. You can now start saving a MPEG 2 stream directly off your DVD with

  ./dvdtest 2>error.log >out.mpeg

If the command fails, check the error.log file for details.

Line-by-line walk through

 include <stdio.h>
 include <dvdnav/dvdnav.h>
 include <dvdnav/dvdnav_events.h>
 include <sys/types.h>

These lines include the necessary headers. Almost all libdvdnav programs will only need to include the dvdnav.h and dvdnav_events.h header files from the dvdnav directory.

 dvdnav_open(&dvdnav, "/dev/dvd");

The libdvdnav uses libdvdread for its DVD I/O. libdvdread accesses the DVD-device directly so dvdnav_open() needs to be passed the location of the DVD device. libdvdread can also open DVD images/mounted DVDs. Read the libdvdread documentation for more information.

 int result = dvdnav_get_next_block(dvdnav, buf,
                                    &event, &len);

Return to A libdvdnav Tutorial.