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.
Riga 82: Riga 82:
  
 
=Effettuare e salvare un video con audio mediante la ''Pipeline'' della funzione ''gst_parse_launch()''=
 
=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. Il file finale creato sarà di formato ''OGV''. 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 ''OGV''====
 +
In questo esempio il file finale creato sarà di formato ''OGV'':
 
  Private webcam As Pointer
 
  Private webcam As Pointer
 
   
 
   
Riga 163: Riga 167:
 
                           "theoraenc ! queue ! mux. alsasrc ! audio/x-raw,rate=44100,channels=2,depth=16 ! 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)
 
                           "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)
  
  

Versione delle 18:13, 6 ott 2016

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"


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 per quasi 20 secondi. Per modificare il tempo di ripresa, bisogna cambiare il valore di "num-buffers".
' Per porre un tempo pressoché infinito, porre il valore a -1 .
  webcam = gst_parse_launch("v4l2src num-buffers=400  ! queue ! vpuenc codec=0 ! matroskamux ! filesink location=/tmp/file_immagine.mkv sync=false", 0)
     
' Avviamo la riproduzione video:
  gst_element_set_state(webcam, GST_STATE_PLAYING)
  
  While True
    gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
    Write #File.Out, "\rTempo: " & Date(0, 0, 0, 0, 0, 0, tempus / 1000000)
    Wait 0.01
  Wend
      
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


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 OGV

In questo esempio il file finale creato sarà di formato OGV:

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 ! videorate ! video/x-raw,framerate=5/1 ! 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=/percorso/del/file.ogv", 0)
     
' Avviamo la riproduzione audio-video:
  gst_element_set_state(webcam, GST_STATE_PLAYING)
  
  While True
    gst_element_query_position(webcam, GST_FORMAT_TIME, VarPtr(tempus))
    Write #File.Out, "\rTempo: " & Date(0, 0, 0, 0, 0, 0, tempus / 1000000)
    Wait 0.01
  Wend
      
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


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=5/1 ! timeoverlay ! 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=/percorso/del/file.ogv", 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=/percorso/del/file.mkv", 0)



Riferimenti