Differenze tra le versioni di "Catturare e riprodurre immagini video mediante una WebCam con le funzioni esterne delle API di VLC"
Da Gambas-it.org - Wikipedia.
(30 versioni intermedie di uno stesso utente non sono mostrate) | |||
Riga 1: | Riga 1: | ||
La risorsa '''VLC''' consente anche di catturare e riprodurre immagini video mediante una WebCam. | La risorsa '''VLC''' consente anche di catturare e riprodurre immagini video mediante una WebCam. | ||
− | Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria | + | Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "''libvlc.so.5.6.1'' ". |
Inoltre, bisognerà avere installata nel sistema la libreria ''v4l2'', poiché ''VCL'' fa uso di tale risorsa per la registrazione video. | Inoltre, bisognerà avere installata nel sistema la libreria ''v4l2'', poiché ''VCL'' fa uso di tale risorsa per la registrazione video. | ||
− | + | Mostriamo di seguito un esempio di cattura di immagini video - <U>senza audio</u> - mediante una webCam con un'applicazione in ambiente grafico. | |
− | Mostriamo di seguito un esempio di cattura di immagini video mediante una webCam con un'applicazione in ambiente grafico | ||
Private bo As Boolean | Private bo As Boolean | ||
− | Library "libvlc:5. | + | Library "libvlc:5.6.1" |
<FONT Color=gray>' ''libvlc_instance_t * libvlc_new (int argc, const char *const *argv)'' | <FONT Color=gray>' ''libvlc_instance_t * libvlc_new (int argc, const char *const *argv)'' | ||
Riga 16: | Riga 15: | ||
Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer | Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer | ||
− | <FONT Color=gray>' ''libvlc_media_t *libvlc_media_new_location(libvlc_instance_t *p_instance, const char * psz_mrl)'' | + | <FONT Color=gray>' ''libvlc_media_t *libvlc_media_new_location (libvlc_instance_t *p_instance, const char * psz_mrl)'' |
' ''Create a media with a certain given media resource location, for instance a valid URL.''</font> | ' ''Create a media with a certain given media resource location, for instance a valid URL.''</font> | ||
Private Extern libvlc_media_new_location(p_instance As Pointer, path As String) As Pointer | Private Extern libvlc_media_new_location(p_instance As Pointer, path As String) As Pointer | ||
− | <FONT Color=gray>' ''libvlc_media_player_t * libvlc_media_player_new(libvlc_instance_t *p_libvlc_instance)'' | + | <FONT Color=gray>' ''void libvlc_media_add_option (libvlc_media_t *p_md, const char *psz_options)'' |
+ | ' ''Add an option to the media.''</font> | ||
+ | Private Extern libvlc_media_add_option(p_mi As Pointer, psz_options As String) | ||
+ | |||
+ | <FONT Color=gray>' ''libvlc_media_player_t * libvlc_media_player_new (libvlc_instance_t *p_libvlc_instance)'' | ||
' ''Create an empty Media Player object.''</font> | ' ''Create an empty Media Player object.''</font> | ||
Private Extern libvlc_media_player_new(p_libvlc_instance As Pointer) As Pointer | Private Extern libvlc_media_player_new(p_libvlc_instance As Pointer) As Pointer | ||
− | <FONT Color=gray>' ''void libvlc_media_player_set_media(libvlc_media_player_t *p_mi, libvlc_media_t *p_md)'' | + | <FONT Color=gray>' ''void libvlc_media_player_set_media (libvlc_media_player_t *p_mi, libvlc_media_t *p_md)'' |
' ''Set the media that will be used by the media_player.''</font> | ' ''Set the media that will be used by the media_player.''</font> | ||
Private Extern libvlc_media_player_set_media(p_md As Pointer, mp As Pointer) | Private Extern libvlc_media_player_set_media(p_md As Pointer, mp As Pointer) | ||
Riga 32: | Riga 35: | ||
Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer | Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer | ||
− | <FONT Color=gray>' '' | + | <FONT Color=gray>' ''void libvlc_media_player_release (libvlc_media_player_t * p_mi)'' |
− | ' '' | + | ' ''Release a media_player after use Decrement the reference count of a media player object.''</font> |
− | Private Extern | + | Private Extern libvlc_media_player_release(p_mi As Pointer) |
+ | |||
+ | <FONT color=gray>' ''void libvlc_media_release (libvlc_media_t *p_md)'' | ||
+ | ' ''Decrement the reference count of a media descriptor object.''</font> | ||
+ | Private Extern libvlc_media_release(p_md As Pointer) | ||
<FONT Color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)'' | <FONT Color=gray>' ''libvlc_release (libvlc_instance_t * p_instance)'' | ||
Riga 41: | Riga 48: | ||
− | + | Public Sub Main() | |
− | |||
− | |||
− | |||
− | |||
+ | Dim inst, mp, m As Pointer | ||
+ | Dim video As String | ||
+ | Dim tm As Date | ||
− | + | <FONT Color=gray>' ''Si imposta il file-device video disponibile:''</font> | |
− | + | video = "/dev/video0" | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | <FONT Color=gray>' '' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | <FONT Color=gray>' ''Inizializza la libreria VLC e imposta le opzioni relative al video.'' | ||
+ | ' ''Se si vuole ottenere un file video MP4:'' '''["vcodec=mp4v,fps=30,vb=512"]''' | ||
+ | ' ''Se si vuole ottenere un file video AVI:'' '''["vcodec=h264,fps=30,vb=512"]''' | ||
+ | ' ''oppure per il formato AVI anche:'' '''["vcodec=h265,fps=30,vb=512"]'''''</font> | ||
+ | inst = libvlc_new(1, ["vcodec=h264,fps=30,vb=512"]) | ||
+ | If inst == 0 Then Error.Raise("Impossibile inizializzare la libreria VLC !") | ||
+ | |||
<FONT Color=gray>' ''Crea il media player:''</font> | <FONT Color=gray>' ''Crea il media player:''</font> | ||
− | + | mp = libvlc_media_player_new(inst) | |
− | + | If mp == 0 Then Error.Raise("Impossibile creare il media player !") | |
+ | |||
<FONT Color=gray>' ''Crea un nuovo oggetto multimedia.'' | <FONT Color=gray>' ''Crea un nuovo oggetto multimedia.'' | ||
' ''Nel secondo argomento della funzione va specificato il percorso del file-device video:''</font> | ' ''Nel secondo argomento della funzione va specificato il percorso del file-device video:''</font> | ||
− | + | m = libvlc_media_new_location(inst, "v4l2://" & video) | |
− | + | If m == 0 Then Error.Raise("Impossibile creare un oggetto multimediale !") | |
− | + | ||
− | + | <FONT Color=gray>' ''Se si vuole ottenere un file video MP4:'' | |
− | <FONT Color=gray>' ''Avvia | + | ' '''libvlc_media_add_option(m, ":sout=#duplicate{dst=display,dst=std{access=file,mux=mp4,dst=/tmp/video.mp4}}")''' |
− | + | ' ''Se si vuole ottenere un file video AVI:''</font> | |
− | + | libvlc_media_add_option(m, ":sout=#duplicate{dst=display,dst=std{access=file,mux=avi,dst=/tmp/video.avi}") | |
− | + | ||
− | + | libvlc_media_player_set_media(mp, m) | |
− | + | ||
− | + | <FONT Color=gray>' ''Avvia la cattura del video da parte del media player:''</font> | |
− | + | libvlc_media_player_play(mp) | |
+ | |||
+ | tm = Now | ||
+ | |||
+ | While bo = False | ||
+ | Write "\r" & Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond))) | ||
+ | Wait 0.01 | ||
+ | Wend | ||
<FONT Color=gray>' ''Chiude la libreria VLC ed il programma:''</font> | <FONT Color=gray>' ''Chiude la libreria VLC ed il programma:''</font> | ||
− | + | libvlc_media_player_release(mp) | |
− | + | libvlc_media_release(m) | |
+ | libvlc_release(inst) | ||
+ | Quit | ||
− | + | End | |
− | ''' | + | Public Sub Application_Read() <FONT Color=gray>' ''Cliccando sul tasto "Invio" della tastiera si arresta la registrazione:''</font> |
− | + | bo = True | |
− | |||
− | + | End | |
− | |||
Versione attuale delle 18:37, 21 ago 2024
La risorsa VLC consente anche di catturare e riprodurre immagini video mediante una WebCam.
Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "libvlc.so.5.6.1 ".
Inoltre, bisognerà avere installata nel sistema la libreria v4l2, poiché VCL fa uso di tale risorsa per la registrazione video.
Mostriamo di seguito un esempio di cattura di immagini video - senza audio - mediante una webCam con un'applicazione in ambiente grafico.
Private bo As Boolean Library "libvlc:5.6.1" ' libvlc_instance_t * libvlc_new (int argc, const char *const *argv) ' Create And initialize a libvlc instance. Private Extern libvlc_new(argc As Integer, argv As String[]) As Pointer ' libvlc_media_t *libvlc_media_new_location (libvlc_instance_t *p_instance, const char * psz_mrl) ' Create a media with a certain given media resource location, for instance a valid URL. Private Extern libvlc_media_new_location(p_instance As Pointer, path As String) As Pointer ' void libvlc_media_add_option (libvlc_media_t *p_md, const char *psz_options) ' Add an option to the media. Private Extern libvlc_media_add_option(p_mi As Pointer, psz_options As String) ' libvlc_media_player_t * libvlc_media_player_new (libvlc_instance_t *p_libvlc_instance) ' Create an empty Media Player object. Private Extern libvlc_media_player_new(p_libvlc_instance As Pointer) As Pointer ' void libvlc_media_player_set_media (libvlc_media_player_t *p_mi, libvlc_media_t *p_md) ' Set the media that will be used by the media_player. Private Extern libvlc_media_player_set_media(p_md As Pointer, mp As Pointer) ' int libvlc_media_player_play (libvlc_media_player_t * p_mi) ' Play the video file. Private Extern libvlc_media_player_play(p_mi As Pointer) As Integer ' void libvlc_media_player_release (libvlc_media_player_t * p_mi) ' Release a media_player after use Decrement the reference count of a media player object. Private Extern libvlc_media_player_release(p_mi As Pointer) ' void libvlc_media_release (libvlc_media_t *p_md) ' Decrement the reference count of a media descriptor object. Private Extern libvlc_media_release(p_md As Pointer) ' libvlc_release (libvlc_instance_t * p_instance) ' Decrement the reference count of a libvlc instance, and destroy it if it reaches zero. Private Extern libvlc_release(p_instance As Pointer) Public Sub Main() Dim inst, mp, m As Pointer Dim video As String Dim tm As Date ' Si imposta il file-device video disponibile: video = "/dev/video0" ' Inizializza la libreria VLC e imposta le opzioni relative al video. ' Se si vuole ottenere un file video MP4: ["vcodec=mp4v,fps=30,vb=512"] ' Se si vuole ottenere un file video AVI: ["vcodec=h264,fps=30,vb=512"] ' oppure per il formato AVI anche: ["vcodec=h265,fps=30,vb=512"] inst = libvlc_new(1, ["vcodec=h264,fps=30,vb=512"]) If inst == 0 Then Error.Raise("Impossibile inizializzare la libreria VLC !") ' Crea il media player: mp = libvlc_media_player_new(inst) If mp == 0 Then Error.Raise("Impossibile creare il media player !") ' Crea un nuovo oggetto multimedia. ' Nel secondo argomento della funzione va specificato il percorso del file-device video: m = libvlc_media_new_location(inst, "v4l2://" & video) If m == 0 Then Error.Raise("Impossibile creare un oggetto multimediale !") ' Se si vuole ottenere un file video MP4: ' libvlc_media_add_option(m, ":sout=#duplicate{dst=display,dst=std{access=file,mux=mp4,dst=/tmp/video.mp4}}") ' Se si vuole ottenere un file video AVI: libvlc_media_add_option(m, ":sout=#duplicate{dst=display,dst=std{access=file,mux=avi,dst=/tmp/video.avi}") libvlc_media_player_set_media(mp, m) ' Avvia la cattura del video da parte del media player: libvlc_media_player_play(mp) tm = Now While bo = False Write "\r" & Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond))) Wait 0.01 Wend ' Chiude la libreria VLC ed il programma: libvlc_media_player_release(mp) libvlc_media_release(m) libvlc_release(inst) Quit End Public Sub Application_Read() ' Cliccando sul tasto "Invio" della tastiera si arresta la registrazione: bo = True End