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.
 
(25 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 dinamica condivisa: "''libvlc.so.5.4.0''"
+
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, 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
 
  Private bo As Boolean
 
   
 
   
 
   
 
   
  Library "libvlc:5.4.0"
+
  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>' ''libvlc_time_t libvlc_media_player_get_time(libvlc_media_player_t * p_mi)''
+
  <FONT Color=gray>' ''void libvlc_media_player_release (libvlc_media_player_t * p_mi)''
  ' ''Get the current movie time (in ms).''</font>
+
  ' ''Release a media_player after use Decrement the reference count of a media player object.''</font>
  Private Extern libvlc_media_player_get_time(p_mi As Pointer) As Integer
+
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 Form_Open()
+
  Public Sub Main()
 
 
  TextBox1.Alignment = Align.Right
 
 
 
'''End'''
 
 
 
   
 
   
'''Public''' Sub Button1_Click()
 
 
 
 
   Dim inst, mp, m As Pointer
 
   Dim inst, mp, m As Pointer
  Dim ss As String[]
 
 
   Dim video As String
 
   Dim video As String
   Dim argc as Integer
+
   Dim tm As Date
 
+
  <FONT Color=gray>' ''Individua con precisione il file-device video disponibile:''</font>
+
  <FONT Color=gray>' ''Si imposta il file-device video disponibile:''</font>
  video = Dir("/dev", "video*", gb.Device)[0]
+
  video = "/dev/video0"
 
 
<FONT Color=gray>' ''Vengono impostate le opzioni ed il percorso ove sarà salvato il file video:''</font>
 
  ss = ["--sout=#transcode{vcodec=mp2v,fps=60,width=640,height=480,acodec=mp2a,scale=1,channels=2,deinterlace,audio-sync}" &
 
  ":standard{access-file,mux=ts,dst=\"/tmp/video.mpg\"}"]
 
 
 
<FONT Color=gray>' ''Imposta il valore del 1° argomento della funzione "libvlc_new()":''</font>
 
  argc = 1
 
 
 
<FONT Color=gray>' ''Inizializza la libreria VLC:''</font>
 
  inst = libvlc_new(argc, ss)
 
 
    
 
    
 +
<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)
+
  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:///dev" &/ 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)
+
 
+
<FONT Color=gray>' ''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:''</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>
 
  <FONT Color=gray>' ''Avvia la cattura del video da parte del media player:''</font>
  libvlc_media_player_play(mp)
+
  libvlc_media_player_play(mp)
 
+
  While bo = False
+
  tm = Now
    TextBox1.Text = Str(Date(0, 0, 0, 0, 0, 0, libvlc_media_player_get_time(mp)))
+
<FONT Color=gray>' ''Il Wait consente di agire su altri oggetti posti sul Form:''</font>
+
  While bo = False
    Wait 0.01
+
    Write "\r" & Str(Time(0, 0, 0, DateDiff(tm, Now, gb.Millisecond)))
  Wend
+
    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_release(inst)
+
  libvlc_media_player_release(mp)
  Me.Close
+
  libvlc_media_release(m)
 +
  libvlc_release(inst)
 +
  Quit
 
    
 
    
  '''End'''
+
  End
 
   
 
   
 
   
 
   
  '''Public''' Sub Button2_Click()
+
  Public Sub Application_Read() <FONT Color=gray>' ''Cliccando sul tasto "Invio" della tastiera si arresta la registrazione:''</font>
 
    
 
    
<FONT Color=gray>' ''Causa l'arresto della registrazione, la chiusura della libreria VLC e del programma:''</font>
+
  bo = True
  bo = True
 
 
    
 
    
  '''End'''
+
  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


Riferimenti