LibVLC Tutorial 086c

From VideoLAN Wiki
Jump to navigation Jump to search

Tutorial for libVLC for version 0.8.6c

Tested on cygwin with gcc 3.4, windows xp; instructions also for linux with small code change. You will want to change the plugin path to where you actually have vlc installed. And you will want to change the video filename to one which actually exists on your system. This will initialize vlc and play the video for ten seconds. After you get this to work, then you will want to look up the external API documentation to see how to use the other interfaces. Note there are two interfaces documented in the external API page; the first one is the "old" legacy one using calls such as VLC_Init. The second one is the "current" one using calls such as libvlc_new, which is used in the following code.

Code

#include <stdio.h>
#include <windows.h>
#include <vlc/libvlc.h>

static void quit_on_exception (libvlc_exception_t *excp) {
   if (libvlc_exception_raised (excp)) {
      fprintf(stderr, "error: %s\n", libvlc_exception_get_message(excp));
      exit(-1);
   }
}

int main(int argc, char **argv) {
   libvlc_exception_t excp;
   libvlc_instance_t *inst;
   int item;
   char *myarg0 = "-I";  char *myarg1 = "dummy";
   char *myarg2 = "--plugin-path=c:\\program files\\videolan\\plugins";
   char *myargs[4] = {myarg0, myarg1, myarg2, NULL};
   char *filename = "c:\\video\\main\\Everybody_Hates_Chris_Feb_26.mpg";

   libvlc_exception_init (&excp);
   inst = libvlc_new (3, myargs, &excp);
   quit_on_exception (&excp);
   item = libvlc_playlist_add (inst, filename, NULL, &excp); 
   quit_on_exception (&excp);
   libvlc_playlist_play (inst, item, 0, NULL, &excp); 
   quit_on_exception (&excp);
   Sleep (10000);
   libvlc_destroy (inst);
   return 0;
}

Compile/link

You can use libvlc without compiling your own copy of the vlc source code. You can use the libvlc.dll which comes in the standard end user installation. But, to access the include files, you will need to download and untar the sources distribution. You only need to untar it; you do not even need to run the configure script. Set your $path to include the directory which contains libvlc.dll and the plugins directory. For example, add "c:\program files\videolan" in a relatively standard installation. This is so Windows can find the libvlc.dll. Otherwise you have to copy the dll to your current directory. Use the following Makefile:

VLC_INST = "c:\program files\videolan"
VLC_SRC = "c:\tools\vlc-0.8.6c\"
demo: demo.c
   gcc -o demo demo.c -I$(VLC_SRC)/include \
   -L$(VLC_INST) -llibvlc

Code for Linux (only the paths have changed)

#include <stdio.h>
#include <stdlib.h>
#include <vlc/libvlc.h>

static void quit_on_exception (libvlc_exception_t *excp) {
   if (libvlc_exception_raised (excp)) {
      fprintf(stderr, "error: %s\n", libvlc_exception_get_message(excp));
      exit(-1);
   }
}

int main(int argc, char **argv) {
   libvlc_exception_t excp;
   libvlc_instance_t *inst;
   int item;
   char *myarg0 = "-I dummy";
   char *myarg1 = "--plugin-path=/tmp/vlc-0.8.6c/";
   char *myargs[3] = {myarg0, myarg1, NULL};
   char *filename = "/tmp/WorldOfPadman_Intro.avi";

   libvlc_exception_init (&excp);
   inst = libvlc_new (2, myargs, &excp);
   quit_on_exception (&excp);
   item = libvlc_playlist_add (inst, filename, NULL, &excp); 
   quit_on_exception (&excp);
   libvlc_playlist_play (inst, item, 0, NULL, &excp); 
   quit_on_exception (&excp);
   usleep (10000000);
   libvlc_destroy (inst);
   return 0;
}

Compile/Link/Run for Linux

$ gcc -I /tmp/vlc-0.8.6c/include/ -lvlc -L /tmp/vlc-0.8.6c/build/src/.libs/ test.c -o testvlc
$ LD_LIBRARY_PATH=/tmp/vlc-0.8.6c/build/src/.libs/ ./testvlc