Differenze tra le versioni di "Salvare in un file video mediante le funzioni esterne del API di GStreamer la ripresa video effettuata con una WebCam"

Da Gambas-it.org - Wikipedia.
 
(7 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1: Riga 1:
 
La risorsa '''GStreamer''' consente anche di catturare, riprodurre, nonché salvare in un file video la ripresa video effettuata con una ''WebCam''.
 
La risorsa '''GStreamer''' consente anche di catturare, riprodurre, nonché salvare in un file video la ripresa video effettuata con una ''WebCam''.
  
Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria dinamica condivisa: "''libgstreamer-1.0''"
+
Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "''libgstreamer-1.0'' "
  
  
Riga 35: Riga 35:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
    
 
    
 
   Dim tempus As Long
 
   Dim tempus As Long
 
        
 
        
  gst_init(0, 0)
+
  gst_init(0, 0)
 
    
 
    
  <FONT Color=gray>' ''Effettua una ripresa per quasi 20 secondi. Per modificare il tempo di ripresa, bisogna cambiare il valore di "num-buffers".''
+
  <FONT Color=gray>' ''Effettua una ripresa video "senza" audio:''</font>
' ''Per porre un tempo pressoché infinito, porre il valore a -1 .''</font>
+
  webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! videoconvert ! matroskamux ! filesink <FONT Color=B22222>location=/tmp/file_video.mkv</font> sync=false", 0)
  webcam = gst_parse_launch("v4l2src <FONT Color=B22222>num-buffers=400</font>  ! queue ! vpuenc codec=0 ! matroskamux ! filesink <FONT Color=B22222>location=/tmp/file_immagine.mkv</font> sync=false", 0)
 
 
        
 
        
 
  <FONT Color=gray>' ''Avviamo la riproduzione video:''</font>
 
  <FONT Color=gray>' ''Avviamo la riproduzione video:''</font>
  gst_element_set_state(webcam, GST_STATE_PLAYING)
+
  gst_element_set_state(webcam, GST_STATE_PLAYING)
 
+
  While True
+
  Do
    gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
+
    gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
    Write #File.Out, "\rTempo: " & Date(0, 0, 0, 0, 0, 0, tempus / 1000000)
+
    Write #File.Out, "\rTempo: " & Time(0, 0, 0, tempus / 1000000)
    Wait 0.01
+
    Wait 0.01
  Wend
+
  Loop
     
+
 
  '''End'''
+
  End  
 
   
 
   
 
   
 
   
  '''Public''' Sub Application_Read()
+
  Public Sub Application_Read()
 
   
 
   
Dim s As String
+
  Dim s As String
 
   
 
   
 
   Input #File.In, s
 
   Input #File.In, s
Riga 78: Riga 77:
 
   End Select
 
   End Select
 
   
 
   
  '''End'''
+
  End
 +
 
 +
====Salvare il video - privo di audio - in un file di formato OGG-Theora====
 +
Se si intende salvare il video - privo di audio - in un file di formato OGG-Theora, si adotterà la seguente riga nel precedente codice:
 +
webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! videoconvert ! theoraenc ! oggmux ! filesink location=/tmp/file_video.ogg sync=false", 0)
  
  
Riga 84: Riga 87:
 
In quest'altro capitolo vedremo le modalità per salvare una ripresa video ''comprensiva di audio'' mediante una WebCam. Si costruirà una ''Pipeline'', per connettere i vari ''elementi'' di ''GStreamer'', con una linea di comando mediante la funzione esterna ''gst_parse_launch()''.
 
In quest'altro capitolo vedremo le modalità per salvare una ripresa video ''comprensiva di audio'' mediante una WebCam. Si costruirà una ''Pipeline'', per connettere i vari ''elementi'' di ''GStreamer'', con una linea di comando mediante la funzione esterna ''gst_parse_launch()''.
  
 
+
===File audio-video di formato ''MKV''===
====File audio-video di formato ''OGV''====
+
In questo esempio il file finale creato sarà di formato ''MKV'' (''Matroska''):
In questo esempio il file finale creato sarà di formato ''OGV'':
 
 
  Private webcam As Pointer
 
  Private webcam As Pointer
 
   
 
   
Riga 116: Riga 118:
 
   
 
   
 
   
 
   
  '''Public''' Sub Main()
+
  Public Sub Main()
 
    
 
    
 
   Dim tempus As Long
 
   Dim tempus As Long
 
        
 
        
  gst_init(0, 0)
+
  gst_init(0, 0)
 
    
 
    
  webcam = gst_parse_launch("v4l2src ! videorate ! video/x-raw,framerate=30/1 ! queue ! theoraenc ! queue ! " &
+
  webcam = gst_parse_launch("<FONT Color=darkgreen>v4l2src device=/dev/video0 ! queue ! videoconvert ! mkv. autoaudiosrc ! queue ! audioconvert ! mkv. matroskamux name=mkv ! filesink location=/tmp/test.mkv sync=false</font>", 0)
                            "mux. alsasrc ! audio/x-raw,rate=44100,channels=2,depth=16 ! queue ! audioconvert ! queue ! " &
 
                            "vorbisenc ! queue ! mux. oggmux name=mux ! filesink location=<font Color=gray>''/percorso/del/file.ogv</font>", 0)
 
 
        
 
        
 
  <FONT Color=gray>' ''Avviamo la riproduzione audio-video:''</font>
 
  <FONT Color=gray>' ''Avviamo la riproduzione audio-video:''</font>
  gst_element_set_state(webcam, GST_STATE_PLAYING)
+
  gst_element_set_state(webcam, GST_STATE_PLAYING)
 
+
  While True
+
  Do
    gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
+
    gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
    Write #File.Out, "\rTempo: " & Date(0, 0, 0, 0, 0, 0, tempus / 1000000)
+
    Write "\rTempo: " & Time(0, 0, 0, tempus / 1000000)
    Wait 0.01
+
    Flush
  Wend
+
    Wait 0.01
     
+
  Loop
  '''End'''
+
 +
  Print "\nEsecuzione terminata."
 +
  Quit
 +
 +
  End
 
   
 
   
 
   
 
   
  '''Public''' Sub Application_Read()
+
  Public Sub Application_Read()
 
   
 
   
Dim s As String
+
  Dim s As String
 
   
 
   
 
   Input #File.In, s
 
   Input #File.In, s
Riga 155: Riga 159:
 
       gst_element_set_state(webcam, GST_STATE_NULL)
 
       gst_element_set_state(webcam, GST_STATE_NULL)
 
       gst_object_unref(webcam)
 
       gst_object_unref(webcam)
      Print "\nEsecuzione terminata."
 
      Quit
 
 
   End Select
 
   End Select
 
   
 
   
  '''End'''
+
  End
 
 
 
 
====Mostrare nel video anche il tempo trascorso====
 
Per mostrare nella finestra del video anche il tempo trascorso dall'inizio della ripresa video, è necessario utilizzare il plugin "''timeoverlay''" fornito da ''GStreamer''. Pertanto la stringa del primo argomento della funzione esterna ''gst_parse_launch( )'' diventa come segue:
 
webcam = gst_parse_launch("v4l2src ! videorate ! video/x-raw,framerate=30/1 ! <FONT Color=#B22222>timeoverlay</font> ! queue ! " &
 
                          "theoraenc ! queue ! mux. alsasrc ! audio/x-raw,rate=44100,channels=2,depth=16 ! queue ! " &
 
                          "audioconvert ! queue ! vorbisenc ! queue ! mux. oggmux name=mux ! filesink location=<FONT Color=gray>''/percorso/del/file.ogv''</font>", 0)
 
 
 
 
 
====File audio-video di formato ''MKV'' (''Matroska'')  codifica ''h264''====
 
Se si intende salvare in un file video di formato ''MKV'' (''matroska'') con codifica ''h264'', la stringa del primo argomento della funzione esterna ''gst_parse_launch( )'' diventa la seguente:
 
webcam = gst_parse_launch("v4l2src ! video/x-raw,width=640,height=480,framerate=30/1 ! x264enc tune=zerolatency ! " &
 
                          "mux. alsasrc ! audio/x-raw,width=16,depth=16,rate=44100,channels=2 ! queue ! matroskamux name=mux ! filesink location=<FONT Color=gray>''/percorso/del/file.mkv''</font>", 0)
 
 
 
 
 
====File audio-video di formato ''AVI''====
 
Se si intende salvare in un file video di formato ''AVI'', la stringa del primo argomento della funzione esterna ''gst_parse_launch( )'' diventa la seguente:
 
webcam = gst_parse_launch("v4l2src ! videorate ! video/x-raw,width=640,height=480,framerate=30/1  ! queue ! mfw_vpuencoder codec-type=2 ! queue ! " &
 
                          "mux. alsasrc ! audio/x-raw,width=16,depth=16,rate=44100,channels=2 ! queue ! avimux name=mux ! filesink location=<FONT Color=gray>''/percorso/del/file.avi''</font>", 0)
 
  
 +
===Mostrare nel video anche il tempo trascorso===
 +
Per mostrare nella finestra del video anche il tempo trascorso dall'inizio della ripresa video, è necessario utilizzare il plugin ''timeoverlay'' fornito da ''GStreamer''. Pertanto la stringa del primo argomento della funzione esterna "gst_parse_launch()" diventa come segue:
 +
webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! <FONT Color=#B22222>timeoverlay</font> ! videoconvert ! mkv. autoaudiosrc ! queue ! audioconvert ! mkv. matroskamux name=mkv ! filesink location=/tmp/test.mkv sync=false", 0)
  
  
Riga 186: Riga 172:
 
* http://gstreamer.freedesktop.org/
 
* http://gstreamer.freedesktop.org/
 
* http://gstreamer.com/
 
* http://gstreamer.com/
* http://www.noah.org/wiki/gstreamer
 
* http://wiki.oz9aec.net/index.php/Gstreamer_cheat_sheet
 
 
* https://www.linuxtv.org/wiki/index.php/GStreamer
 
* https://www.linuxtv.org/wiki/index.php/GStreamer

Versione attuale delle 06:38, 12 dic 2023

La risorsa GStreamer consente anche di catturare, riprodurre, nonché salvare in un file video la ripresa video effettuata con una WebCam.

Sarà necessario avere installata nel sistema e richiamare nell'applicazione Gambas la libreria condivisa: "libgstreamer-1.0 "


Effettuare e salvare un video senza audio

Mostriamo di seguito un esempo pratico di ripresa video - senza audio - in un'applicazione a riga di comando. Oltre a non contenere audio, non saranno mostrate in contemporanea le immagini della ripresa video. La ripresa video sarà salvata in file immagine di formato MKV.

Private webcam As Pointer


Library "libgstreamer-1.0"

Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING
Private Const GST_FORMAT_TIME As Integer = 3

' gst_init (int *argc, char **argv[])
' Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.
Private Extern gst_init(argc As Pointer, argv As Pointer)

' GstElement * gst_parse_launch (const gchar *pipeline_description, GError **error)
' Create a new pipeline based on command line syntax.
Private Extern gst_parse_launch(description As String, GError As Pointer) As Pointer

' GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state)
' Sets the state of the element.
Private Extern gst_element_set_state(gstelement As Pointer, state As Integer) As Integer
 
' gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *cur)
' Queries an element for the stream position in nanoseconds.
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur As Pointer) As Boolean

' void gst_object_unref(gpointer object)
' Decrements the reference count on object.
Private Extern gst_object_unref(gobject As Pointer)


Public Sub Main()
 
 Dim tempus As Long
     
 gst_init(0, 0)
  
' Effettua una ripresa video "senza" audio:
 webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! videoconvert ! matroskamux ! filesink location=/tmp/file_video.mkv sync=false", 0)
     
' Avviamo la riproduzione video:
 gst_element_set_state(webcam, GST_STATE_PLAYING)

 Do
   gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
   Write #File.Out, "\rTempo: " & Time(0, 0, 0, tempus / 1000000)
   Wait 0.01
 Loop
 
End 


Public Sub Application_Read()

 Dim s As String

 Input #File.In, s
  
 Select Case s
   Case "p"
' Pone in pausa la riproduzione del file mediale
     gst_element_set_state(webcam, GST_STATE_PAUSED)
   Case "r"
' Riprende la riproduzione del file mediale
     gst_element_set_state(webcam, GST_STATE_PLAYING)
   Case "s"
' Arresta la riproduzione del file mediale
     gst_element_set_state(webcam, GST_STATE_NULL)
     gst_object_unref(webcam)
     Print "\nEsecuzione terminata."
     Quit
 End Select

End

Salvare il video - privo di audio - in un file di formato OGG-Theora

Se si intende salvare il video - privo di audio - in un file di formato OGG-Theora, si adotterà la seguente riga nel precedente codice:

webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! videoconvert ! theoraenc ! oggmux ! filesink location=/tmp/file_video.ogg sync=false", 0)


Effettuare e salvare un video con audio mediante la Pipeline della funzione gst_parse_launch()

In quest'altro capitolo vedremo le modalità per salvare una ripresa video comprensiva di audio mediante una WebCam. Si costruirà una Pipeline, per connettere i vari elementi di GStreamer, con una linea di comando mediante la funzione esterna gst_parse_launch().

File audio-video di formato MKV

In questo esempio il file finale creato sarà di formato MKV (Matroska):

Private webcam As Pointer


Library "libgstreamer-1.0"

Private Enum GST_STATE_VOID_PENDING = 0, GST_STATE_NULL, GST_STATE_READY, GST_STATE_PAUSED, GST_STATE_PLAYING
Private Const GST_FORMAT_TIME As Integer = 3

' gst_init (int *argc, char **argv[])
' Initializes the GStreamer library, setting up internal path lists, registering built-in elements, and loading standard plugins.
Private Extern gst_init(argc As Pointer, argv As Pointer)

' GstElement * gst_parse_launch (const gchar *pipeline_description, GError **error)
' Create a new pipeline based on command line syntax.
Private Extern gst_parse_launch(description As String, GError As Pointer) As Pointer

' GstStateChangeReturn gst_element_set_state(GstElement *element, GstState state)
' Sets the state of the element.
Private Extern gst_element_set_state(gstelement As Pointer, state As Integer) As Integer
 
' gboolean gst_element_query_position (GstElement *element, GstFormat format, gint64 *cur)
' Queries an element for the stream position in nanoseconds.
Private Extern gst_element_query_position(gstelement As Pointer, gstformat As Pointer, cur As Pointer) As Boolean

' void gst_object_unref(gpointer object)
' Decrements the reference count on object.
Private Extern gst_object_unref(gobject As Pointer)


Public Sub Main()
 
 Dim tempus As Long
     
 gst_init(0, 0)
  
 webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! videoconvert ! mkv. autoaudiosrc ! queue ! audioconvert ! mkv. matroskamux name=mkv ! filesink location=/tmp/test.mkv sync=false", 0)
     
' Avviamo la riproduzione audio-video:
 gst_element_set_state(webcam, GST_STATE_PLAYING)

 Do
   gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
   Write "\rTempo: " & Time(0, 0, 0, tempus / 1000000)
   Flush
   Wait 0.01
 Loop

 Print "\nEsecuzione terminata."
 Quit

End


Public Sub Application_Read()

 Dim s As String

 Input #File.In, s
  
 Select Case s
   Case "p"
' Pone in pausa la riproduzione del file mediale
     gst_element_set_state(webcam, GST_STATE_PAUSED)
   Case "r"
' Riprende la riproduzione del file mediale
     gst_element_set_state(webcam, GST_STATE_PLAYING)
   Case "s"
' Arresta la riproduzione del file mediale
     gst_element_set_state(webcam, GST_STATE_NULL)
     gst_object_unref(webcam)
 End Select

End

Mostrare nel video anche il tempo trascorso

Per mostrare nella finestra del video anche il tempo trascorso dall'inizio della ripresa video, è necessario utilizzare il plugin timeoverlay fornito da GStreamer. Pertanto la stringa del primo argomento della funzione esterna "gst_parse_launch()" diventa come segue:

webcam = gst_parse_launch("v4l2src device=/dev/video0 ! queue ! timeoverlay ! videoconvert ! mkv. autoaudiosrc ! queue ! audioconvert ! mkv. matroskamux name=mkv ! filesink location=/tmp/test.mkv sync=false", 0)


Riferimenti