Differenze tra le versioni di "Catturare e riprodurre immagini video mediante una WebCam con le funzioni esterne delle API di VLC"
Riga 18: | Riga 18: | ||
' ''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>' ''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)'' | <FONT Color=gray>' ''libvlc_media_player_t * libvlc_media_player_new (libvlc_instance_t *p_libvlc_instance)'' | ||
Riga 31: | 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>' ''void libvlc_media_player_release (libvlc_media_player_t * p_mi)'' | <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> | ' ''Release a media_player after use Decrement the reference count of a media player object.''</font> | ||
Riga 51: | Riga 51: | ||
Dim inst, mp, m As Pointer | Dim inst, mp, m As Pointer | ||
− | |||
Dim video As String | Dim video As String | ||
− | Dim | + | Dim tm As Date |
− | + | ||
<FONT Color=gray>' ''Si imposta il file-device video disponibile:''</font> | <FONT Color=gray>' ''Si imposta il file-device video disponibile:''</font> | ||
video = "/dev/video0" | 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"]''' | |
− | + | inst = libvlc_new(1, ["vcodec=h264,fps=30,vb=512"]) | |
− | |||
− | |||
− | inst = libvlc_new( | ||
If inst == 0 Then Error.Raise("Impossibile inizializzare la libreria VLC !") | 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) | mp = libvlc_media_player_new(inst) | ||
If mp == 0 Then Error.Raise("Impossibile creare il media player !") | 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> | ||
Riga 81: | Riga 77: | ||
<FONT Color=gray>' ''Avvia la cattura del video da parte del media player:''</font> | <FONT Color=gray>' ''Avvia la cattura del video da parte del media player:''</font> | ||
libvlc_media_player_play(mp) | libvlc_media_player_play(mp) | ||
− | + | ||
+ | tm = Now | ||
+ | |||
While bo = False | While bo = False | ||
− | + | Write "\r" & Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond))) | |
Wait 0.01 | Wait 0.01 | ||
Wend | Wend |
Versione delle 18:25, 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, precisando che, se il valore del primo argomento della funzione "libvlc_new()" è uguale a zero, allora verrà semplicemente mostrata la ripresa video in tempo reale, ma non verrà effettuata alcuna registrazione; se il valore è uguale a 1, allora verrà effettuata la registrazione su file video, ma non verrà mostrato contemporaneamente la ripresa video.
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 !") 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
Indice
Ottenere altri formati di file video
Oltre al formato MPG è possibile ottenere qualche altro formato di file video.
Formato video .mp4
Per ottenere un file video di formato MP4, è necessario sostituire la stringa delle opzioni con la seguente:
ss = ["--sout=#transcode{vcodec=mp4v,fps=60,vb=512,width=640,height=480}:standard{access=file,mux=ts,dst=/tmp/video.mp4}"]
Formato video .flv
Per ottenere un file video di formato FLV, è necessario sostituire la stringa delle opzioni con la seguente:
ss = ["--sout=#transcode{vcodec=flv,fps=60,vb=512,width=640,height=480}:standard{access=file,mux=flv,dst=/tmp/video.flv}"]